-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
111 lines (99 loc) · 3.91 KB
/
Copy pathsw.js
File metadata and controls
111 lines (99 loc) · 3.91 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
103
104
105
106
107
108
109
110
/**
* sw.js — Service Worker for python-remote-sensing.com
* Strategy:
* - HTML pages: Network-first → cache fallback
* - CSS/JS/fonts: Cache-first (static assets, versioned by URL)
* - Images/icons: Stale-while-revalidate
*/
const CACHE_VERSION = 'v1';
const STATIC_CACHE = `prs-static-${CACHE_VERSION}`;
const PAGES_CACHE = `prs-pages-${CACHE_VERSION}`;
const IMAGE_CACHE = `prs-images-${CACHE_VERSION}`;
const PRECACHE_ASSETS = [
'/',
'/assets/css/main.css',
'/assets/css/prism-theme.css',
'/assets/js/copy-button.js',
'/assets/js/site.js',
'/favicon.svg',
'/manifest.json',
];
// ── Install ──────────────────────────────────────────────────────────────────
self.addEventListener('install', event => {
event.waitUntil(
caches.open(STATIC_CACHE).then(cache => cache.addAll(PRECACHE_ASSETS))
);
self.skipWaiting();
});
// ── Activate ─────────────────────────────────────────────────────────────────
self.addEventListener('activate', event => {
const allowedCaches = [STATIC_CACHE, PAGES_CACHE, IMAGE_CACHE];
event.waitUntil(
caches.keys().then(keys =>
Promise.all(
keys.filter(k => !allowedCaches.includes(k)).map(k => caches.delete(k))
)
)
);
self.clients.claim();
});
// ── Fetch ─────────────────────────────────────────────────────────────────────
self.addEventListener('fetch', event => {
const { request } = event;
const url = new URL(request.url);
// Only handle same-origin GET requests
if (request.method !== 'GET' || url.origin !== self.location.origin) return;
const isHTML = request.headers.get('accept') && request.headers.get('accept').includes('text/html');
const isStatic = /\.(css|js|woff2?|ttf|otf)(\?.*)?$/.test(url.pathname);
const isImage = /\.(svg|png|jpg|jpeg|webp|gif|ico)(\?.*)?$/.test(url.pathname);
if (isHTML) {
// Network-first for HTML
event.respondWith(networkFirst(request, PAGES_CACHE));
} else if (isStatic) {
// Cache-first for static assets
event.respondWith(cacheFirst(request, STATIC_CACHE));
} else if (isImage) {
// Stale-while-revalidate for images
event.respondWith(staleWhileRevalidate(request, IMAGE_CACHE));
} else {
// Default: network with cache fallback
event.respondWith(networkFirst(request, PAGES_CACHE));
}
});
// ── Strategies ────────────────────────────────────────────────────────────────
async function networkFirst(request, cacheName) {
try {
const response = await fetch(request);
if (response.ok) {
const cache = await caches.open(cacheName);
cache.put(request, response.clone());
}
return response;
} catch {
const cached = await caches.match(request);
return cached || caches.match('/');
}
}
async function cacheFirst(request, cacheName) {
const cached = await caches.match(request);
if (cached) return cached;
try {
const response = await fetch(request);
if (response.ok) {
const cache = await caches.open(cacheName);
cache.put(request, response.clone());
}
return response;
} catch {
return new Response('', { status: 503 });
}
}
async function staleWhileRevalidate(request, cacheName) {
const cache = await caches.open(cacheName);
const cached = await cache.match(request);
const fetchPromise = fetch(request).then(response => {
if (response.ok) cache.put(request, response.clone());
return response;
});
return cached || fetchPromise;
}