docs(sw): cache unpkg.com

This commit is contained in:
qingwei.li 2017-02-22 09:39:17 +08:00
commit 083cc4af56
3 changed files with 63 additions and 57 deletions

View file

@ -17,17 +17,18 @@ Create a sw.js file in your documents root directory and copy this code.
* Register service worker. * Register service worker.
* ========================================================== */ * ========================================================== */
const RUNTIME = 'docsify'; const RUNTIME = 'docsify'
const HOSTNAME_WHITELIST = [ const HOSTNAME_WHITELIST = [
self.location.hostname, self.location.hostname,
'fonts.gstatic.com', 'fonts.gstatic.com',
'fonts.googleapis.com' 'fonts.googleapis.com',
'unpkg.com'
] ]
// The Util Function to hack URLs of intercepted requests // The Util Function to hack URLs of intercepted requests
const getFixedUrl = (req) => { const getFixedUrl = (req) => {
var now = Date.now(); var now = Date.now()
url = new URL(req.url) var url = new URL(req.url)
// 1. fixed http URL // 1. fixed http URL
// Just keep syncing with location.protocol // 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. // 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. // 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 // 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 return url.href
} }
@ -50,16 +53,15 @@ const getFixedUrl = (req) => {
* *
* waitUntil(): activating ====> activated * waitUntil(): activating ====> activated
*/ */
self.addEventListener('activate', event => { self.addEventListener('activate', event => {
event.waitUntil(self.clients.claim()); event.waitUntil(self.clients.claim())
}); })
/** /**
* @Functional Fetch * @Functional Fetch
* All network requests are being intercepted here. * All network requests are being intercepted here.
* *
* void respondWith(Promise<Response> r); * void respondWith(Promise<Response> r)
*/ */
self.addEventListener('fetch', event => { self.addEventListener('fetch', event => {
// Skip some of cross-origin requests, like those for Google Analytics. // Skip some of cross-origin requests, like those for Google Analytics.
@ -67,10 +69,10 @@ self.addEventListener('fetch', event => {
// Stale-while-revalidate // Stale-while-revalidate
// similar to HTTP's stale-while-revalidate: https://www.mnot.net/blog/2007/12/12/stale // 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 // Upgrade from Jake's to Surma's: https://gist.github.com/surma/eb441223daaedf880801ad80006389f1
const cached = caches.match(event.request); const cached = caches.match(event.request)
const fixedUrl = getFixedUrl(event.request); const fixedUrl = getFixedUrl(event.request)
const fetched = fetch(fixedUrl, {cache: 'no-store'}); const fetched = fetch(fixedUrl, { cache: 'no-store' })
const fetchedCopy = fetched.then(resp => resp.clone()); const fetchedCopy = fetched.then(resp => resp.clone())
// Call respondWith() with whatever we get first. // Call respondWith() with whatever we get first.
// If the fetch fails (e.g disconnected), wait for the cache. // If the fetch fails (e.g disconnected), wait for the cache.
@ -79,17 +81,17 @@ self.addEventListener('fetch', event => {
event.respondWith( event.respondWith(
Promise.race([fetched.catch(_ => cached), cached]) Promise.race([fetched.catch(_ => cached), cached])
.then(resp => resp || fetched) .then(resp => resp || fetched)
.catch(_ => {/* eat any errors */}) .catch(_ => { /* eat any errors */ })
); )
// Update the cache with the version we fetched (only for ok status) // Update the cache with the version we fetched (only for ok status)
event.waitUntil( event.waitUntil(
Promise.all([fetchedCopy, caches.open(RUNTIME)]) Promise.all([fetchedCopy, caches.open(RUNTIME)])
.then(([response, cache]) => response.ok && cache.put(event.request, response)) .then(([response, cache]) => response.ok && cache.put(event.request, response))
.catch(_ => {/* eat any errors */}) .catch(_ => { /* eat any errors */ })
); )
} }
}); })
``` ```
## Register ## Register

View file

@ -6,17 +6,18 @@
* Register service worker. * Register service worker.
* ========================================================== */ * ========================================================== */
const RUNTIME = 'docsify'; const RUNTIME = 'docsify'
const HOSTNAME_WHITELIST = [ const HOSTNAME_WHITELIST = [
self.location.hostname, self.location.hostname,
'fonts.gstatic.com', 'fonts.gstatic.com',
'fonts.googleapis.com' 'fonts.googleapis.com',
'unpkg.com'
] ]
// The Util Function to hack URLs of intercepted requests // The Util Function to hack URLs of intercepted requests
const getFixedUrl = (req) => { const getFixedUrl = (req) => {
var now = Date.now(); var now = Date.now()
url = new URL(req.url) var url = new URL(req.url)
// 1. fixed http URL // 1. fixed http URL
// Just keep syncing with location.protocol // 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. // 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. // 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 // 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 return url.href
} }
@ -39,16 +42,15 @@ const getFixedUrl = (req) => {
* *
* waitUntil(): activating ====> activated * waitUntil(): activating ====> activated
*/ */
self.addEventListener('activate', event => { self.addEventListener('activate', event => {
event.waitUntil(self.clients.claim()); event.waitUntil(self.clients.claim())
}); })
/** /**
* @Functional Fetch * @Functional Fetch
* All network requests are being intercepted here. * All network requests are being intercepted here.
* *
* void respondWith(Promise<Response> r); * void respondWith(Promise<Response> r)
*/ */
self.addEventListener('fetch', event => { self.addEventListener('fetch', event => {
// Skip some of cross-origin requests, like those for Google Analytics. // Skip some of cross-origin requests, like those for Google Analytics.
@ -56,10 +58,10 @@ self.addEventListener('fetch', event => {
// Stale-while-revalidate // Stale-while-revalidate
// similar to HTTP's stale-while-revalidate: https://www.mnot.net/blog/2007/12/12/stale // 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 // Upgrade from Jake's to Surma's: https://gist.github.com/surma/eb441223daaedf880801ad80006389f1
const cached = caches.match(event.request); const cached = caches.match(event.request)
const fixedUrl = getFixedUrl(event.request); const fixedUrl = getFixedUrl(event.request)
const fetched = fetch(fixedUrl, {cache: 'no-store'}); const fetched = fetch(fixedUrl, { cache: 'no-store' })
const fetchedCopy = fetched.then(resp => resp.clone()); const fetchedCopy = fetched.then(resp => resp.clone())
// Call respondWith() with whatever we get first. // Call respondWith() with whatever we get first.
// If the fetch fails (e.g disconnected), wait for the cache. // If the fetch fails (e.g disconnected), wait for the cache.
@ -68,14 +70,14 @@ self.addEventListener('fetch', event => {
event.respondWith( event.respondWith(
Promise.race([fetched.catch(_ => cached), cached]) Promise.race([fetched.catch(_ => cached), cached])
.then(resp => resp || fetched) .then(resp => resp || fetched)
.catch(_ => {/* eat any errors */}) .catch(_ => { /* eat any errors */ })
); )
// Update the cache with the version we fetched (only for ok status) // Update the cache with the version we fetched (only for ok status)
event.waitUntil( event.waitUntil(
Promise.all([fetchedCopy, caches.open(RUNTIME)]) Promise.all([fetchedCopy, caches.open(RUNTIME)])
.then(([response, cache]) => response.ok && cache.put(event.request, response)) .then(([response, cache]) => response.ok && cache.put(event.request, response))
.catch(_ => {/* eat any errors */}) .catch(_ => { /* eat any errors */ })
); )
} }
}); })

View file

@ -17,17 +17,18 @@
* Register service worker. * Register service worker.
* ========================================================== */ * ========================================================== */
const RUNTIME = 'docsify'; const RUNTIME = 'docsify'
const HOSTNAME_WHITELIST = [ const HOSTNAME_WHITELIST = [
self.location.hostname, self.location.hostname,
'fonts.gstatic.com', 'fonts.gstatic.com',
'fonts.googleapis.com' 'fonts.googleapis.com',
'unpkg.com'
] ]
// The Util Function to hack URLs of intercepted requests // The Util Function to hack URLs of intercepted requests
const getFixedUrl = (req) => { const getFixedUrl = (req) => {
var now = Date.now(); var now = Date.now()
url = new URL(req.url) var url = new URL(req.url)
// 1. fixed http URL // 1. fixed http URL
// Just keep syncing with location.protocol // 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. // 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. // 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 // 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 return url.href
} }
@ -50,16 +53,15 @@ const getFixedUrl = (req) => {
* *
* waitUntil(): activating ====> activated * waitUntil(): activating ====> activated
*/ */
self.addEventListener('activate', event => { self.addEventListener('activate', event => {
event.waitUntil(self.clients.claim()); event.waitUntil(self.clients.claim())
}); })
/** /**
* @Functional Fetch * @Functional Fetch
* All network requests are being intercepted here. * All network requests are being intercepted here.
* *
* void respondWith(Promise<Response> r); * void respondWith(Promise<Response> r)
*/ */
self.addEventListener('fetch', event => { self.addEventListener('fetch', event => {
// Skip some of cross-origin requests, like those for Google Analytics. // Skip some of cross-origin requests, like those for Google Analytics.
@ -67,10 +69,10 @@ self.addEventListener('fetch', event => {
// Stale-while-revalidate // Stale-while-revalidate
// similar to HTTP's stale-while-revalidate: https://www.mnot.net/blog/2007/12/12/stale // 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 // Upgrade from Jake's to Surma's: https://gist.github.com/surma/eb441223daaedf880801ad80006389f1
const cached = caches.match(event.request); const cached = caches.match(event.request)
const fixedUrl = getFixedUrl(event.request); const fixedUrl = getFixedUrl(event.request)
const fetched = fetch(fixedUrl, {cache: 'no-store'}); const fetched = fetch(fixedUrl, { cache: 'no-store' })
const fetchedCopy = fetched.then(resp => resp.clone()); const fetchedCopy = fetched.then(resp => resp.clone())
// Call respondWith() with whatever we get first. // Call respondWith() with whatever we get first.
// If the fetch fails (e.g disconnected), wait for the cache. // If the fetch fails (e.g disconnected), wait for the cache.
@ -79,17 +81,17 @@ self.addEventListener('fetch', event => {
event.respondWith( event.respondWith(
Promise.race([fetched.catch(_ => cached), cached]) Promise.race([fetched.catch(_ => cached), cached])
.then(resp => resp || fetched) .then(resp => resp || fetched)
.catch(_ => {/* eat any errors */}) .catch(_ => { /* eat any errors */ })
); )
// Update the cache with the version we fetched (only for ok status) // Update the cache with the version we fetched (only for ok status)
event.waitUntil( event.waitUntil(
Promise.all([fetchedCopy, caches.open(RUNTIME)]) Promise.all([fetchedCopy, caches.open(RUNTIME)])
.then(([response, cache]) => response.ok && cache.put(event.request, response)) .then(([response, cache]) => response.ok && cache.put(event.request, response))
.catch(_ => {/* eat any errors */}) .catch(_ => { /* eat any errors */ })
); )
} }
}); })
``` ```
## 注册 ## 注册