-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gatsby-plugin-offline): replace no-cache detection with dynamic …
…path whitelist (#9907) * Remove all no-cache code * Remove references to no-cache in offline plugin * Initial work on hybrid navigation handler * Refactor whitelist code to allow it to support onPostPrefetchPathname * Fix service worker detection * Fix IndexedDB race condition * Prevent race conditions + reset whitelist on SW update * Remove unnecessary API handler (onPostPrefetchPathname is called anyway) * Add debugging statements + fix some minor problems * Fix back/forward not working after 404 * Remove unneeded debugging statements * Bundle idb-keyval instead of using an external CDN * Update README * Backport fixes from #9907 * minor fixes for things I copy-pasted wrong * Refactor prefetching so we can detect success
- Loading branch information
Showing
12 changed files
with
260 additions
and
222 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,83 @@ | ||
// noop | ||
/* global importScripts, workbox, idbKeyval */ | ||
|
||
importScripts(`idb-keyval-iife.min.js`) | ||
const WHITELIST_KEY = `custom-navigation-whitelist` | ||
|
||
const navigationRoute = new workbox.routing.NavigationRoute(({ event }) => { | ||
const { pathname } = new URL(event.request.url) | ||
|
||
return idbKeyval.get(WHITELIST_KEY).then((customWhitelist = []) => { | ||
// Respond with the offline shell if we match the custom whitelist | ||
if (customWhitelist.includes(pathname)) { | ||
const offlineShell = `%pathPrefix%/offline-plugin-app-shell-fallback/index.html` | ||
const cacheName = workbox.core.cacheNames.precache | ||
|
||
return caches.match(offlineShell, { cacheName }) | ||
} | ||
|
||
return fetch(event.request) | ||
}) | ||
}) | ||
|
||
workbox.routing.registerRoute(navigationRoute) | ||
|
||
let updatingWhitelist = null | ||
|
||
function rawWhitelistPathnames(pathnames) { | ||
if (updatingWhitelist !== null) { | ||
// Prevent the whitelist from being updated twice at the same time | ||
return updatingWhitelist.then(() => rawWhitelistPathnames(pathnames)) | ||
} | ||
|
||
updatingWhitelist = idbKeyval | ||
.get(WHITELIST_KEY) | ||
.then((customWhitelist = []) => { | ||
pathnames.forEach(pathname => { | ||
if (!customWhitelist.includes(pathname)) customWhitelist.push(pathname) | ||
}) | ||
|
||
return idbKeyval.set(WHITELIST_KEY, customWhitelist) | ||
}) | ||
.then(() => { | ||
updatingWhitelist = null | ||
}) | ||
|
||
return updatingWhitelist | ||
} | ||
|
||
function rawResetWhitelist() { | ||
if (updatingWhitelist !== null) { | ||
return updatingWhitelist.then(() => rawResetWhitelist()) | ||
} | ||
|
||
updatingWhitelist = idbKeyval.set(WHITELIST_KEY, []).then(() => { | ||
updatingWhitelist = null | ||
}) | ||
|
||
return updatingWhitelist | ||
} | ||
|
||
const messageApi = { | ||
whitelistPathnames(event) { | ||
let { pathnames } = event.data | ||
|
||
pathnames = pathnames.map(({ pathname, includesPrefix }) => { | ||
if (!includesPrefix) { | ||
return `%pathPrefix%${pathname}` | ||
} else { | ||
return pathname | ||
} | ||
}) | ||
|
||
event.waitUntil(rawWhitelistPathnames(pathnames)) | ||
}, | ||
|
||
resetWhitelist(event) { | ||
event.waitUntil(rawResetWhitelist()) | ||
}, | ||
} | ||
|
||
self.addEventListener(`message`, event => { | ||
const { gatsbyApi } = event.data | ||
if (gatsbyApi) messageApi[gatsbyApi](event) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.