Repository navigation
Expand file tree
/
Copy pathsw.js
More file actions
181 lines (173 loc) · 4.76 KB
/
Copy pathsw.js
File metadata and controls
181 lines (173 loc) · 4.76 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
const CACHE = 'boardflow-v10';
const CRITICAL = [
'/js/config.js',
'/js/auth/auth.js',
'/js/app.js',
'/js/router.js',
'/js/i18n/i18n.js',
'/sw.js',
'/js/utils/helpers.js',
'/js/utils/dom.js',
'/js/ui/sidebar.js',
'/js/board/board-manager.js',
'/js/templates/template-engine.js',
'/js/templates/template-gallery.js',
'/js/ui/modal.js',
'/js/ui/toast.js',
'/js/ui/context-menu.js',
'/js/components/chat.js',
'/js/ai/ai-assistant.js',
'/js/sharing/share-manager.js',
'/js/sharing/permissions.js',
'/js/components/file-manager.js',
'/js/components/link-card.js',
'/js/components/roadmap.js',
'/js/components/sketch.js',
'/js/components/sticky-note.js',
'/js/components/rich-note.js',
'/js/components/screenshot.js',
'/js/components/media-player.js',
'/js/components/audio-record.js',
'/js/components/video-upload.js',
'/js/board/canvas.js',
'/js/board/item-manager.js',
'/js/board/drag-drop.js',
'/js/board/selection.js',
'/js/board/history.js',
'/js/board/connections.js',
'/js/board/export.js',
'/js/ui/minimap.js',
'/js/utils/storage.js',
'/js/utils/image-utils.js',
'/js/pwa.js'
];
const CRITICAL_SET = new Set(CRITICAL);
const STATIC = [
'/',
'/index.html',
'/manifest.json',
'/assets/icons/favicon.svg',
'/assets/icons/icon-192.svg',
'/assets/icons/icon-512.svg',
'/assets/icons/icon-192.png',
'/assets/icons/icon-512.png',
'/assets/icons/apple-touch-icon.png',
'/js/config.js',
'/js/i18n/i18n.js',
'/js/router.js',
'/js/auth/auth.js',
'/js/auth/login.js',
'/js/auth/signup.js',
'/js/ui/search.js',
'/js/ui/modal.js',
'/js/ui/toast.js',
'/js/board/board-manager.js',
'/js/ui/sidebar.js',
'/js/templates/template-engine.js',
'/js/templates/template-gallery.js',
'/js/utils/helpers.js',
'/js/utils/dom.js',
'/js/ui/context-menu.js',
'/js/components/sticky-note.js',
'/js/components/rich-note.js',
'/js/board/history.js',
'/js/components/sketch.js',
'/js/board/export.js',
'/js/board/canvas.js',
'/js/board/item-manager.js',
'/js/board/drag-drop.js',
'/js/board/selection.js',
'/js/ui/minimap.js',
'/js/utils/storage.js',
'/js/utils/image-utils.js',
'/js/components/screenshot.js',
'/js/components/file-manager.js',
'/js/components/link-card.js',
'/js/components/audio-record.js',
'/js/components/video-upload.js',
'/js/components/media-player.js',
'/js/components/roadmap.js',
'/js/board/connections.js',
'/js/ai/ai-assistant.js',
'/js/sharing/permissions.js',
'/js/sharing/share-manager.js',
'/js/components/chat.js',
'/js/pwa.js',
'/js/app.js'
];
self.addEventListener('install', (e) => {
e.waitUntil(
caches.open(CACHE).then(cache =>
Promise.allSettled(STATIC.map(u => cache.add(u).catch(err => console.warn('SW cache skip', u, err.message))))
)
);
self.skipWaiting();
});
self.addEventListener('message', (e) => {
if (e.data === 'SKIP_WAITING') self.skipWaiting();
});
self.addEventListener('activate', (e) => {
e.waitUntil(
caches.keys().then(keys =>
Promise.all(keys.filter(k => k !== CACHE).map(k => caches.delete(k)))
)
);
self.clients.claim();
});
self.addEventListener('fetch', (e) => {
const url = new URL(e.request.url);
if (url.origin !== self.location.origin) return;
const path = url.pathname;
if (e.request.mode === 'navigate') {
e.respondWith(
fetch(e.request).catch(() => caches.match('/index.html').then(r => r || new Response('Offline', { status: 503 })))
);
return;
}
if (path.startsWith('/js/i18n/locales/')) {
e.respondWith(networkFirst(e.request));
return;
}
if (CRITICAL_SET.has(path)) {
e.respondWith(networkOnlyNoStore(e.request));
return;
}
e.respondWith(cacheFirst(e.request));
});
async function cacheFirst(request) {
const cached = await caches.match(request);
if (cached) return cached;
try {
const resp = await fetch(request);
if (resp.ok) {
const cache = await caches.open(CACHE);
cache.put(request, resp.clone());
}
return resp;
} catch {
return new Response('Offline', { status: 503 });
}
}
async function networkFirst(request) {
try {
const resp = await fetch(request);
if (resp.ok && request.method === 'GET') {
const cache = await caches.open(CACHE);
cache.put(request, resp.clone());
}
return resp;
} catch {
const cached = await caches.match(request);
return cached || new Response('Offline', { status: 503 });
}
}
async function networkOnlyNoStore(request) {
try {
const resp = await fetch(request, { cache: 'no-store' });
// do not cache critical to allow instant updates; still fallback to cache on offline
return resp;
} catch {
const cached = await caches.match(request);
return cached || new Response('Offline', { status: 503 });
}
}