-
-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathsw.js
More file actions
81 lines (75 loc) · 2.63 KB
/
Copy pathsw.js
File metadata and controls
81 lines (75 loc) · 2.63 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
// WordFeather - Service Worker
// CACHE_NAME below is auto-updated by build.py on every run — do not edit by hand.
// AUTO-CACHE-VERSION-START
const CACHE_NAME = 'deutsch-lernen-v20260916-152248';
// AUTO-CACHE-VERSION-END
const BASE = '/';
// Static assets that rarely change (cache-first)
const STATIC_ASSETS = [
'https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css',
'https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js',
'https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap'
];
// Install: cache static assets only
self.addEventListener('install', function(event) {
event.waitUntil(
caches.open(CACHE_NAME).then(function(cache) {
return cache.addAll(STATIC_ASSETS);
})
);
self.skipWaiting();
});
// Activate: clean ALL old caches immediately
self.addEventListener('activate', function(event) {
event.waitUntil(
caches.keys().then(function(names) {
return Promise.all(
names.filter(function(name) { return name !== CACHE_NAME; })
.map(function(name) { return caches.delete(name); })
);
})
);
self.clients.claim();
});
// Fetch strategy:
// HTML pages: network-first (always get latest, cache as offline fallback)
// Static assets: cache-first (fast loading)
self.addEventListener('fetch', function(event) {
var request = event.request;
var url = new URL(request.url);
// HTML pages and own assets: network-first
if (request.mode === 'navigate' ||
(url.origin === self.location.origin && url.pathname.startsWith(BASE))) {
event.respondWith(
fetch(request).then(function(networkResponse) {
if (networkResponse && networkResponse.status === 200) {
var responseClone = networkResponse.clone();
caches.open(CACHE_NAME).then(function(cache) {
cache.put(request, responseClone);
});
}
return networkResponse;
}).catch(function() {
return caches.match(request).then(function(cachedResponse) {
return cachedResponse || caches.match(BASE);
});
})
);
return;
}
// External static assets (CDN): cache-first
event.respondWith(
caches.match(request).then(function(cachedResponse) {
if (cachedResponse) { return cachedResponse; }
return fetch(request).then(function(networkResponse) {
if (networkResponse && networkResponse.status === 200) {
var responseClone = networkResponse.clone();
caches.open(CACHE_NAME).then(function(cache) {
cache.put(request, responseClone);
});
}
return networkResponse;
});
})
);
});