-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
115 lines (99 loc) · 3.24 KB
/
sw.js
File metadata and controls
115 lines (99 loc) · 3.24 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
const CACHE_NAME = 'basujindal-v1';
const STATIC_CACHE = 'static-v1';
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' });
}
}