-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice-worker.js
More file actions
258 lines (236 loc) · 8.68 KB
/
service-worker.js
File metadata and controls
258 lines (236 loc) · 8.68 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/**
* Service Worker for FPL IIM Mumbai Website
*
* Provides basic Progressive Web App (PWA) functionality:
* - Caches static assets for offline viewing
* - Handles failed network requests gracefully
* - Supports caching of CSS, JS, and image files
*
* @version 1.0.3
* @date 2025-09-04
*/
// Import shared version (available both in window and worker scope)
try {
importScripts('/version.js');
} catch (_) {}
const VERSION = (typeof self !== 'undefined' && self.SITE_VERSION) ? self.SITE_VERSION : 'dev';
const CACHE_NAME = `fpl-iim-mumbai-v${VERSION}`;
const CACHE_URLS = [
'/',
'/index.html',
'/winners.html',
// Consolidated CSS bundle
'/css/styles.css',
'/js/utils.js',
'/js/data-loader.js',
'/js/error-handler.js',
'/js/countdown.js',
'/js/ui-manager.js',
'/favicon.ico',
'/assets/icons.svg',
'/assets/fonts/poppins/poppins-latin-400.woff2',
'/assets/fonts/poppins/poppins-latin-600.woff2',
'/assets/fonts/poppins/poppins-latin-700.woff2',
// Twemoji local assets (self-hosted subset)
'/assets/twemoji/twemoji.min.js',
'/assets/twemoji/svg/26bd.svg', // ⚽
'/assets/twemoji/svg/1f386.svg', // 🎆
'/assets/twemoji/svg/1f3c6.svg', // 🏆
'/assets/twemoji/svg/1f4b0.svg', // 💰
'/assets/twemoji/svg/1f4c3.svg', // 📃 (rules)
'/assets/twemoji/svg/1f6aa.svg', // 🚪
'/assets/twemoji/svg/1f4c5.svg', // 📅
'/assets/twemoji/svg/1f5d3.svg', // 🗓️
'/assets/twemoji/svg/1f3c5.svg', // 🏅
'/assets/twemoji/svg/1f3af.svg', // 🎯
'/assets/twemoji/svg/1f4ca.svg', // 📊
'/assets/twemoji/svg/1f500.svg', // 🔀
'/assets/twemoji/svg/1f504.svg', // 🔄
'/assets/twemoji/svg/2b05.svg', // ⬅️
'/assets/twemoji/svg/25c0.svg', // ◀️
'/assets/twemoji/svg/25b6.svg', // ▶️
'/assets/twemoji/svg/1f465.svg', // 👥
'/assets/twemoji/svg/2139.svg', // ℹ️
];
// Install event - cache static assets
self.addEventListener('install', (event) => {
console.log('[ServiceWorker] Installing...');
event.waitUntil(
(async () => {
const cache = await caches.open(CACHE_NAME);
let urlsToCache = [...CACHE_URLS];
// Try to include build-time precache manifest if present
try {
const resp = await fetch('/precache-manifest.json', { cache: 'no-store' });
if (resp.ok) {
const manifest = await resp.json();
if (Array.isArray(manifest) && manifest.length) {
urlsToCache = Array.from(new Set(urlsToCache.concat(manifest)));
}
}
} catch (e) {
// No manifest available; continue with defaults
}
console.log('[ServiceWorker] Caching app shell:', urlsToCache.length, 'items');
await cache.addAll(urlsToCache.map((url) => new Request(url, { cache: 'no-cache' })));
})()
.catch((error) => {
console.warn('[ServiceWorker] Cache installation failed:', error);
// Don't fail the entire installation if caching fails
return Promise.resolve();
})
);
// Force the waiting service worker to become the active service worker
self.skipWaiting();
});
// Activate event - clean up old caches
self.addEventListener('activate', (event) => {
console.log('[ServiceWorker] Activating...');
event.waitUntil(
caches
.keys()
.then((cacheNames) => {
return Promise.all(
cacheNames.map((cacheName) => {
if (cacheName !== CACHE_NAME) {
console.log('[ServiceWorker] Removing old cache:', cacheName);
return caches.delete(cacheName);
}
})
);
})
.then(() => {
// Claim control of all clients immediately
return self.clients.claim();
})
);
});
// Fetch event - serve cached content when offline
self.addEventListener('fetch', (event) => {
// Only handle GET requests
if (event.request.method !== 'GET') return;
// Skip cross-origin requests (APIs, external resources)
if (!event.request.url.startsWith(self.location.origin)) return;
const req = event.request;
// Detect navigation/HTML requests
const isNavigation =
req.mode === 'navigate' ||
(req.headers && req.headers.get('accept') && req.headers.get('accept').includes('text/html'));
if (isNavigation) {
// Network-first for navigations so reload gets freshest HTML
event.respondWith(
fetch(req, { cache: 'no-store' })
.then((networkResp) => {
// Cache a copy as offline fallback
if (networkResp && networkResp.status === 200) {
const copy = networkResp.clone();
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => cache.put(req, copy)).catch(() => {})
);
}
return networkResp;
})
.catch(async () => {
const cached = await caches.match(req);
if (cached) return cached;
// Fallback simple offline page
return new Response(
`<!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>Offline</title><style>body{font-family:Arial,sans-serif;background:#f5f5f5;margin:0;padding:32px;text-align:center}.card{max-width:520px;margin:0 auto;background:#fff;border-radius:8px;box-shadow:0 2px 10px rgba(0,0,0,.1);padding:24px}h1{color:#37003c;margin:0 0 8px}button{background:#37003c;color:#fff;border:none;border-radius:6px;padding:10px 16px;cursor:pointer;margin-top:16px}</style></head><body><div class="card"><h1>🏈 FPL IIM Mumbai</h1><p>You're offline. Please reconnect and try again.</p><button onclick="location.reload()">Retry</button></div></body></html>`,
{ headers: { 'Content-Type': 'text/html' } }
);
})
);
return;
}
// Cache-first with background update for static assets
event.respondWith(
caches.match(req).then((cachedResponse) => {
if (cachedResponse) {
// Background revalidation
event.waitUntil(
fetch(req)
.then((networkResp) => {
if (networkResp && networkResp.status === 200 && networkResp.type === 'basic') {
return caches.open(CACHE_NAME).then((cache) =>
cache.put(req, withLongTTL(networkResp.clone(), req))
);
}
})
.catch(() => {})
);
return cachedResponse;
}
return fetch(req)
.then((response) => {
if (!response || response.status !== 200 || response.type !== 'basic') {
return response;
}
if (shouldCache(req.url)) {
const responseToCache = withLongTTL(response.clone(), req);
caches
.open(CACHE_NAME)
.then((cache) => cache.put(req, responseToCache))
.catch((error) => console.warn('[ServiceWorker] Failed to cache response:', error));
}
return withLongTTL(response, req);
})
.catch((error) => {
console.warn('[ServiceWorker] Network request failed:', req.url, error);
throw error;
});
})
);
});
/**
* Determines if a URL should be cached
* @param {string} url - The URL to check
* @returns {boolean} - Whether to cache this URL
*/
function shouldCache(url) {
// Cache CSS, JS, images, and HTML files
return (
url.includes('.css') ||
url.includes('.js') ||
url.includes('.png') ||
url.includes('.jpg') ||
url.includes('.jpeg') ||
url.includes('.gif') ||
url.includes('.svg') ||
url.includes('.ico') ||
url.includes('.html') ||
url.endsWith('/')
);
}
/**
* Wrap a Response ensuring long-lived caching semantics for static assets.
* Adds Cache-Control headers to responses we serve (does not change server headers).
*/
function withLongTTL(response, request) {
try {
const url = new URL(request.url);
const isHTML = request.destination === 'document' || url.pathname.endsWith('.html');
const isStatic = ['style', 'script', 'font', 'image'].includes(request.destination);
// For HTML, avoid long TTL; let the browser revalidate normally.
if (isHTML) return response;
// For static assets, add long TTL + immutable hint.
if (isStatic || shouldCache(url.pathname)) {
const headers = new Headers(response.headers);
headers.set('Cache-Control', 'public, max-age=31536000, immutable');
return new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers,
});
}
} catch (e) {
// Fall through if anything goes wrong
}
return response;
}
// Handle service worker updates
self.addEventListener('message', (event) => {
if (event.data && event.data.type === 'SKIP_WAITING') {
self.skipWaiting();
}
});
console.log('[ServiceWorker] Service Worker script loaded');