pinafore/templates/service-worker.js

94 lines
2.5 KiB
JavaScript
Raw Normal View History

2018-01-14 22:54:26 +00:00
const timestamp = '__timestamp__'
const ASSETS = `assets_${timestamp}`
const ON_DEMAND = `ondemand_${timestamp}`
2018-01-06 23:51:25 +00:00
// `assets` is an array of everything in the `assets` directory
const assets = __assets__
.map(file => file.startsWith('/') ? file : `/${file}`)
2018-03-20 03:24:33 +00:00
.filter(filename => !filename.startsWith('/apple-icon'))
// `shell` is an array of all the files generated by webpack
// also contains '/index.html' for some reason
const resources = __shell__
.filter(filename => !filename.endsWith('.map'))
const toCache = [].concat(assets).concat(resources)
const cacheSet = new Set(toCache)
2018-01-06 23:51:25 +00:00
// `routes` is an array of `{ pattern: RegExp }` objects that
// match the pages in your app
2018-01-14 22:54:26 +00:00
const routes = __routes__
2018-01-06 23:51:25 +00:00
self.addEventListener('install', event => {
2018-03-23 05:30:48 +00:00
event.waitUntil((async () => {
let cache = await caches.open(ASSETS)
await cache.addAll(toCache)
self.skipWaiting()
})())
2018-01-14 22:54:26 +00:00
})
2018-01-06 23:51:25 +00:00
self.addEventListener('activate', event => {
2018-03-23 05:30:48 +00:00
event.waitUntil((async () => {
let keys = await caches.keys()
// delete old asset/ondemand caches
for (let key of keys) {
if (key !== ASSETS && key !== ON_DEMAND) {
await caches.delete(key)
2018-01-14 22:54:26 +00:00
}
}
await self.clients.claim()
})())
2018-01-14 22:54:26 +00:00
})
2018-01-06 23:51:25 +00:00
const ON_DEMAND_PATHS = [
2018-01-15 01:13:42 +00:00
'/system/accounts/avatars'
]
2018-01-06 23:51:25 +00:00
self.addEventListener('fetch', event => {
2018-01-15 01:13:42 +00:00
const req = event.request
const url = new URL(req.url)
2018-01-06 23:51:25 +00:00
2018-01-14 22:54:26 +00:00
// don't try to handle e.g. data: URIs
if (!url.protocol.startsWith('http')) {
2018-02-09 06:29:29 +00:00
return
2018-01-14 22:54:26 +00:00
}
2018-01-06 23:51:25 +00:00
2018-03-23 05:30:48 +00:00
event.respondWith((async () => {
let sameOrigin = url.origin === self.origin
2018-01-06 23:51:25 +00:00
// always serve webpack-generated resources and
// assets from the cache
if (sameOrigin && cacheSet.has(url.pathname)) {
return caches.match(req)
}
2018-01-06 23:51:25 +00:00
// for routes, serve the /index.html file from the most recent
// assets cache
if (sameOrigin && routes.find(route => route.pattern.test(url.pathname))) {
return caches.match('/index.html')
}
2018-01-06 23:51:25 +00:00
// For these GET requests, go cache-first
if (req.method === 'GET' &&
ON_DEMAND_PATHS.some(pattern => url.pathname.startsWith(pattern))) {
let cache = await caches.open(ON_DEMAND)
let response = await cache.match(req)
if (response) {
// update asynchronously
fetch(req).then(response => {
cache.put(req, response.clone())
})
2018-01-15 01:13:42 +00:00
return response
}
response = await fetch(req)
cache.put(req, response.clone())
return response
}
2018-01-15 01:13:42 +00:00
// for everything else, go network-only
return fetch(req)
})())
2018-01-14 22:54:26 +00:00
})