12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- import { timestamp, files, shell, routes } from '@sapper/service-worker';
- const ASSETS = `cache${timestamp}`;
- const to_cache = shell.concat(files);
- const cached = new Set(to_cache);
- self.addEventListener('install', event => {
- event.waitUntil(
- caches
- .open(ASSETS)
- .then(cache => cache.addAll(to_cache))
- .then(() => {
- self.skipWaiting();
- })
- );
- });
- self.addEventListener('activate', event => {
- event.waitUntil(
- caches.keys().then(async keys => {
-
- for (const key of keys) {
- if (key !== ASSETS) await caches.delete(key);
- }
- self.clients.claim();
- })
- );
- });
- self.addEventListener('fetch', event => {
- if (event.request.method !== 'GET' || event.request.headers.has('range')) return;
- const url = new URL(event.request.url);
-
- if (!url.protocol.startsWith('http')) return;
-
- if (url.hostname === self.location.hostname && url.port !== self.location.port) return;
-
- if (url.host === self.location.host && cached.has(url.pathname)) {
- event.respondWith(caches.match(event.request));
- return;
- }
-
-
-
-
- if (event.request.cache === 'only-if-cached') return;
-
-
-
- event.respondWith(
- caches
- .open(`offline${timestamp}`)
- .then(async cache => {
- try {
- const response = await fetch(event.request);
- cache.put(event.request, response.clone());
- return response;
- } catch(err) {
- const response = await cache.match(event.request);
- if (response) return response;
- throw err;
- }
- })
- );
- });
|