-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
126 lines (116 loc) · 3.98 KB
/
Copy pathsw.js
File metadata and controls
126 lines (116 loc) · 3.98 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
121
122
123
124
125
126
const CACHE_NAME = 'haven-v12';
const CONTENT_CACHE = 'haven-content-v12';
const APP_SHELL = [
'/',
'/index.html',
'/app.js',
'/style.css',
'/manifest.json',
'/icons/icon-192.svg',
'/icons/icon-512.svg',
];
const CONTENT_FILES = [
'/content/itinerary.json',
'/content/gf-guide.json',
'/content/ports.json',
'/content/spa.json',
'/content/entertainment.json',
'/content/tips.json',
'/content/navigation.json',
// Deck plan images
'/content/deck-plans/Luna-Deck-06-021726.webp',
'/content/deck-plans/Luna-Deck-07-021726.webp',
'/content/deck-plans/Luna_Deck_08_12182025.webp',
'/content/deck-plans/Luna_Deck_09_01202026.webp',
'/content/deck-plans/Luna_Deck_10_01202026.webp',
'/content/deck-plans/Luna_Deck_11_01202026.webp',
'/content/deck-plans/Luna_Deck_12_01202026.webp',
'/content/deck-plans/Luna_Deck_13_01202026.webp',
'/content/deck-plans/Luna_Deck_14_01202026.webp',
'/content/deck-plans/Luna_Deck_15_01202026.webp',
'/content/deck-plans/Luna_Deck_16_01202026.webp',
'/content/deck-plans/Luna-Deck-17-022426.webp',
'/content/deck-plans/Norwegian_Luna_Deck_18_012926.webp',
'/content/deck-plans/Norwegian_Luna_Deck_19_012926.webp',
'/content/deck-plans/Luna_Deck_20_01202026.webp',
];
self.addEventListener('install', event => {
event.waitUntil(
Promise.all([
caches.open(CACHE_NAME).then(cache => cache.addAll(APP_SHELL)),
caches.open(CONTENT_CACHE).then(cache => cache.addAll(CONTENT_FILES)),
]).then(() => self.skipWaiting())
);
});
self.addEventListener('activate', event => {
event.waitUntil(
caches.keys().then(keys =>
Promise.all(
keys
.filter(k => k !== CACHE_NAME && k !== CONTENT_CACHE)
.map(k => caches.delete(k))
)
).then(() => self.clients.claim())
.then(() => self.clients.matchAll().then(clients =>
clients.forEach(c => c.postMessage({ type: 'CONTENT_UPDATED' }))
))
);
});
self.addEventListener('fetch', event => {
const url = new URL(event.request.url);
// Never cache AI endpoint — always go straight to network
if (url.pathname === '/haven-ai') {
event.respondWith(fetch(event.request));
return;
}
// Stale-while-revalidate for content JSON — serve cached immediately, fetch fresh in background
if (url.pathname.startsWith('/content/')) {
event.respondWith(
caches.open(CONTENT_CACHE).then(cache =>
cache.match(event.request).then(cached => {
// Fire background fetch to update cache
const fetchAndUpdate = fetch(event.request).then(res => {
if (res.ok) cache.put(event.request, res.clone());
return res;
});
// Serve cached version immediately if available, else wait for fetch
return cached || fetchAndUpdate;
})
)
);
return;
}
// Cache-first for static assets (icons, fonts cached by browser)
if (url.pathname.startsWith('/icons/') || url.pathname.match(/\.(svg|png|ico)$/)) {
event.respondWith(
caches.match(event.request).then(cached => cached || fetch(event.request))
);
return;
}
// Network-first for app shell
event.respondWith(
fetch(event.request)
.then(res => {
const clone = res.clone();
caches.open(CACHE_NAME).then(cache => cache.put(event.request, clone));
return res;
})
.catch(() => caches.match(event.request).then(cached => cached || caches.match('/index.html')))
);
});
// ─── Background Sync via message ─────────────────────────────────
self.addEventListener('message', event => {
if (event.data && event.data.type === 'SYNC_CONTENT') {
event.waitUntil(
caches.open(CONTENT_CACHE).then(cache =>
Promise.allSettled(
CONTENT_FILES.map(url =>
fetch(url, { cache: 'no-store' }).then(res => {
if (res.ok) cache.put(url, res);
}).catch(() => {}) // silent fail — offline
)
)
)
);
}
});