-
Notifications
You must be signed in to change notification settings - Fork 19
/
sw.js
195 lines (181 loc) · 5.24 KB
/
sw.js
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
if (typeof importScripts === 'function') {
// eslint-disable-next-line no-undef
importScripts(
'https://storage.googleapis.com/workbox-cdn/releases/5.0.0/workbox-sw.js'
)
/* global workbox */
if (workbox) {
workbox.core.skipWaiting()
workbox.core.clientsClaim()
/* injection point for manifest files. */
const WB_MANIFEST = self.__WB_MANIFEST
// precache fallback route
WB_MANIFEST.push({ url: '/offline', revision: '12345678' })
workbox.precaching.precacheAndRoute(WB_MANIFEST)
workbox.precaching.cleanupOutdatedCaches()
// Start URL
workbox.routing.registerRoute(
'/',
new workbox.strategies.NetworkFirst({
cacheName: 'start-url',
plugins: [
new workbox.expiration.ExpirationPlugin({
maxEntries: 1,
maxAgeSeconds: 24 * 60 * 60, // 24 hours
}),
],
}),
'GET'
)
// Self-hosted Fonts
workbox.routing.registerRoute(
/\/fonts\/.*$/i,
new workbox.strategies.CacheFirst({
cacheName: 'fonts',
plugins: [
new workbox.cacheableResponse.CacheableResponsePlugin({
statuses: [0, 200],
}),
new workbox.expiration.ExpirationPlugin({
maxEntries: 4,
maxAgeSeconds: 365 * 24 * 60 * 60, // 365 days
}),
],
}),
'GET'
)
//For images
workbox.routing.registerRoute(
/\.(?:jpg|jpeg|gif|png|svg|ico|webp)$/i,
new workbox.strategies.StaleWhileRevalidate({
cacheName: 'image-caches',
plugins: [
new workbox.cacheableResponse.CacheableResponsePlugin({
statuses: [0, 200],
}),
new workbox.expiration.ExpirationPlugin({
maxEntries: 20,
maxAgeSeconds: 24 * 60 * 60, // 24 hours
}),
],
}),
'GET'
)
// JS Files
workbox.routing.registerRoute(
/\.(?:js)$/i,
new workbox.strategies.StaleWhileRevalidate({
cacheName: 'js-caches',
plugins: [
new workbox.cacheableResponse.CacheableResponsePlugin({
statuses: [0, 200],
}),
new workbox.expiration.ExpirationPlugin({
maxEntries: 20,
maxAgeSeconds: 12 * 60 * 60, // 12 hours
}),
],
}),
'GET'
)
// CSS Files
workbox.routing.registerRoute(
/\.(?:css)$/i,
new workbox.strategies.StaleWhileRevalidate({
cacheName: 'style-caches',
plugins: [
new workbox.cacheableResponse.CacheableResponsePlugin({
statuses: [0, 200],
}),
new workbox.expiration.ExpirationPlugin({
maxEntries: 20,
maxAgeSeconds: 24 * 60 * 60, // 24 hours
}),
],
}),
'GET'
)
// JSON, XML, CSV FILES
workbox.routing.registerRoute(
/\.(?:json|xml|csv)$/i,
new workbox.strategies.NetworkFirst({
cacheName: 'data-caches',
plugins: [
new workbox.cacheableResponse.CacheableResponsePlugin({
statuses: [0, 200],
}),
new workbox.expiration.ExpirationPlugin({
maxEntries: 20,
maxAgeSeconds: 24 * 60 * 60, // 24 hours
}),
],
}),
'GET'
)
// API Routes
workbox.routing.registerRoute(
/\/api\/.*$/i,
new workbox.strategies.NetworkFirst({
cacheName: 'api-caches',
plugins: [
new workbox.cacheableResponse.CacheableResponsePlugin({
statuses: [0, 200],
}),
new workbox.expiration.ExpirationPlugin({
maxEntries: 10,
maxAgeSeconds: 24 * 60 * 60, // 24 hours
}),
],
}),
'GET'
)
// Others
workbox.routing.registerRoute(
/.*/i,
new workbox.strategies.NetworkFirst({
cacheName: 'others',
plugins: [
new workbox.cacheableResponse.CacheableResponsePlugin({
statuses: [0, 200],
}),
new workbox.expiration.ExpirationPlugin({
maxEntries: 20,
maxAgeSeconds: 24 * 60 * 60, // 24 hours
}),
],
}),
'GET'
)
// Register 'default'
const defaultStrategy = new workbox.strategies.StaleWhileRevalidate({
cacheName: 'default',
plugins: [
new workbox.cacheableResponse.CacheableResponsePlugin({
statuses: [0, 200],
}),
new workbox.expiration.ExpirationPlugin({
maxEntries: 128,
maxAgeSeconds: 7 * 24 * 60 * 60, // 1 week
purgeOnQuotaError: true, // Opt-in to automatic cleanup
}),
],
})
// Use a stale-while-revalidate strategy for all other requests.
workbox.routing.setDefaultHandler((args) => {
if (args.event.request.method === 'GET') {
return defaultStrategy.handle(args)
}
return fetch(args.event.request)
})
// Fallback page
workbox.routing.setCatchHandler(async ({ event }) => {
if (event.request.destination === 'document') {
return workbox.precaching.matchPrecache('/offline')
}
return Response.error()
})
} else {
/* eslint-disable no-console */
console.log('Workbox could not be loaded. No Offline support')
}
}