2018-01-14 23:54:26 +01:00
|
|
|
const timestamp = '__timestamp__'
|
2018-03-26 06:31:40 +02:00
|
|
|
const ASSETS = `assets_${timestamp}`
|
2018-04-01 02:26:10 +02:00
|
|
|
const WEBPACK_ASSETS = `webpack_assets_${timestamp}`
|
2018-01-07 00:51:25 +01:00
|
|
|
|
|
|
|
// `assets` is an array of everything in the `assets` directory
|
2018-03-26 06:31:40 +02:00
|
|
|
const assets = __assets__
|
|
|
|
.map(file => file.startsWith('/') ? file : `/${file}`)
|
2018-03-20 04:24:33 +01:00
|
|
|
.filter(filename => !filename.startsWith('/apple-icon'))
|
2018-04-01 02:26:10 +02:00
|
|
|
.concat(['/index.html'])
|
2018-03-26 06:31:40 +02:00
|
|
|
|
|
|
|
// `shell` is an array of all the files generated by webpack
|
|
|
|
// also contains '/index.html' for some reason
|
2018-04-01 02:26:10 +02:00
|
|
|
const webpackAssets = __shell__
|
2018-03-26 06:31:40 +02:00
|
|
|
.filter(filename => !filename.endsWith('.map'))
|
2018-04-01 02:26:10 +02:00
|
|
|
.filter(filename => filename !== '/index.html')
|
2018-03-26 06:31:40 +02:00
|
|
|
|
2018-01-07 00:51:25 +01:00
|
|
|
// `routes` is an array of `{ pattern: RegExp }` objects that
|
|
|
|
// match the pages in your app
|
2018-01-14 23:54:26 +01:00
|
|
|
const routes = __routes__
|
2018-01-07 00:51:25 +01:00
|
|
|
|
|
|
|
self.addEventListener('install', event => {
|
2018-03-23 06:30:48 +01:00
|
|
|
event.waitUntil((async () => {
|
2018-04-01 02:26:10 +02:00
|
|
|
await Promise.all([
|
|
|
|
caches.open(WEBPACK_ASSETS).then(cache => cache.addAll(webpackAssets)),
|
|
|
|
caches.open(ASSETS).then(cache => cache.addAll(assets))
|
|
|
|
])
|
2018-03-15 08:03:22 +01:00
|
|
|
self.skipWaiting()
|
|
|
|
})())
|
2018-01-14 23:54:26 +01:00
|
|
|
})
|
2018-01-07 00:51:25 +01:00
|
|
|
|
|
|
|
self.addEventListener('activate', event => {
|
2018-03-23 06:30:48 +01:00
|
|
|
event.waitUntil((async () => {
|
2018-03-15 08:03:22 +01:00
|
|
|
let keys = await caches.keys()
|
2018-03-26 06:31:40 +02:00
|
|
|
|
|
|
|
// delete old asset/ondemand caches
|
2018-03-15 08:03:22 +01:00
|
|
|
for (let key of keys) {
|
2018-04-01 02:26:10 +02:00
|
|
|
if (key !== ASSETS &&
|
|
|
|
!key.startsWith('webpack_assets_')) {
|
2018-03-15 08:03:22 +01:00
|
|
|
await caches.delete(key)
|
2018-01-14 23:54:26 +01:00
|
|
|
}
|
2018-03-15 08:03:22 +01:00
|
|
|
}
|
2018-03-26 06:31:40 +02:00
|
|
|
|
2018-04-01 02:26:10 +02:00
|
|
|
// for webpack assets, keep the two latest builds because we may need
|
|
|
|
// them when the service worker has installed but the page has not
|
|
|
|
// yet reloaded (e.g. when it gives the toast saying "please reload"
|
|
|
|
// but then you don't refresh and instead load an async chunk)
|
|
|
|
let webpackKeysToDelete = keys
|
|
|
|
.filter(key => key.startsWith('webpack_assets_'))
|
|
|
|
.sort((a, b) => {
|
|
|
|
let aTimestamp = parseInt(a.substring(15), 10)
|
|
|
|
let bTimestamp = parseInt(b.substring(15), 10)
|
|
|
|
return bTimestamp < aTimestamp ? -1 : 1
|
|
|
|
})
|
|
|
|
.slice(2)
|
|
|
|
|
|
|
|
for (let key of webpackKeysToDelete) {
|
|
|
|
await caches.delete(key)
|
|
|
|
}
|
|
|
|
|
2018-03-15 08:03:22 +01:00
|
|
|
await self.clients.claim()
|
|
|
|
})())
|
2018-01-14 23:54:26 +01:00
|
|
|
})
|
2018-01-07 00:51:25 +01:00
|
|
|
|
|
|
|
self.addEventListener('fetch', event => {
|
2018-01-15 02:13:42 +01:00
|
|
|
const req = event.request
|
|
|
|
const url = new URL(req.url)
|
2018-01-07 00:51:25 +01:00
|
|
|
|
2018-01-14 23:54:26 +01:00
|
|
|
// don't try to handle e.g. data: URIs
|
|
|
|
if (!url.protocol.startsWith('http')) {
|
2018-02-09 07:29:29 +01:00
|
|
|
return
|
2018-01-14 23:54:26 +01:00
|
|
|
}
|
2018-01-07 00:51:25 +01:00
|
|
|
|
2018-03-23 06:30:48 +01:00
|
|
|
event.respondWith((async () => {
|
2018-03-26 06:31:40 +02:00
|
|
|
let sameOrigin = url.origin === self.origin
|
2018-01-07 00:51:25 +01:00
|
|
|
|
2018-04-05 07:52:04 +02:00
|
|
|
if (sameOrigin) {
|
|
|
|
// always serve webpack-generated resources and
|
|
|
|
// assets from the cache if possible
|
|
|
|
let response = await caches.match(req)
|
|
|
|
if (response) {
|
|
|
|
return response
|
|
|
|
}
|
|
|
|
// for routes, serve the /index.html file from the most recent
|
|
|
|
// assets cache
|
|
|
|
if (routes.find(route => route.pattern.test(url.pathname))) {
|
2018-08-24 17:51:09 +02:00
|
|
|
let response = await caches.match('/index.html')
|
|
|
|
if (response) {
|
|
|
|
return response
|
|
|
|
}
|
2018-04-05 07:52:04 +02:00
|
|
|
}
|
2018-03-15 08:03:22 +01:00
|
|
|
}
|
2018-01-07 00:51:25 +01:00
|
|
|
|
2018-03-15 08:03:22 +01:00
|
|
|
// for everything else, go network-only
|
|
|
|
return fetch(req)
|
|
|
|
})())
|
2018-01-14 23:54:26 +01:00
|
|
|
})
|