-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsw.js
More file actions
83 lines (74 loc) · 2.26 KB
/
Copy pathsw.js
File metadata and controls
83 lines (74 loc) · 2.26 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
/* ============================================================
Offline Python Tutor — Service Worker
Cache-first for shell, network-first for content
============================================================ */
const CACHE_VERSION = 'pytutor-v2026-05-19a';
const SHELL_ASSETS = [
'./',
'./index.html',
'./base.css',
'./style.css',
'./tutor-chat.css',
'./tutor-codelab.css',
'./app.js',
'./tutor-chat.js',
'./tutor-codelab.js',
'./manifest.json',
'./assets/favicon.svg',
'./content/sections.json'
];
/* ---------- Install: pre-cache the app shell ---------- */
self.addEventListener('install', (e) => {
e.waitUntil(
caches.open(CACHE_VERSION)
.then(cache => cache.addAll(SHELL_ASSETS))
.then(() => self.skipWaiting()) // Activate immediately
);
});
/* ---------- Activate: purge old caches ---------- */
self.addEventListener('activate', (e) => {
e.waitUntil(
caches.keys().then(keys =>
Promise.all(
keys
.filter(k => k !== CACHE_VERSION)
.map(k => caches.delete(k))
)
).then(() => self.clients.claim()) // Take control of all tabs
);
});
/* ---------- Fetch: stale-while-revalidate for shell, network-first for fonts ---------- */
self.addEventListener('fetch', (e) => {
const url = new URL(e.request.url);
// Skip cross-origin requests except fonts
if (url.origin !== location.origin) {
// Let font requests through to network (they have their own CDN caching)
return;
}
// Never cache tutor backend API calls — they must hit the live FastAPI server.
if (url.pathname.startsWith('/api/')) {
return;
}
// For navigation requests, serve the shell (SPA)
if (e.request.mode === 'navigate') {
e.respondWith(
caches.match('./index.html')
.then(cached => cached || fetch(e.request))
);
return;
}
// Stale-while-revalidate for everything else
e.respondWith(
caches.open(CACHE_VERSION).then(cache =>
cache.match(e.request).then(cached => {
const fetchPromise = fetch(e.request).then(response => {
if (response.ok) {
cache.put(e.request, response.clone());
}
return response;
}).catch(() => cached);
return cached || fetchPromise;
})
)
);
});