-
-
Notifications
You must be signed in to change notification settings - Fork 425
Expand file tree
/
Copy pathservice-worker.ts
More file actions
105 lines (96 loc) · 3.04 KB
/
service-worker.ts
File metadata and controls
105 lines (96 loc) · 3.04 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
import {
cleanupOutdatedCaches,
createHandlerBoundToURL,
precacheAndRoute,
} from 'workbox-precaching'
import { clientsClaim } from 'workbox-core'
import { NetworkFirst, StaleWhileRevalidate } from 'workbox-strategies'
import { NavigationRoute, registerRoute } from 'workbox-routing'
import { CacheableResponsePlugin } from 'workbox-cacheable-response'
import { ExpirationPlugin } from 'workbox-expiration'
declare let self: ServiceWorkerGlobalScope
const cacheNames = ['npmx-packages', 'npmx-packages-code-and-docs', 'npmx-vercel-proxies'] as const
async function createRuntimeCaches() {
await Promise.all(cacheNames.map(c => caches.open(c)))
}
self.addEventListener('install', event => {
event.waitUntil(createRuntimeCaches())
})
self.skipWaiting()
clientsClaim()
cleanupOutdatedCaches()
precacheAndRoute(self.__WB_MANIFEST, {
urlManipulation: ({ url }) => {
const urls: URL[] = []
// search use query params, we need to include here any page using query params
if (url.pathname.endsWith('_payload.json') || url.pathname.endsWith('/search')) {
const newUrl = new URL(url.href)
newUrl.search = ''
urls.push(newUrl)
}
return urls
},
})
// allow only fallback in dev: we don't want to cache anything
let allowlist: undefined | RegExp[]
if (import.meta.env.DEV) allowlist = [/^\/$/]
// deny api and server page calls
let denylist: undefined | RegExp[]
if (import.meta.env.PROD) {
denylist = [
// search page
/^\/search$/,
/^\/search\?/,
/^\/~/,
/^\/org\//,
// api calls
/^\/api\//,
/^\/oauth\//,
/^\/package\//,
/^\/package-code\//,
/^\/package-docs\//,
/^\/_v\//,
/^\/opensearch\.xml$/,
// exclude sw: if the user navigates to it, fallback to index.html
/^\/service-worker\.js$/,
]
registerRoute(
({ sameOrigin, url }) =>
sameOrigin &&
(url.pathname.startsWith('/package/') ||
url.pathname.startsWith('/org/') ||
url.pathname.startsWith('/~') ||
url.pathname.startsWith('/api/')),
new NetworkFirst({
cacheName: cacheNames[0],
plugins: [
new CacheableResponsePlugin({ statuses: [200] }),
new ExpirationPlugin({ maxEntries: 1000, maxAgeSeconds: 60 }),
],
}),
)
registerRoute(
({ sameOrigin, url }) =>
sameOrigin &&
(url.pathname.startsWith('/package-docs/') || url.pathname.startsWith('/package-code/')),
new StaleWhileRevalidate({
cacheName: cacheNames[1],
plugins: [
new CacheableResponsePlugin({ statuses: [200] }),
new ExpirationPlugin({ maxEntries: 1000, maxAgeSeconds: 365 * 24 * 60 * 60 }),
],
}),
)
registerRoute(
({ sameOrigin, url }) => sameOrigin && url.pathname.startsWith('/_v/'),
new NetworkFirst({
cacheName: cacheNames[1],
plugins: [
new CacheableResponsePlugin({ statuses: [200] }),
new ExpirationPlugin({ maxEntries: 100, maxAgeSeconds: 60 }),
],
}),
)
}
// to allow work offline
registerRoute(new NavigationRoute(createHandlerBoundToURL('/'), { allowlist, denylist }))