2018-01-14 23:54:26 +01:00
|
|
|
const timestamp = '__timestamp__'
|
|
|
|
const ASSETS = `cache${timestamp}`
|
2018-01-07 00:51:25 +01:00
|
|
|
|
|
|
|
// `shell` is an array of all the files generated by webpack,
|
|
|
|
// `assets` is an array of everything in the `assets` directory
|
2018-03-20 04:24:33 +01:00
|
|
|
const assets = __assets__.map(file => file.startsWith('/') ? file : `/${file}`)
|
|
|
|
const toCache = __shell__.concat(assets)
|
2018-03-17 06:12:04 +01:00
|
|
|
.filter(filename => !filename.endsWith('.map'))
|
2018-03-20 04:24:33 +01:00
|
|
|
.filter(filename => !filename.startsWith('/apple-icon'))
|
2018-02-09 07:29:29 +01:00
|
|
|
const cached = new Set(toCache)
|
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-03-15 08:03:22 +01:00
|
|
|
let cache = await caches.open(ASSETS)
|
|
|
|
await cache.addAll(toCache)
|
|
|
|
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()
|
|
|
|
// delete old caches
|
|
|
|
for (let key of keys) {
|
|
|
|
if (key !== ASSETS) {
|
|
|
|
await caches.delete(key)
|
2018-01-14 23:54:26 +01:00
|
|
|
}
|
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
|
|
|
|
2018-01-15 02:13:42 +01:00
|
|
|
const CACHE_FIRST = [
|
|
|
|
'/system/accounts/avatars'
|
|
|
|
]
|
|
|
|
|
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-15 08:03:22 +01:00
|
|
|
// always serve assets and webpack-generated files from cache
|
2018-03-20 04:24:33 +01:00
|
|
|
if (url.origin === self.origin && cached.has(url.pathname)) {
|
2018-03-23 06:30:48 +01:00
|
|
|
let cache = await caches.open(ASSETS)
|
|
|
|
return cache.match(req)
|
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 pages, you might want to serve a shell `index.html` file,
|
|
|
|
// which Sapper has generated for you. It's not right for every
|
|
|
|
// app, but if it's right for yours then uncomment this section
|
2018-01-07 00:51:25 +01:00
|
|
|
|
2018-03-15 08:03:22 +01:00
|
|
|
if (url.origin === self.origin &&
|
|
|
|
routes.find(route => route.pattern.test(url.pathname))) {
|
2018-03-23 06:30:48 +01:00
|
|
|
let cache = await caches.open(ASSETS)
|
|
|
|
return cache.match('/index.html')
|
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 these GET requests, go cache-first
|
|
|
|
if (req.method === 'GET' &&
|
|
|
|
CACHE_FIRST.some(pattern => url.pathname.startsWith(pattern))) {
|
|
|
|
let cache = await caches.open(`offline${timestamp}`)
|
|
|
|
let response = await cache.match(req)
|
|
|
|
if (response) {
|
|
|
|
// update asynchronously
|
|
|
|
fetch(req).then(response => {
|
|
|
|
cache.put(req, response.clone())
|
|
|
|
})
|
2018-01-15 02:13:42 +01:00
|
|
|
return response
|
2018-03-15 08:03:22 +01:00
|
|
|
}
|
|
|
|
response = await fetch(req)
|
|
|
|
cache.put(req, response.clone())
|
|
|
|
return response
|
|
|
|
}
|
2018-01-15 02:13:42 +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
|
|
|
})
|