-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
102 lines (84 loc) · 2.75 KB
/
sw.js
File metadata and controls
102 lines (84 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/**
* FillKit Demo Hub — Service Worker
*
* Strategy:
* - Navigation requests → network-first, fall back to cached /index.html
* - Static assets (.js, .css, .svg, .png, .woff2) → cache-first
* - External CDN requests (e.g. Tailwind) → skip, let browser cache handle
*
* Bump CACHE_VERSION to invalidate all caches on deploy.
*/
const CACHE_VERSION = 'fillkit-demo-v1';
const STATIC_EXTENSIONS = ['.js', '.css', '.svg', '.png', '.woff2', '.woff', '.ico'];
// ---------------- Install ----------------
self.addEventListener('install', (event) => {
// Activate immediately, don't wait for existing tabs to close
self.skipWaiting();
});
// ---------------- Activate ----------------
self.addEventListener('activate', (event) => {
event.waitUntil(
caches
.keys()
.then((keys) =>
Promise.all(
keys
.filter((key) => key !== CACHE_VERSION)
.map((key) => caches.delete(key))
)
)
.then(() => self.clients.claim())
);
});
// ---------------- Fetch ----------------
self.addEventListener('fetch', (event) => {
const { request } = event;
const url = new URL(request.url);
// Only handle same-origin requests — skip external CDN, analytics, etc.
if (url.origin !== self.location.origin) return;
// Navigation requests → network-first with offline fallback
if (request.mode === 'navigate') {
event.respondWith(networkFirstNavigation(request));
return;
}
// Static assets → cache-first
if (isStaticAsset(url.pathname)) {
event.respondWith(cacheFirst(request));
return;
}
// Everything else → network only (no caching)
});
// ---------------- Strategies ----------------
async function networkFirstNavigation(request) {
try {
const response = await fetch(request);
// Cache a clone for offline use
const cache = await caches.open(CACHE_VERSION);
cache.put(request, response.clone());
return response;
} catch {
// Offline — try cached version, then fall back to cached /index.html
const cached = await caches.match(request);
if (cached) return cached;
return caches.match('/index.html');
}
}
async function cacheFirst(request) {
const cached = await caches.match(request);
if (cached) return cached;
try {
const response = await fetch(request);
if (response.ok) {
const cache = await caches.open(CACHE_VERSION);
cache.put(request, response.clone());
}
return response;
} catch {
// Asset not available offline — return a basic 503
return new Response('Offline', { status: 503, statusText: 'Service Unavailable' });
}
}
// ---------------- Helpers ----------------
function isStaticAsset(pathname) {
return STATIC_EXTENSIONS.some((ext) => pathname.endsWith(ext));
}