-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathsw.ts
68 lines (57 loc) · 1.43 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/// <reference lib="es2017" />
/// <reference lib="WebWorker" />
import { ExpirationPlugin } from 'workbox-expiration'
import {
cleanupOutdatedCaches,
precacheAndRoute,
PrecacheFallbackPlugin
} from 'workbox-precaching'
import {
googleFontsCache,
imageCache,
staticResourceCache
} from 'workbox-recipes'
import { registerRoute } from 'workbox-routing'
import { NetworkFirst, StaleWhileRevalidate } from 'workbox-strategies'
// export {}
declare const self: ServiceWorkerGlobalScope
//optionally disable debug logging
// @ts-expect-error - type missing
self.__WB_DISABLE_DEV_LOGS = false
//optionally disable debug logging
// @ts-expect-error - type missing
self.__WB_DISABLE_DEV_LOGS = false
cleanupOutdatedCaches()
precacheAndRoute(self.__WB_MANIFEST)
staticResourceCache()
imageCache()
googleFontsCache()
/**
* Next.js dynamic data (json)
* */
registerRoute(
/\/_next\/data\/.+\/.+\.json$/i,
new StaleWhileRevalidate({
cacheName: 'next-data',
plugins: [
new ExpirationPlugin({
maxEntries: 120,
maxAgeSeconds: 24 * 60 * 60, // 24 hours
purgeOnQuotaError: true
})
]
})
)
registerRoute(
({ request }) => request.mode === 'navigate',
new NetworkFirst({
plugins: [
new PrecacheFallbackPlugin({ fallbackURL: 'offline/offline.html' })
]
})
)
addEventListener('message', (event) => {
if (event.data && event.data.type === 'SKIP_WAITING') {
self.skipWaiting()
}
})