-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsw.js
More file actions
100 lines (92 loc) · 3 KB
/
Copy pathsw.js
File metadata and controls
100 lines (92 loc) · 3 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
// Be sure to bump this version number whenever you change this file
// so the browser knows to run the 'install' and 'activate' steps again.
const CACHE_NAME = 'klasskit-v1.4.9';
// 1. Pre-cache Core Assets (Static)
const PRE_CACHE_ASSETS = [
'./',
'./index.html',
'./hub.html',
'./admin/index.html',
'./admin/style.css',
'./admin/script.js',
'./script.js',
'./games.json',
'./manifest.json',
'./css/base.css',
'./css/components.css',
'./css/home.css',
'./css/side-panel.css',
'./media/icon.png',
'./media/icon-180.png',
'./media/icon-192.png',
'./media/icon-512.png',
'./media/icon.ico',
'./media/icon.webp',
// Optimized Previews (webp)
'./media/card_match_preview.webp',
'./media/classtally_preview.webp',
'./media/connect_four_preview.webp',
'./media/hangman_preview.webp',
'./media/magic_cups_preview.webp',
'./media/presentation_preview.webp',
'./media/quiz_preview.webp',
'./media/spinwheel_preview.webp',
'./media/teampicker_preview.webp',
'./media/whiteboard_preview.webp',
'./media/word_search_preview.webp'
];
// Install Event - Pre-cache static assets
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => {
console.log('[SW] Pre-caching core assets');
return cache.addAll(PRE_CACHE_ASSETS);
})
);
self.skipWaiting();
});
// Activate Event - Clean up old caches
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((cacheNames) => {
return Promise.all(
cacheNames.map((cache) => {
if (cache !== CACHE_NAME) {
console.log('[SW] Deleting old cache:', cache);
return caches.delete(cache);
}
})
);
})
);
self.clients.claim();
});
// Fetch Event
self.addEventListener('fetch', (event) => {
const { request } = event;
const url = new URL(request.url);
// 1. Ignore non-GET and non-HTTP requests
if (request.method !== 'GET' || !url.protocol.startsWith('http')) return;
// 2. Strategy: Network-Only for Supabase/API calls
// We don't want to cache database responses here as they are handled by the app's sync logic.
if (url.hostname.includes('supabase.co')) return;
// 3. Strategy: Stale-While-Revalidate for all other assets
// This gives the fastest load time while keeping assets fresh in the background.
event.respondWith(
caches.open(CACHE_NAME).then((cache) => {
return cache.match(request).then((cachedResponse) => {
const fetchPromise = fetch(request).then((networkResponse) => {
// If network call succeeds, update cache
if (networkResponse && networkResponse.status === 200) {
cache.put(request, networkResponse.clone());
}
return networkResponse;
}).catch(() => {
// If network fails completely, we already returned cachedResponse
});
// Return cached if available, otherwise wait for network
return cachedResponse || fetchPromise;
});
})
);
});