This commit is contained in:
2025-09-18 10:20:04 -06:00
parent b20e43b951
commit 12305887f8
3 changed files with 82 additions and 2 deletions

View File

@@ -0,0 +1,77 @@
/// <reference lib="webworker" />
/// <reference types="@sveltejs/kit" />
import { build, files, version } from '$service-worker';
// Give `self` the correct type
const selfRef = globalThis.self as unknown as ServiceWorkerGlobalScope;
// Unique cache key per deployment
const CACHE = `gt-cache-${version}`;
// Precache application shell (built assets) and static files
const ASSETS = [
...build,
...files
];
selfRef.addEventListener('install', (event) => {
event.waitUntil(
(async () => {
const cache = await caches.open(CACHE);
await cache.addAll(ASSETS);
})()
);
});
selfRef.addEventListener('activate', (event) => {
event.waitUntil(
(async () => {
const keys = await caches.keys();
await Promise.all(keys.map((k) => (k === CACHE ? undefined : caches.delete(k))));
// Claim clients so updated SW takes control immediately on refresh
await selfRef.clients.claim();
})()
);
});
selfRef.addEventListener('fetch', (event: FetchEvent) => {
// Only handle GET requests
if (event.request.method !== 'GET') return;
const url = new URL(event.request.url);
// Serve precached ASSETS from cache directly
if (ASSETS.includes(url.pathname)) {
event.respondWith(
(async () => {
const cache = await caches.open(CACHE);
const cached = await cache.match(url.pathname);
if (cached) return cached;
// Fallback to network if somehow missing
const res = await fetch(event.request);
if (res.ok) cache.put(url.pathname, res.clone());
return res;
})()
);
return;
}
// Runtime: network-first with cache fallback for other GETs
event.respondWith(
(async () => {
const cache = await caches.open(CACHE);
try {
const res = await fetch(event.request);
if (res instanceof Response && res.status === 200) {
cache.put(event.request, res.clone());
}
return res;
} catch (err) {
const cached = await cache.match(event.request);
if (cached) return cached;
throw err; // propagate if nothing cached
}
})()
);
});

View File

@@ -8,7 +8,10 @@ const config = {
adapter: adapter({ adapter: adapter({
fallback: '200.html' fallback: '200.html'
}), }),
// Service worker wiring comes in T008 serviceWorker: {
// keep default auto-registration explicit
register: true
},
paths: { paths: {
// supports GitHub Pages-like hosting later; keep default for now // supports GitHub Pages-like hosting later; keep default for now
} }

View File

@@ -60,7 +60,7 @@ Paths below are absolute to this repo.
- Root CI scripts in tools/ci (stub) and package scripts wiring - Root CI scripts in tools/ci (stub) and package scripts wiring
- Dependencies: T005 - Dependencies: T005
- [ ] T008 PWA service worker wiring (SvelteKit) - [X] T008 PWA service worker wiring (SvelteKit)
- Enable service worker in SvelteKit config and add minimal SW handler - Enable service worker in SvelteKit config and add minimal SW handler
- Ensure static asset caching strategy is defined (runtime-minimal) - Ensure static asset caching strategy is defined (runtime-minimal)
- Dependencies: T005 - Dependencies: T005