diff --git a/docs/pwa.md b/docs/pwa.md index 98ff6302..7cf0200f 100644 --- a/docs/pwa.md +++ b/docs/pwa.md @@ -17,17 +17,18 @@ Create a sw.js file in your documents root directory and copy this code. * Register service worker. * ========================================================== */ -const RUNTIME = 'docsify'; +const RUNTIME = 'docsify' const HOSTNAME_WHITELIST = [ self.location.hostname, 'fonts.gstatic.com', - 'fonts.googleapis.com' + 'fonts.googleapis.com', + 'unpkg.com' ] // The Util Function to hack URLs of intercepted requests const getFixedUrl = (req) => { - var now = Date.now(); - url = new URL(req.url) + var now = Date.now() + var url = new URL(req.url) // 1. fixed http URL // Just keep syncing with location.protocol @@ -40,7 +41,9 @@ const getFixedUrl = (req) => { // max-age on mutable content is error-prone, with SW life of bugs can even extend. // Until cache mode of Fetch API landed, we have to workaround cache-busting with query string. // Cache-Control-Bug: https://bugs.chromium.org/p/chromium/issues/detail?id=453190 - url.search += (url.search ? '&' : '?') + 'cache-bust=' + now; + if (url.hostname === self.location.hostname) { + url.search += (url.search ? '&' : '?') + 'cache-bust=' + now + } return url.href } @@ -50,16 +53,15 @@ const getFixedUrl = (req) => { * * waitUntil(): activating ====> activated */ -self.addEventListener('activate', event => { - event.waitUntil(self.clients.claim()); -}); - +self.addEventListener('activate', event => { + event.waitUntil(self.clients.claim()) +}) /** * @Functional Fetch * All network requests are being intercepted here. * - * void respondWith(Promise r); + * void respondWith(Promise r) */ self.addEventListener('fetch', event => { // Skip some of cross-origin requests, like those for Google Analytics. @@ -67,10 +69,10 @@ self.addEventListener('fetch', event => { // Stale-while-revalidate // similar to HTTP's stale-while-revalidate: https://www.mnot.net/blog/2007/12/12/stale // Upgrade from Jake's to Surma's: https://gist.github.com/surma/eb441223daaedf880801ad80006389f1 - const cached = caches.match(event.request); - const fixedUrl = getFixedUrl(event.request); - const fetched = fetch(fixedUrl, {cache: 'no-store'}); - const fetchedCopy = fetched.then(resp => resp.clone()); + const cached = caches.match(event.request) + const fixedUrl = getFixedUrl(event.request) + const fetched = fetch(fixedUrl, { cache: 'no-store' }) + const fetchedCopy = fetched.then(resp => resp.clone()) // Call respondWith() with whatever we get first. // If the fetch fails (e.g disconnected), wait for the cache. @@ -79,17 +81,17 @@ self.addEventListener('fetch', event => { event.respondWith( Promise.race([fetched.catch(_ => cached), cached]) .then(resp => resp || fetched) - .catch(_ => {/* eat any errors */}) - ); + .catch(_ => { /* eat any errors */ }) + ) // Update the cache with the version we fetched (only for ok status) event.waitUntil( Promise.all([fetchedCopy, caches.open(RUNTIME)]) .then(([response, cache]) => response.ok && cache.put(event.request, response)) - .catch(_ => {/* eat any errors */}) - ); + .catch(_ => { /* eat any errors */ }) + ) } -}); +}) ``` ## Register diff --git a/docs/sw.js b/docs/sw.js index 1c6757fd..1e4aaeb7 100644 --- a/docs/sw.js +++ b/docs/sw.js @@ -6,17 +6,18 @@ * Register service worker. * ========================================================== */ -const RUNTIME = 'docsify'; +const RUNTIME = 'docsify' const HOSTNAME_WHITELIST = [ self.location.hostname, 'fonts.gstatic.com', - 'fonts.googleapis.com' + 'fonts.googleapis.com', + 'unpkg.com' ] // The Util Function to hack URLs of intercepted requests const getFixedUrl = (req) => { - var now = Date.now(); - url = new URL(req.url) + var now = Date.now() + var url = new URL(req.url) // 1. fixed http URL // Just keep syncing with location.protocol @@ -29,7 +30,9 @@ const getFixedUrl = (req) => { // max-age on mutable content is error-prone, with SW life of bugs can even extend. // Until cache mode of Fetch API landed, we have to workaround cache-busting with query string. // Cache-Control-Bug: https://bugs.chromium.org/p/chromium/issues/detail?id=453190 - url.search += (url.search ? '&' : '?') + 'cache-bust=' + now; + if (url.hostname === self.location.hostname) { + url.search += (url.search ? '&' : '?') + 'cache-bust=' + now + } return url.href } @@ -39,16 +42,15 @@ const getFixedUrl = (req) => { * * waitUntil(): activating ====> activated */ -self.addEventListener('activate', event => { - event.waitUntil(self.clients.claim()); -}); - +self.addEventListener('activate', event => { + event.waitUntil(self.clients.claim()) +}) /** * @Functional Fetch * All network requests are being intercepted here. * - * void respondWith(Promise r); + * void respondWith(Promise r) */ self.addEventListener('fetch', event => { // Skip some of cross-origin requests, like those for Google Analytics. @@ -56,10 +58,10 @@ self.addEventListener('fetch', event => { // Stale-while-revalidate // similar to HTTP's stale-while-revalidate: https://www.mnot.net/blog/2007/12/12/stale // Upgrade from Jake's to Surma's: https://gist.github.com/surma/eb441223daaedf880801ad80006389f1 - const cached = caches.match(event.request); - const fixedUrl = getFixedUrl(event.request); - const fetched = fetch(fixedUrl, {cache: 'no-store'}); - const fetchedCopy = fetched.then(resp => resp.clone()); + const cached = caches.match(event.request) + const fixedUrl = getFixedUrl(event.request) + const fetched = fetch(fixedUrl, { cache: 'no-store' }) + const fetchedCopy = fetched.then(resp => resp.clone()) // Call respondWith() with whatever we get first. // If the fetch fails (e.g disconnected), wait for the cache. @@ -68,14 +70,14 @@ self.addEventListener('fetch', event => { event.respondWith( Promise.race([fetched.catch(_ => cached), cached]) .then(resp => resp || fetched) - .catch(_ => {/* eat any errors */}) - ); + .catch(_ => { /* eat any errors */ }) + ) // Update the cache with the version we fetched (only for ok status) event.waitUntil( Promise.all([fetchedCopy, caches.open(RUNTIME)]) .then(([response, cache]) => response.ok && cache.put(event.request, response)) - .catch(_ => {/* eat any errors */}) - ); + .catch(_ => { /* eat any errors */ }) + ) } -}); +}) diff --git a/docs/zh-cn/pwa.md b/docs/zh-cn/pwa.md index 7eb7ac65..f8d03ff5 100644 --- a/docs/zh-cn/pwa.md +++ b/docs/zh-cn/pwa.md @@ -17,17 +17,18 @@ * Register service worker. * ========================================================== */ -const RUNTIME = 'docsify'; +const RUNTIME = 'docsify' const HOSTNAME_WHITELIST = [ self.location.hostname, 'fonts.gstatic.com', - 'fonts.googleapis.com' + 'fonts.googleapis.com', + 'unpkg.com' ] // The Util Function to hack URLs of intercepted requests const getFixedUrl = (req) => { - var now = Date.now(); - url = new URL(req.url) + var now = Date.now() + var url = new URL(req.url) // 1. fixed http URL // Just keep syncing with location.protocol @@ -40,7 +41,9 @@ const getFixedUrl = (req) => { // max-age on mutable content is error-prone, with SW life of bugs can even extend. // Until cache mode of Fetch API landed, we have to workaround cache-busting with query string. // Cache-Control-Bug: https://bugs.chromium.org/p/chromium/issues/detail?id=453190 - url.search += (url.search ? '&' : '?') + 'cache-bust=' + now; + if (url.hostname === self.location.hostname) { + url.search += (url.search ? '&' : '?') + 'cache-bust=' + now + } return url.href } @@ -50,16 +53,15 @@ const getFixedUrl = (req) => { * * waitUntil(): activating ====> activated */ -self.addEventListener('activate', event => { - event.waitUntil(self.clients.claim()); -}); - +self.addEventListener('activate', event => { + event.waitUntil(self.clients.claim()) +}) /** * @Functional Fetch * All network requests are being intercepted here. * - * void respondWith(Promise r); + * void respondWith(Promise r) */ self.addEventListener('fetch', event => { // Skip some of cross-origin requests, like those for Google Analytics. @@ -67,10 +69,10 @@ self.addEventListener('fetch', event => { // Stale-while-revalidate // similar to HTTP's stale-while-revalidate: https://www.mnot.net/blog/2007/12/12/stale // Upgrade from Jake's to Surma's: https://gist.github.com/surma/eb441223daaedf880801ad80006389f1 - const cached = caches.match(event.request); - const fixedUrl = getFixedUrl(event.request); - const fetched = fetch(fixedUrl, {cache: 'no-store'}); - const fetchedCopy = fetched.then(resp => resp.clone()); + const cached = caches.match(event.request) + const fixedUrl = getFixedUrl(event.request) + const fetched = fetch(fixedUrl, { cache: 'no-store' }) + const fetchedCopy = fetched.then(resp => resp.clone()) // Call respondWith() with whatever we get first. // If the fetch fails (e.g disconnected), wait for the cache. @@ -79,17 +81,17 @@ self.addEventListener('fetch', event => { event.respondWith( Promise.race([fetched.catch(_ => cached), cached]) .then(resp => resp || fetched) - .catch(_ => {/* eat any errors */}) - ); + .catch(_ => { /* eat any errors */ }) + ) // Update the cache with the version we fetched (only for ok status) event.waitUntil( Promise.all([fetchedCopy, caches.open(RUNTIME)]) .then(([response, cache]) => response.ok && cache.put(event.request, response)) - .catch(_ => {/* eat any errors */}) - ); + .catch(_ => { /* eat any errors */ }) + ) } -}); +}) ``` ## 注册