-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
106 lines (97 loc) · 2.98 KB
/
sw.js
File metadata and controls
106 lines (97 loc) · 2.98 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
// sw.js - Service Worker for HN Reader PWA
const CACHE_VERSION = 'hn-v12';
const PRECACHE_URLS = [
'/HN/',
'/HN/index.html',
'/HN/static/css/style.css',
'/HN/static/js/utils.js',
'/HN/static/js/api.js',
'/HN/static/js/thumbnails.js',
'/HN/static/js/extractive.js',
'/HN/static/js/summaries.js',
'/HN/static/js/settings.js',
'/HN/static/js/stories.js',
'/HN/static/js/comments.js',
'/HN/static/js/pullrefresh.js',
'/HN/static/js/reader.js',
'/HN/static/js/app.js',
'/HN/static/icon-192.png',
'/HN/static/icon-512.png',
'/HN/static/favicon.ico'
];
const CDN_URLS = [
'cdn.tailwindcss.com',
'cdn.jsdelivr.net'
];
// Install: precache app shell
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_VERSION).then((cache) => cache.addAll(PRECACHE_URLS))
);
self.skipWaiting();
});
// Activate: clean old caches
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((keys) =>
Promise.all(keys.filter((k) => k !== CACHE_VERSION).map((k) => caches.delete(k)))
)
);
self.clients.claim();
});
// Fetch strategies
self.addEventListener('fetch', (event) => {
const url = new URL(event.request.url);
// Static assets + app shell: stale-while-revalidate
if (url.pathname.startsWith('/HN/static/') || url.pathname === '/HN/' || url.pathname === '/HN/index.html') {
event.respondWith(
caches.match(event.request).then((cached) => {
const fetchPromise = fetch(event.request).then((res) => {
const clone = res.clone();
caches.open(CACHE_VERSION).then((cache) => cache.put(event.request, clone));
return res;
}).catch(() => cached);
return cached || fetchPromise;
})
);
return;
}
// HN API: network-first with cache fallback
if (url.hostname === 'hacker-news.firebaseio.com') {
event.respondWith(
fetch(event.request)
.then((res) => {
const clone = res.clone();
caches.open(CACHE_VERSION).then((cache) => cache.put(event.request, clone));
return res;
})
.catch(() => caches.match(event.request))
);
return;
}
// CDN scripts: stale-while-revalidate
if (CDN_URLS.some((cdn) => url.hostname.includes(cdn))) {
event.respondWith(
caches.match(event.request).then((cached) => {
const fetchPromise = fetch(event.request).then((res) => {
const clone = res.clone();
caches.open(CACHE_VERSION).then((cache) => cache.put(event.request, clone));
return res;
});
return cached || fetchPromise;
})
);
return;
}
// Third-party APIs (Jina, OpenAI, Microlink): network-only
if (url.hostname.includes('r.jina.ai') ||
url.hostname.includes('api.openai.com') ||
url.hostname.includes('api.microlink.io')) {
event.respondWith(fetch(event.request));
return;
}
// Default: network-first
event.respondWith(
fetch(event.request).catch(() => caches.match(event.request))
);
});