-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
120 lines (109 loc) · 3.13 KB
/
Copy pathsw.js
File metadata and controls
120 lines (109 loc) · 3.13 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
/* ============================================
OFFLOAD — Service Worker
Cache-first strategy for offline PWA support
============================================ */
const CACHE_NAME = 'offload-v1';
const STATIC_ASSETS = [
'./',
'./index.html',
'./css/style.css',
'./js/app.js',
'./js/data-store.js',
'./js/audio-engine.js',
'./js/ceremony-renderer.js',
'./js/sentiment-analyzer.js',
'./manifest.json',
'./icon-192.png',
'./icon-512.png',
'./apple-touch-icon.png',
'./favicon-32x32.png',
'./maskable-icon-512.png',
'./og-image.png',
'./lyf-badge.png',
'./moon-new-moon.png',
'./moon-waxing-crescent.png',
'./moon-first-quarter.png',
'./moon-waxing-gibbous.png',
'./moon-full-moon.png',
'./moon-waning-gibbous.png',
'./moon-last-quarter.png',
'./moon-waning-crescent.png'
];
// Install: Cache static assets
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME)
.then((cache) => cache.addAll(STATIC_ASSETS))
.then(() => self.skipWaiting())
);
});
// Activate: Clean old caches
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((cacheNames) => {
return Promise.all(
cacheNames
.filter((name) => name !== CACHE_NAME)
.map((name) => caches.delete(name))
);
}).then(() => self.clients.claim())
);
});
// Fetch: Cache-first strategy
self.addEventListener('fetch', (event) => {
const { request } = event;
// Skip non-GET requests
if (request.method !== 'GET') {
return;
}
// Skip cross-origin requests
if (!request.url.startsWith(self.location.origin)) {
return;
}
event.respondWith(
caches.match(request).then((cachedResponse) => {
if (cachedResponse) {
// Return cached response and refresh in background
fetch(request)
.then((networkResponse) => {
if (networkResponse && networkResponse.ok) {
caches.open(CACHE_NAME).then((cache) => {
cache.put(request, networkResponse.clone());
});
}
})
.catch(() => {
// Network failed, cached response is fine
});
return cachedResponse;
}
// Not in cache, fetch from network
return fetch(request)
.then((networkResponse) => {
if (!networkResponse || !networkResponse.ok) {
return networkResponse;
}
// Cache the new response
const responseToCache = networkResponse.clone();
caches.open(CACHE_NAME).then((cache) => {
cache.put(request, responseToCache);
});
return networkResponse;
})
.catch(() => {
// Network failed, no cache available
return new Response('Offline — content not available', {
status: 503,
statusText: 'Service Unavailable',
headers: { 'Content-Type': 'text/plain' }
});
});
})
);
});
// Background sync for offline writes
self.addEventListener('sync', (event) => {
if (event.tag === 'offload-sync') {
event.waitUntil(Promise.resolve());
}
});