/
sw.ts
54 lines (46 loc) · 1.72 KB
/
sw.ts
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
import { cleanupOutdatedCaches, createHandlerBoundToURL, precacheAndRoute } from 'workbox-precaching';
import { NavigationRoute, registerRoute } from 'workbox-routing';
import { CacheFirst, StaleWhileRevalidate } from 'workbox-strategies';
import { CacheableResponsePlugin } from 'workbox-cacheable-response';
import { ExpirationPlugin } from 'workbox-expiration';
declare let self: ServiceWorkerGlobalScope;
const CACHE_NAME_PREFIX = 'BingAI';
self.addEventListener('message', (event) => {
if (event.data && event.data.type === 'SKIP_WAITING') {
self.skipWaiting();
}
});
// self.__WB_MANIFEST is default injection point
precacheAndRoute(self.__WB_MANIFEST);
// clean old assets
cleanupOutdatedCaches();
// to allow work offline
registerRoute(new NavigationRoute(createHandlerBoundToURL('./index.html')));
registerRoute(
({ request, url }) => {
return request.destination === 'style' || request.destination === 'manifest' || request.destination === 'script' || request.destination === 'worker';
},
new StaleWhileRevalidate({
cacheName: `${CACHE_NAME_PREFIX}-assets`,
plugins: [new CacheableResponsePlugin({ statuses: [200] })],
})
);
registerRoute(
({ request, url }) => {
if (url.pathname.includes('hm.gif') || url.pathname.includes('/fd/ls/')) {
return false;
}
return request.destination === 'image';
},
new CacheFirst({
cacheName: `${CACHE_NAME_PREFIX}-images`,
plugins: [
new CacheableResponsePlugin({ statuses: [200] }),
// 100 entries max, 30 days max
new ExpirationPlugin({ maxEntries: 100, maxAgeSeconds: 60 * 60 * 24 * 30 }),
],
})
);
self.addEventListener('install', (ev) => {
self.skipWaiting();
});