pinafore/templates/service-worker.js

125 lines
3.2 KiB
JavaScript
Raw Normal View History

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-01-14 23:54:26 +01:00
const to_cache = __shell__.concat(__assets__)
const cached = new Set(to_cache)
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-01-14 23:54:26 +01:00
event.waitUntil(
caches
.open(ASSETS)
.then(cache => cache.addAll(to_cache))
.then(() => {
self.skipWaiting()
})
)
})
2018-01-07 00:51:25 +01:00
self.addEventListener('activate', event => {
2018-01-14 23:54:26 +01:00
event.waitUntil(
caches.keys().then(async keys => {
// delete old caches
for (const key of keys) {
if (key !== ASSETS) {
await caches.delete(key)
}
}
2018-01-07 00:51:25 +01:00
2018-01-14 23:54:26 +01:00
await self.clients.claim()
})
)
})
2018-01-07 00:51:25 +01:00
2018-01-15 02:13:42 +01:00
const NETWORK_ONLY = [
2018-01-19 05:57:15 +01:00
'/oauth',
'/api/v1/timelines'
2018-01-15 02:13:42 +01:00
]
const CACHE_FIRST = [
'/api/v1/accounts/verify_credentials',
'/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')) {
return
}
2018-01-07 00:51:25 +01:00
2018-01-14 23:54:26 +01:00
// always serve assets and webpack-generated files from cache
if (cached.has(url.pathname)) {
2018-01-15 02:13:42 +01:00
event.respondWith(caches.match(req))
2018-01-14 23:54:26 +01:00
return
}
2018-01-07 00:51:25 +01:00
2018-01-14 23:54:26 +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-01-14 23:54:26 +01:00
if (url.origin === self.origin && routes.find(route => route.pattern.test(url.pathname))) {
2018-01-15 02:13:42 +01:00
event.respondWith(caches.match('/index.html'))
return
2018-01-14 23:54:26 +01:00
}
2018-01-07 00:51:25 +01:00
2018-01-15 02:13:42 +01:00
// Non-GET and for certain endpoints (e.g. OAuth), go network-only
if (req.method !== 'GET' ||
NETWORK_ONLY.some(pattern => url.pathname.startsWith(pattern))) {
console.log('Using network-only for', url.href)
event.respondWith(fetch(req))
return
}
2018-01-14 23:54:26 +01:00
2018-01-15 02:13:42 +01:00
// For these, go cache-first.
if (CACHE_FIRST.some(pattern => url.pathname.startsWith(pattern))) {
console.log('Using cache-first for', url.href)
event.respondWith(caches
2018-01-14 23:54:26 +01:00
.open(`offline${timestamp}`)
.then(async cache => {
2018-01-15 02:13:42 +01:00
let response = await cache.match(req)
if (response) {
// update asynchronously
fetch(req).then(response => {
cache.put(req, response.clone())
})
2018-01-14 23:54:26 +01:00
return response
2018-01-15 02:13:42 +01:00
}
response = await fetch(req)
cache.put(req, response.clone())
return response
}))
return
}
2018-01-14 23:54:26 +01:00
2018-01-15 02:13:42 +01:00
// for everything else, try the network first, falling back to
// cache if the user is offline. (If the pages never change, you
// might prefer a cache-first approach to a network-first one.)
event.respondWith(caches
.open(`offline${timestamp}`)
.then(async cache => {
try {
console.log('Using network-first for', url.href)
const response = await fetch(req)
cache.put(req, response.clone())
return response
} catch (err) {
const response = await cache.match(req)
if (response) {
return response
2018-01-14 23:54:26 +01:00
}
2018-01-15 02:13:42 +01:00
throw err
}
})
2018-01-14 23:54:26 +01:00
)
})