I hate service workers for not reloading things.

master
Zed A. Shaw 4 years ago
parent 8f8db6555d
commit 1d3e9cefb7
  1. 112
      src/service-worker.js

@ -8,75 +8,75 @@ const to_cache = shell.concat(files);
const cached = new Set(to_cache); const cached = new Set(to_cache);
self.addEventListener('install', event => { self.addEventListener('install', event => {
event.waitUntil( event.waitUntil(
caches caches
.open(ASSETS) .open(ASSETS)
.then(cache => cache.addAll(to_cache)) .then(cache => cache.addAll(to_cache))
.then(() => { .then(() => {
self.skipWaiting(); self.skipWaiting();
}) })
); );
}); });
self.addEventListener('activate', event => { self.addEventListener('activate', event => {
event.waitUntil( event.waitUntil(
caches.keys().then(async keys => { caches.keys().then(async keys => {
// delete old caches // delete old caches
for (const key of keys) { for (const key of keys) {
if (key !== ASSETS) await caches.delete(key); if (key !== ASSETS) await caches.delete(key);
} }
self.clients.claim(); self.clients.claim();
}) })
); );
}); });
self.addEventListener('fetch', event => { self.addEventListener('fetch', event => {
if (event.request.method !== 'GET' || event.request.headers.has('range')) return; if (event.request.method !== 'GET' || event.request.headers.has('range')) return;
const url = new URL(event.request.url); const url = new URL(event.request.url);
// don't try to handle e.g. data: URIs // don't try to handle e.g. data: URIs
if (!url.protocol.startsWith('http')) return; if (!url.protocol.startsWith('http')) return;
// ignore dev server requests // ignore dev server requests
if (url.hostname === self.location.hostname && url.port !== self.location.port) return; if (url.hostname === self.location.hostname && url.port !== self.location.port) return;
// always serve static files and bundler-generated assets from cache // always serve static files and bundler-generated assets from cache
if (url.host === self.location.host && cached.has(url.pathname)) { if (url.host === self.location.host && cached.has(url.pathname)) {
event.respondWith(caches.match(event.request)); event.respondWith(caches.match(event.request));
return; return;
} }
// for pages, you might want to serve a shell `service-worker-index.html` file, // for pages, you might want to serve a shell `service-worker-index.html` file,
// which Sapper has generated for you. It's not right for every // which Sapper has generated for you. It's not right for every
// app, but if it's right for yours then uncomment this section // app, but if it's right for yours then uncomment this section
/* /*
if (url.origin === self.origin && routes.find(route => route.pattern.test(url.pathname))) { if (url.origin === self.origin && routes.find(route => route.pattern.test(url.pathname))) {
event.respondWith(caches.match('/service-worker-index.html')); event.respondWith(caches.match('/service-worker-index.html'));
return; return;
} }
*/ */
if (event.request.cache === 'only-if-cached') return; if (event.request.cache === 'only-if-cached') return;
// for everything else, try the network first, falling back to // for everything else, try the network first, falling back to
// cache if the user is offline. (If the pages never change, you // cache if the user is offline. (If the pages never change, you
// might prefer a cache-first approach to a network-first one.) // might prefer a cache-first approach to a network-first one.)
event.respondWith( event.respondWith(
caches caches
.open(`offline${timestamp}`) .open(`offline${timestamp}`)
.then(async cache => { .then(async cache => {
try { try {
const response = await fetch(event.request); const response = await fetch(event.request, {credentials: 'same-origin'});
cache.put(event.request, response.clone()); // cache.put(event.request, response.clone());
return response; return response;
} catch(err) { } catch(err) {
const response = await cache.match(event.request); const response = await cache.match(event.request);
if (response) return response; if (response) return response;
throw err; throw err;
} }
}) })
); );
}); });

Loading…
Cancel
Save