This repository was archived by the owner on Oct 31, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathsw.js
More file actions
87 lines (81 loc) · 2.79 KB
/
Copy pathsw.js
File metadata and controls
87 lines (81 loc) · 2.79 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
// Offline LMS - Service Worker for Offline PWA Support
const CACHE_NAME = 'offline-lms-v1';
const ASSETS_TO_CACHE = [
'./',
'./index.html',
'./styles.css',
'./app.js',
'./manifest.json',
'./icons/icon-192.png',
'./icons/icon-512.png',
'./icons/icon.svg',
'./icons/apple-touch-icon.png',
'./icons/favicon-32.png',
'https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&family=JetBrains+Mono:wght@400;500&display=swap'
];
// Install: Cache all core assets
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => {
return cache.addAll(ASSETS_TO_CACHE).catch((err) => {
console.warn('Some assets could not be precached during SW install:', err);
});
}).then(() => self.skipWaiting())
);
});
// Activate: Clean up old caches
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((keys) => {
return Promise.all(
keys.map((key) => {
if (key !== CACHE_NAME) {
return caches.delete(key);
}
})
);
}).then(() => self.clients.claim())
);
});
// Fetch: Stale-while-revalidate or Cache-first for UI shell
self.addEventListener('fetch', (event) => {
const url = new URL(event.request.url);
// Ignore non-GET, blob:, data:, chrome-extension:, or range requests (videos)
if (
event.request.method !== 'GET' ||
url.protocol.startsWith('blob') ||
url.protocol.startsWith('data') ||
event.request.headers.get('range')
) {
return;
}
// Handle local app assets with Cache-First strategy
event.respondWith(
caches.match(event.request).then((cachedResponse) => {
if (cachedResponse) {
// Fetch in background to update cache (stale-while-revalidate)
fetch(event.request).then((networkResponse) => {
if (networkResponse && networkResponse.status === 200 && networkResponse.type === 'basic') {
const clone = networkResponse.clone();
caches.open(CACHE_NAME).then((cache) => cache.put(event.request, clone));
}
}).catch(() => {});
return cachedResponse;
}
// If not in cache, fetch from network
return fetch(event.request).then((networkResponse) => {
if (!networkResponse || networkResponse.status !== 200 || (networkResponse.type !== 'basic' && networkResponse.type !== 'cors')) {
return networkResponse;
}
const clone = networkResponse.clone();
caches.open(CACHE_NAME).then((cache) => cache.put(event.request, clone));
return networkResponse;
}).catch(() => {
// Offline fallback for HTML navigation
if (event.request.mode === 'navigate') {
return caches.match('./index.html') || caches.match('./');
}
});
})
);
});