mirror of https://gitee.com/antv-l7/antv-l7
169 lines
4.9 KiB
JavaScript
169 lines
4.9 KiB
JavaScript
/**
|
|
* Welcome to your Workbox-powered service worker!
|
|
*
|
|
* You'll need to register this file in your web app and you should
|
|
* disable HTTP caching for this file too.
|
|
* See https://goo.gl/nhQhGp
|
|
*
|
|
* The rest of the code is auto-generated. Please don't update this file
|
|
* directly; instead, make changes to your Workbox build configuration
|
|
* and re-run your build process.
|
|
* See https://goo.gl/2aRDsh
|
|
*/
|
|
|
|
importScripts("workbox-v4.3.1/workbox-sw.js");
|
|
workbox.setConfig({modulePathPrefix: "workbox-v4.3.1"});
|
|
|
|
workbox.core.setCacheNameDetails({prefix: "gatsby-plugin-offline"});
|
|
|
|
workbox.core.skipWaiting();
|
|
|
|
workbox.core.clientsClaim();
|
|
|
|
/**
|
|
* The workboxSW.precacheAndRoute() method efficiently caches and responds to
|
|
* requests for URLs in the manifest.
|
|
* See https://goo.gl/S9QRab
|
|
*/
|
|
self.__precacheManifest = [
|
|
{
|
|
"url": "webpack-runtime-cd36d30ee5f631fd448a.js"
|
|
},
|
|
{
|
|
"url": "styles.beb594d2a40c64f7dcca.css"
|
|
},
|
|
{
|
|
"url": "styles-0464e72332e767bb5fff.js"
|
|
},
|
|
{
|
|
"url": "commons-fdabd6bc01c519648e38.js"
|
|
},
|
|
{
|
|
"url": "component---node-modules-gatsby-plugin-offline-app-shell-js-d3db59a0d258d9d07c90.js"
|
|
},
|
|
{
|
|
"url": "offline-plugin-app-shell-fallback/index.html",
|
|
"revision": "050718cc6822715fc4edfecfda91aa72"
|
|
},
|
|
{
|
|
"url": "page-data/offline-plugin-app-shell-fallback/page-data.json",
|
|
"revision": "5636d5ab217f85530a92b2939e270518"
|
|
},
|
|
{
|
|
"url": "manifest.webmanifest",
|
|
"revision": "123daa3226c4e9bd715f027dfb4e03ab"
|
|
}
|
|
].concat(self.__precacheManifest || []);
|
|
workbox.precaching.precacheAndRoute(self.__precacheManifest, {});
|
|
|
|
workbox.routing.registerRoute(/(\.js$|\.css$|static\/)/, new workbox.strategies.CacheFirst(), 'GET');
|
|
workbox.routing.registerRoute(/^https?:.*\page-data\/.*\/page-data\.json/, new workbox.strategies.StaleWhileRevalidate(), 'GET');
|
|
workbox.routing.registerRoute(/^https?:.*\.(png|jpg|jpeg|webp|svg|gif|tiff|js|woff|woff2|json|css)$/, new workbox.strategies.StaleWhileRevalidate(), 'GET');
|
|
workbox.routing.registerRoute(/^https?:\/\/fonts\.googleapis\.com\/css/, new workbox.strategies.StaleWhileRevalidate(), 'GET');
|
|
|
|
/* global importScripts, workbox, idbKeyval */
|
|
|
|
importScripts(`idb-keyval-iife.min.js`)
|
|
|
|
const { NavigationRoute } = workbox.routing
|
|
|
|
let lastNavigationRequest = null
|
|
let offlineShellEnabled = true
|
|
|
|
// prefer standard object syntax to support more browsers
|
|
const MessageAPI = {
|
|
setPathResources: (event, { path, resources }) => {
|
|
event.waitUntil(idbKeyval.set(`resources:${path}`, resources))
|
|
},
|
|
|
|
clearPathResources: event => {
|
|
event.waitUntil(idbKeyval.clear())
|
|
},
|
|
|
|
enableOfflineShell: () => {
|
|
offlineShellEnabled = true
|
|
},
|
|
|
|
disableOfflineShell: () => {
|
|
offlineShellEnabled = false
|
|
},
|
|
}
|
|
|
|
self.addEventListener(`message`, event => {
|
|
const { gatsbyApi: api } = event.data
|
|
if (api) MessageAPI[api](event, event.data)
|
|
})
|
|
|
|
function handleAPIRequest({ event }) {
|
|
const { pathname } = new URL(event.request.url)
|
|
|
|
const params = pathname.match(/:(.+)/)[1]
|
|
const data = {}
|
|
|
|
if (params.indexOf(`=`) !== -1) {
|
|
params.split(`&`).forEach(param => {
|
|
const [key, val] = param.split(`=`)
|
|
data[key] = val
|
|
})
|
|
} else {
|
|
data.api = params
|
|
}
|
|
|
|
if (MessageAPI[data.api] !== undefined) {
|
|
MessageAPI[data.api]()
|
|
}
|
|
|
|
if (!data.redirect) {
|
|
return new Response()
|
|
}
|
|
|
|
return new Response(null, {
|
|
status: 302,
|
|
headers: {
|
|
Location: lastNavigationRequest,
|
|
},
|
|
})
|
|
}
|
|
|
|
const navigationRoute = new NavigationRoute(async ({ event }) => {
|
|
// handle API requests separately to normal navigation requests, so do this
|
|
// check first
|
|
if (event.request.url.match(/\/.gatsby-plugin-offline:.+/)) {
|
|
return handleAPIRequest({ event })
|
|
}
|
|
|
|
if (!offlineShellEnabled) {
|
|
return await fetch(event.request)
|
|
}
|
|
|
|
lastNavigationRequest = event.request.url
|
|
|
|
let { pathname } = new URL(event.request.url)
|
|
pathname = pathname.replace(new RegExp(`^`), ``)
|
|
|
|
// 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(`/app-d463ca8c198cfb487798.js`))) {
|
|
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 = `/offline-plugin-app-shell-fallback/index.html`
|
|
const offlineShellWithKey = workbox.precaching.getCacheKeyForURL(offlineShell)
|
|
return await caches.match(offlineShellWithKey)
|
|
})
|
|
|
|
workbox.routing.registerRoute(navigationRoute)
|
|
|
|
// this route is used when performing a non-navigation request (e.g. fetch)
|
|
workbox.routing.registerRoute(/\/.gatsby-plugin-offline:.+/, handleAPIRequest)
|