|
1 | 1 | /* global importScripts, workbox, idbKeyval */
|
2 | 2 |
|
3 | 3 | importScripts(`idb-keyval-iife.min.js`)
|
4 |
| -const WHITELIST_KEY = `custom-navigation-whitelist` |
5 | 4 |
|
6 |
| -const navigationRoute = new workbox.routing.NavigationRoute(({ event }) => { |
7 |
| - const { pathname } = new URL(event.request.url) |
| 5 | +const { NavigationRoute } = workbox.routing |
8 | 6 |
|
9 |
| - return idbKeyval.get(WHITELIST_KEY).then((customWhitelist = []) => { |
10 |
| - // Respond with the offline shell if we match the custom whitelist |
11 |
| - if (customWhitelist.includes(pathname)) { |
12 |
| - const offlineShell = `%pathPrefix%/offline-plugin-app-shell-fallback/index.html` |
13 |
| - const cacheName = workbox.core.cacheNames.precache |
| 7 | +const navigationRoute = new NavigationRoute(async ({ event }) => { |
| 8 | + let { pathname } = new URL(event.request.url) |
| 9 | + pathname = pathname.replace(new RegExp(`^%pathPrefix%`), ``) |
14 | 10 |
|
15 |
| - return caches.match(offlineShell, { cacheName }).then(cachedResponse => { |
16 |
| - if (cachedResponse) return cachedResponse |
17 |
| - |
18 |
| - console.error( |
19 |
| - `The offline shell (${offlineShell}) was not found ` + |
20 |
| - `while attempting to serve a response for ${pathname}` |
21 |
| - ) |
| 11 | + // Check for resources + the app bundle |
| 12 | + // The latter may not exist if the SW is updating to a new version |
| 13 | + const resources = await idbKeyval.get(`resources:${pathname}`) |
| 14 | + if (!resources || !(await caches.match(`%pathPrefix%/%appFile%`))) { |
| 15 | + return await fetch(event.request) |
| 16 | + } |
22 | 17 |
|
23 |
| - return fetch(offlineShell).then(response => { |
24 |
| - if (response.ok) { |
25 |
| - return caches.open(cacheName).then(cache => |
26 |
| - // Clone is needed because put() consumes the response body. |
27 |
| - cache.put(offlineShell, response.clone()).then(() => response) |
28 |
| - ) |
29 |
| - } else { |
30 |
| - return fetch(event.request) |
31 |
| - } |
32 |
| - }) |
33 |
| - }) |
| 18 | + for (const resource of resources) { |
| 19 | + // As soon as we detect a failed resource, fetch the entire page from |
| 20 | + // network - that way we won't risk being in an inconsistent state with |
| 21 | + // some parts of the page failing. |
| 22 | + if (!(await caches.match(resource))) { |
| 23 | + return await fetch(event.request) |
34 | 24 | }
|
| 25 | + } |
35 | 26 |
|
36 |
| - return fetch(event.request) |
37 |
| - }) |
| 27 | + const offlineShell = `%pathPrefix%/offline-plugin-app-shell-fallback/index.html` |
| 28 | + return await caches.match(offlineShell) |
38 | 29 | })
|
39 | 30 |
|
40 | 31 | workbox.routing.registerRoute(navigationRoute)
|
41 | 32 |
|
42 |
| -let updatingWhitelist = null |
43 |
| - |
44 |
| -function rawWhitelistPathnames(pathnames) { |
45 |
| - if (updatingWhitelist !== null) { |
46 |
| - // Prevent the whitelist from being updated twice at the same time |
47 |
| - return updatingWhitelist.then(() => rawWhitelistPathnames(pathnames)) |
48 |
| - } |
49 |
| - |
50 |
| - updatingWhitelist = idbKeyval |
51 |
| - .get(WHITELIST_KEY) |
52 |
| - .then((customWhitelist = []) => { |
53 |
| - pathnames.forEach(pathname => { |
54 |
| - if (!customWhitelist.includes(pathname)) customWhitelist.push(pathname) |
55 |
| - }) |
56 |
| - |
57 |
| - return idbKeyval.set(WHITELIST_KEY, customWhitelist) |
58 |
| - }) |
59 |
| - .then(() => { |
60 |
| - updatingWhitelist = null |
61 |
| - }) |
62 |
| - |
63 |
| - return updatingWhitelist |
64 |
| -} |
65 |
| - |
66 |
| -function rawResetWhitelist() { |
67 |
| - if (updatingWhitelist !== null) { |
68 |
| - return updatingWhitelist.then(() => rawResetWhitelist()) |
69 |
| - } |
70 |
| - |
71 |
| - updatingWhitelist = idbKeyval.set(WHITELIST_KEY, []).then(() => { |
72 |
| - updatingWhitelist = null |
73 |
| - }) |
74 |
| - |
75 |
| - return updatingWhitelist |
76 |
| -} |
77 |
| - |
78 | 33 | const messageApi = {
|
79 |
| - whitelistPathnames(event) { |
80 |
| - let { pathnames } = event.data |
81 |
| - |
82 |
| - pathnames = pathnames.map(({ pathname, includesPrefix }) => { |
83 |
| - if (!includesPrefix) { |
84 |
| - return `%pathPrefix%${pathname}` |
85 |
| - } else { |
86 |
| - return pathname |
87 |
| - } |
88 |
| - }) |
89 |
| - |
90 |
| - event.waitUntil(rawWhitelistPathnames(pathnames)) |
| 34 | + setPathResources(event, { path, resources }) { |
| 35 | + event.waitUntil(idbKeyval.set(`resources:${path}`, resources)) |
91 | 36 | },
|
92 | 37 |
|
93 |
| - resetWhitelist(event) { |
94 |
| - event.waitUntil(rawResetWhitelist()) |
| 38 | + clearPathResources(event) { |
| 39 | + event.waitUntil(idbKeyval.clear()) |
95 | 40 | },
|
96 | 41 | }
|
97 | 42 |
|
98 | 43 | self.addEventListener(`message`, event => {
|
99 | 44 | const { gatsbyApi } = event.data
|
100 |
| - if (gatsbyApi) messageApi[gatsbyApi](event) |
| 45 | + if (gatsbyApi) messageApi[gatsbyApi](event, event.data) |
101 | 46 | })
|
0 commit comments