development #27
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "scan-wave",
|
"name": "scan-wave",
|
||||||
"private": true,
|
"private": true,
|
||||||
"version": "0.0.1",
|
"version": "0.0.2",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "vite dev",
|
"dev": "vite dev",
|
||||||
|
|||||||
@@ -1,86 +1,71 @@
|
|||||||
|
/// <reference lib="webworker" />
|
||||||
/// <reference types="@sveltejs/kit" />
|
/// <reference types="@sveltejs/kit" />
|
||||||
import { build, files, version } from '$service-worker';
|
import { build, files, version } from '$service-worker';
|
||||||
|
|
||||||
// Create a unique cache name for this deployment
|
declare const self: ServiceWorkerGlobalScope;
|
||||||
const CACHE = `cache-${version}`;
|
|
||||||
|
|
||||||
|
const CACHE = `cache-${version}`;
|
||||||
const ASSETS = [
|
const ASSETS = [
|
||||||
...build, // the app itself
|
...build,
|
||||||
...files // everything in `static`
|
...files
|
||||||
];
|
];
|
||||||
|
|
||||||
self.addEventListener('install', (event) => {
|
self.addEventListener('install', (event: ExtendableEvent) => {
|
||||||
// Create a new cache and add all files to it
|
const addFilesToCache = async () => {
|
||||||
async function addFilesToCache() {
|
const cache = await caches.open(CACHE);
|
||||||
const cache = await caches.open(CACHE);
|
await cache.addAll(ASSETS);
|
||||||
await cache.addAll(ASSETS);
|
};
|
||||||
}
|
|
||||||
|
|
||||||
event.waitUntil(addFilesToCache());
|
console.log("[SW] Installing new service worker");
|
||||||
|
|
||||||
|
event.waitUntil(addFilesToCache());
|
||||||
|
self.skipWaiting();
|
||||||
});
|
});
|
||||||
|
|
||||||
self.addEventListener('activate', (event) => {
|
self.addEventListener('activate', (event: ExtendableEvent) => {
|
||||||
// Remove previous cached data from disk
|
const deleteOldCaches = async () => {
|
||||||
async function deleteOldCaches() {
|
for (const key of await caches.keys()) {
|
||||||
for (const key of await caches.keys()) {
|
if (key !== CACHE) await caches.delete(key);
|
||||||
if (key !== CACHE) await caches.delete(key);
|
console.log("[SW] Removing old service worker")
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
event.waitUntil(deleteOldCaches());
|
event.waitUntil(deleteOldCaches());
|
||||||
|
self.clients.claim();
|
||||||
});
|
});
|
||||||
|
|
||||||
self.addEventListener('fetch', (event) => {
|
self.addEventListener('fetch', (event: FetchEvent) => {
|
||||||
// ignore POST requests etc
|
if (event.request.method !== 'GET') return;
|
||||||
if (event.request.method !== 'GET') return;
|
|
||||||
|
|
||||||
async function respond() {
|
const url = new URL(event.request.url);
|
||||||
const url = new URL(event.request.url);
|
|
||||||
|
|
||||||
// Skip caching for auth routes
|
// Never cache private routes
|
||||||
if (url.pathname.startsWith('/auth/')) {
|
if (url.pathname.startsWith('/private')) {
|
||||||
return fetch(event.request);
|
event.respondWith(fetch(event.request));
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const cache = await caches.open(CACHE);
|
const respond = async () => {
|
||||||
|
const cache = await caches.open(CACHE);
|
||||||
|
|
||||||
// `build`/`files` can always be served from the cache
|
if (ASSETS.includes(url.pathname)) {
|
||||||
if (ASSETS.includes(url.pathname)) {
|
const cached = await cache.match(url.pathname);
|
||||||
const response = await cache.match(url.pathname);
|
if (cached) return cached;
|
||||||
|
}
|
||||||
|
|
||||||
if (response) {
|
try {
|
||||||
return response;
|
const response = await fetch(event.request);
|
||||||
}
|
if (response.status === 200 && build.length > 0 && url.pathname.startsWith(`/${build[0]}/`)) {
|
||||||
}
|
cache.put(event.request, response.clone());
|
||||||
|
}
|
||||||
|
return response;
|
||||||
|
} catch {
|
||||||
|
const cached = await cache.match(event.request);
|
||||||
|
if (cached) return cached;
|
||||||
|
}
|
||||||
|
|
||||||
// for everything else, try the network first, but
|
return new Response('Not found', { status: 404 });
|
||||||
// fall back to the cache if we're offline
|
};
|
||||||
try {
|
|
||||||
const response = await fetch(event.request);
|
|
||||||
|
|
||||||
// if we're offline, fetch can return a value that is not a Response
|
event.respondWith(respond());
|
||||||
// instead of throwing - and we can't pass this non-Response to respondWith
|
|
||||||
if (!(response instanceof Response)) {
|
|
||||||
throw new Error('invalid response from fetch');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (response.status === 200) {
|
|
||||||
cache.put(event.request, response.clone());
|
|
||||||
}
|
|
||||||
|
|
||||||
return response;
|
|
||||||
} catch (err) {
|
|
||||||
const response = await cache.match(event.request);
|
|
||||||
|
|
||||||
if (response) {
|
|
||||||
return response;
|
|
||||||
}
|
|
||||||
|
|
||||||
// if there's no cache, then just error out
|
|
||||||
// as there is nothing we can do to respond to this request
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
event.respondWith(respond());
|
|
||||||
});
|
});
|
||||||
Reference in New Issue
Block a user