Skip to content

Commit f7111b5

Browse files
committed
feat(pwa): add sw.js
1 parent 42b2dba commit f7111b5

File tree

3 files changed

+86
-3
lines changed

3 files changed

+86
-3
lines changed

docs/index.html

+4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
'/changelog': 'https://raw.githubusercontent.com/QingWei-Li/docsify/master/CHANGELOG'
3333
}
3434
}
35+
36+
if (typeof navigator.serviceWorker !== 'undefined') {
37+
navigator.serviceWorker.register('sw.js')
38+
}
3539
</script>
3640
<script src="//unpkg.com/docsify/lib/docsify.min.js"></script>
3741
<script src="//unpkg.com/docsify/lib/plugins/search.min.js"></script>

docs/sw.js

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/* ===========================================================
2+
* docsify sw.js
3+
* ===========================================================
4+
* Copyright 2016 @huxpro
5+
* Licensed under Apache 2.0
6+
* Register service worker.
7+
* ========================================================== */
8+
9+
const RUNTIME = 'docsify';
10+
const HOSTNAME_WHITELIST = [
11+
self.location.hostname,
12+
'unpkg.com',
13+
'fonts.gstatic.com',
14+
'fonts.googleapis.com'
15+
]
16+
17+
// The Util Function to hack URLs of intercepted requests
18+
const getFixedUrl = (req) => {
19+
var now = Date.now();
20+
url = new URL(req.url)
21+
22+
// 1. fixed http URL
23+
// Just keep syncing with location.protocol
24+
// fetch(httpURL) belongs to active mixed content.
25+
// And fetch(httpRequest) is not supported yet.
26+
url.protocol = self.location.protocol
27+
28+
// 2. add query for caching-busting.
29+
// Github Pages served with Cache-Control: max-age=600
30+
// max-age on mutable content is error-prone, with SW life of bugs can even extend.
31+
// Until cache mode of Fetch API landed, we have to workaround cache-busting with query string.
32+
// Cache-Control-Bug: https://bugs.chromium.org/p/chromium/issues/detail?id=453190
33+
url.search += (url.search ? '&' : '?') + 'cache-bust=' + now;
34+
return url.href
35+
}
36+
37+
/**
38+
* @Lifecycle Activate
39+
* New one activated when old isnt being used.
40+
*
41+
* waitUntil(): activating ====> activated
42+
*/
43+
self.addEventListener('activate', event => {
44+
event.waitUntil(self.clients.claim());
45+
});
46+
47+
48+
/**
49+
* @Functional Fetch
50+
* All network requests are being intercepted here.
51+
*
52+
* void respondWith(Promise<Response> r);
53+
*/
54+
self.addEventListener('fetch', event => {
55+
// Skip some of cross-origin requests, like those for Google Analytics.
56+
if (HOSTNAME_WHITELIST.indexOf(new URL(event.request.url).hostname) > -1) {
57+
// Stale-while-revalidate
58+
// similar to HTTP's stale-while-revalidate: https://www.mnot.net/blog/2007/12/12/stale
59+
// Upgrade from Jake's to Surma's: https://gist.github.com/surma/eb441223daaedf880801ad80006389f1
60+
const cached = caches.match(event.request);
61+
const fixedUrl = getFixedUrl(event.request);
62+
const fetched = fetch(fixedUrl, {cache: 'no-store'});
63+
const fetchedCopy = fetched.then(resp => resp.clone());
64+
65+
// Call respondWith() with whatever we get first.
66+
// If the fetch fails (e.g disconnected), wait for the cache.
67+
// If there’s nothing in cache, wait for the fetch.
68+
// If neither yields a response, return offline pages.
69+
event.respondWith(
70+
Promise.race([fetched.catch(_ => cached), cached])
71+
.then(resp => resp || fetched)
72+
.catch(_ => {/* eat any errors */})
73+
);
74+
75+
// Update the cache with the version we fetched (only for ok status)
76+
event.waitUntil(
77+
Promise.all([fetchedCopy, caches.open(RUNTIME)])
78+
.then(([response, cache]) => response.ok && cache.put(event.request, response))
79+
.catch(_ => {/* eat any errors */})
80+
);
81+
}
82+
});

src/core/fetch/ajax.js

-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import progressbar from '../render/progressbar'
22
import { noop } from '../util/core'
33

44
const cache = {}
5-
const RUN_VERSION = Date.now()
65

76
/**
87
* Simple ajax get
@@ -16,8 +15,6 @@ export function get (url, hasBar = false) {
1615
xhr.addEventListener.apply(xhr, arguments)
1716
}
1817

19-
url += (/\?(\w+)=/g.test(url) ? '&' : '?') + `v=${RUN_VERSION}`
20-
2118
if (cache[url]) {
2219
return { then: cb => cb(cache[url]), abort: noop }
2320
}

0 commit comments

Comments
 (0)