/* global importScripts, workbox, idbKeyval */ importScripts(`idb-keyval-iife.min.js`) const { NavigationRoute } = workbox.routing const navigationRoute = new NavigationRoute(async ({ event }) => { let { pathname } = new URL(event.request.url) pathname = pathname.replace(new RegExp(`^%pathPrefix%`), ``) // Check for resources + the app bundle // The latter may not exist if the SW is updating to a new version const resources = await idbKeyval.get(`resources:${pathname}`) if (!resources || !(await caches.match(`%pathPrefix%/%appFile%`))) { return await fetch(event.request) } for (const resource of resources) { // As soon as we detect a failed resource, fetch the entire page from // network - that way we won't risk being in an inconsistent state with // some parts of the page failing. if (!(await caches.match(resource))) { return await fetch(event.request) } } const offlineShell = `%pathPrefix%/offline-plugin-app-shell-fallback/index.html` return await caches.match(offlineShell) }) workbox.routing.registerRoute(navigationRoute) const messageApi = { setPathResources(event, { path, resources }) { event.waitUntil(idbKeyval.set(`resources:${path}`, resources)) }, clearPathResources(event) { event.waitUntil(idbKeyval.clear()) }, } self.addEventListener(`message`, event => { const { gatsbyApi } = event.data if (gatsbyApi) messageApi[gatsbyApi](event, event.data) })