-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsw.js
More file actions
150 lines (132 loc) · 4.47 KB
/
sw.js
File metadata and controls
150 lines (132 loc) · 4.47 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
// ------------------------------------------------------------
// CONFIG
// ------------------------------------------------------------
const OFFLINE_MODE_ENABLED = false;
const CACHE_VERSION = '--hash--';
const CACHE_NAME = `PodMail-${CACHE_VERSION}`;
const APP_SHELL = [
'/',
'/?v=--hash--',
'/index.html?v=--hash--',
'/404.html?v=--hash--',
'/auth.html?v=--hash--',
'/css/app.css?v=--hash--',
'/css/supports.css?v=--hash--',
'/js/app.js?v=--hash--',
'/js/esi.js?v=--hash--',
'/js/SimpleESI.js?v=--hash--',
'/js/KeyValues.js?v=--hash--',
'/favicon.ico?v=--hash--',
'/README.md?v=--hash--',
'/img/github.svg?v=--hash--',
'/img/podmail.png?v=--hash--',
'/img/ssologin.png?v=--hash--',
'/img/character.jpg?v=--hash--',
];
// CDN includes only when offline mode is ON
const CDN_SHELL = [
'https://cdn.jsdelivr.net/npm/bootstrap@5.3.7/dist/css/bootstrap.min.css',
'https://cdn.jsdelivr.net/npm/bootstrap@5.3.7/dist/js/bootstrap.bundle.min.js',
'https://cdn.jsdelivr.net/npm/quill@2.0.3/dist/quill.snow.css',
'https://cdn.jsdelivr.net/npm/quill@2.0.3/dist/quill.min.js',
'https://cdn.jsdelivr.net/npm/marked@15.0.6/marked.min.js',
'https://cdn.jsdelivr.net/npm/dompurify@3.2.6/dist/purify.min.js'
];
const NETWORK_TIMEOUT = 3000;
// ------------------------------------------------------------
// INSTALL — only cache when offline mode enabled
// ------------------------------------------------------------
self.addEventListener('install', event => {
self.skipWaiting(); // Always replace previous worker immediately
if (!OFFLINE_MODE_ENABLED) return;
event.waitUntil(
caches.open(CACHE_NAME).then(cache => cache.addAll(APP_SHELL.concat(CDN_SHELL)))
);
});
// ------------------------------------------------------------
// ACTIVATE — always take control AND always delete old caches
// ------------------------------------------------------------
self.addEventListener('activate', event => {
event.waitUntil(
(async () => {
const names = await caches.keys();
await Promise.all(
names.map(name => name !== CACHE_NAME ? caches.delete(name) : Promise.resolve())
);
})()
);
self.clients.claim(); // Take over immediately
});
// ------------------------------------------------------------
// FETCH — completely bypass cache when offline mode is off
// ------------------------------------------------------------
self.addEventListener('fetch', event => {
// HARD DISABLE CACHING MODE
if (!OFFLINE_MODE_ENABLED) {
event.respondWith(fetch(event.request));
return;
}
// Only GET requests can be cached
if (event.request.method !== 'GET') return;
const url = event.request.url;
// ------------------------------------------------------------
// CDN requests — cache-first
// ------------------------------------------------------------
if (url.startsWith('https://cdn.jsdelivr.net')) {
event.respondWith(
caches.match(event.request).then(cached => {
if (cached) return cached;
return fetch(event.request)
.then(res => {
if (res.ok) {
const copy = res.clone();
caches.open(CACHE_NAME).then(c => c.put(event.request, copy));
}
return res;
})
.catch(() => new Response('CDN unavailable', { status: 503 }));
})
);
return;
}
// ------------------------------------------------------------
// Navigation — network-first with timeout and fallback
// ------------------------------------------------------------
if (event.request.mode === 'navigate') {
event.respondWith(
Promise.race([
fetch(event.request)
.then(res => {
if (res.ok) {
const copy = res.clone();
caches.open(CACHE_NAME).then(c => c.put(event.request, copy));
}
return res;
}),
new Promise((_, reject) => setTimeout(reject, NETWORK_TIMEOUT))
]).catch(() =>
caches.match(`/index.html?v=${CACHE_VERSION}`) ||
new Response('Offline', { status: 503 })
)
);
return;
}
// ------------------------------------------------------------
// Static assets — stale-while-revalidate
// ------------------------------------------------------------
event.respondWith(
caches.match(event.request).then(cached => {
const networkFetch = fetch(event.request)
.then(res => {
if (res.ok) {
const copy = res.clone();
caches.open(CACHE_NAME).then(c => c.put(event.request, copy));
}
return res;
})
.catch(() => null);
// Return cached immediately, or wait for network
return cached || networkFetch || new Response('Offline', { status: 503 });
})
);
});