-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
152 lines (138 loc) · 4.38 KB
/
sw.js
File metadata and controls
152 lines (138 loc) · 4.38 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
/**
* Service Worker for Open-Interview
* Enables offline support and faster repeat visits
*/
// Cache version tied to build time — changes on every deploy, busting stale asset caches
const CACHE_VERSION = '1777635037531';
const STATIC_CACHE = `code-reels-static-${CACHE_VERSION}`;
const DATA_CACHE = `code-reels-data-${CACHE_VERSION}`;
const RUNTIME_CACHE = `code-reels-runtime-${CACHE_VERSION}`;
// Static assets to cache on install (excluding index.html to prevent stale chunk references)
const STATIC_ASSETS = [
'/',
];
// Install event - cache static assets
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(STATIC_CACHE).then((cache) => {
return cache.addAll(STATIC_ASSETS);
}).then(() => {
// Activate immediately
return self.skipWaiting();
})
);
});
// Activate event - clean up old caches
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((cacheNames) => {
return Promise.all(
cacheNames
.filter((name) => {
return name.startsWith('code-reels-') &&
name !== STATIC_CACHE &&
name !== DATA_CACHE &&
name !== RUNTIME_CACHE;
})
.map((name) => caches.delete(name))
);
}).then(() => {
// Take control of all pages immediately
return self.clients.claim();
})
);
});
// Fetch event - serve from cache with network fallback
self.addEventListener('fetch', (event) => {
const { request } = event;
const url = new URL(request.url);
// Skip non-GET requests
if (request.method !== 'GET') return;
// Skip chrome-extension and other non-http(s) requests
if (!url.protocol.startsWith('http')) return;
// Handle data files (JSON) - cache-first strategy
if (url.pathname.includes('/data/') && url.pathname.endsWith('.json')) {
event.respondWith(
caches.open(DATA_CACHE).then((cache) => {
return cache.match(request).then((cachedResponse) => {
if (cachedResponse) {
// Return cached, but also update cache in background
fetch(request).then((networkResponse) => {
if (networkResponse.ok) {
cache.put(request, networkResponse.clone());
}
}).catch(() => {});
return cachedResponse;
}
// Not in cache, fetch from network
return fetch(request).then((networkResponse) => {
if (networkResponse.ok) {
cache.put(request, networkResponse.clone());
}
return networkResponse;
});
});
})
);
return;
}
// Handle static assets (JS, CSS, images) - cache-first
if (
url.pathname.endsWith('.js') ||
url.pathname.endsWith('.css') ||
url.pathname.endsWith('.png') ||
url.pathname.endsWith('.svg') ||
url.pathname.endsWith('.ico') ||
url.pathname.endsWith('.woff2')
) {
event.respondWith(
caches.open(RUNTIME_CACHE).then((cache) => {
return cache.match(request).then((cachedResponse) => {
if (cachedResponse) {
return cachedResponse;
}
return fetch(request).then((networkResponse) => {
if (networkResponse.ok) {
cache.put(request, networkResponse.clone());
}
return networkResponse;
});
});
})
);
return;
}
// Handle navigation requests - always network-first, never cache index.html
// Caching index.html causes stale chunk hash mismatches after deploys
if (request.mode === 'navigate') {
event.respondWith(
fetch(request).catch(() => {
// Offline fallback - serve cached non-index page or nothing
return caches.match(request);
})
);
return;
}
});
// Handle messages from the main thread
self.addEventListener('message', (event) => {
if (event.data === 'skipWaiting') {
self.skipWaiting();
}
// Prefetch specific channels
if (event.data?.type === 'prefetch' && event.data?.urls) {
event.waitUntil(
caches.open(DATA_CACHE).then((cache) => {
return Promise.all(
event.data.urls.map((url) => {
return fetch(url).then((response) => {
if (response.ok) {
return cache.put(url, response);
}
}).catch(() => {});
})
);
})
);
}
});