-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
160 lines (138 loc) · 4.6 KB
/
Copy pathsw.js
File metadata and controls
160 lines (138 loc) · 4.6 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
const CACHE_NAME = 'basujindal-v3';
const STATIC_CACHE = 'static-v3';
const CDN_CACHE = 'cdn-v1';
const BLOG_CACHE = 'blog-v1';
// Core static assets to pre-cache on install
const PRECACHE_URLS = [
'/css/main.css',
'/js/config.js',
'/js/components.js',
'/js/main.js',
'/js/post.js',
'/js/blog-list.js',
];
// Install - pre-cache core assets
self.addEventListener('install', event => {
event.waitUntil(
caches.open(STATIC_CACHE)
.then(cache => cache.addAll(PRECACHE_URLS))
.then(() => self.skipWaiting())
);
});
// Activate - clean old caches
self.addEventListener('activate', event => {
const currentCaches = [CACHE_NAME, STATIC_CACHE, CDN_CACHE, BLOG_CACHE];
event.waitUntil(
caches.keys()
.then(names => Promise.all(
names.filter(name => !currentCaches.includes(name)).map(name => caches.delete(name))
))
.then(() => self.clients.claim())
);
});
// Fetch strategies
self.addEventListener('fetch', event => {
const { request } = event;
const url = new URL(request.url);
// Skip non-GET requests
if (request.method !== 'GET') return;
// Skip API calls
if (url.hostname === 'server.basujindal.me') return;
// CDN resources (jsdelivr, unpkg) - cache-first
if (url.hostname.includes('jsdelivr.net') || url.hostname.includes('unpkg.com') ||
url.hostname.includes('cdnjs.cloudflare.com')) {
event.respondWith(cacheFirst(request, CDN_CACHE));
return;
}
// Blog markdown files - stale-while-revalidate
if (url.pathname.match(/\/(blog-posts|drafts)\/.*\.md$/)) {
event.respondWith(staleWhileRevalidate(request, BLOG_CACHE));
return;
}
// Static assets (CSS, JS, images, WebP) - stale-while-revalidate
if (url.pathname.match(/\.(css|js|png|jpg|jpeg|webp|gif|svg|ico|woff2?)$/)) {
event.respondWith(staleWhileRevalidate(request, STATIC_CACHE));
return;
}
// HTML pages - network-first with cache fallback
if (request.headers.get('accept')?.includes('text/html')) {
event.respondWith(networkFirst(request, CACHE_NAME));
return;
}
});
// Cache-first: serve from cache, fall back to network
async function cacheFirst(request, cacheName) {
const cached = await caches.match(request);
if (cached) return cached;
const response = await fetch(request);
if (response.ok) {
const cache = await caches.open(cacheName);
cache.put(request, response.clone());
}
return response;
}
// Stale-while-revalidate: serve cached, update in background
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;
}).catch(() => cached);
return cached || fetchPromise;
}
// Network-first: try network, fall back to cache
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 || new Response('Offline', { status: 503, statusText: 'Service Unavailable' });
}
}
// ===== Web Push: Hacker News top-post notifications =====
self.addEventListener('push', event => {
let data = {};
try {
data = event.data ? event.data.json() : {};
} catch {
data = { body: event.data && event.data.text() };
}
const title = data.title || 'Hacker News';
const options = {
body: data.body || '',
icon: '/images/basujindal.jpg',
badge: '/images/basujindal.jpg',
tag: data.tag || 'hn-notification',
renotify: true,
data: {
url: data.url || data.hn_url || 'https://news.ycombinator.com/',
hn_url: data.hn_url || 'https://news.ycombinator.com/'
},
actions: [
{ action: 'open', title: 'Read article' },
{ action: 'comments', title: 'HN comments' }
]
};
event.waitUntil(self.registration.showNotification(title, options));
});
self.addEventListener('notificationclick', event => {
event.notification.close();
const d = event.notification.data || {};
const target = event.action === 'comments' ? (d.hn_url || d.url) : d.url;
event.waitUntil(
clients.matchAll({ type: 'window', includeUncontrolled: true }).then(windowClients => {
for (const client of windowClients) {
if (client.url === target && 'focus' in client) return client.focus();
}
if (clients.openWindow) return clients.openWindow(target || 'https://news.ycombinator.com/');
})
);
});