-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
189 lines (168 loc) · 4.83 KB
/
sw.js
File metadata and controls
189 lines (168 loc) · 4.83 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
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
// eslint-disable-next-line no-unused-vars
/* global self, caches, fetch, importScripts, workbox, Response */
/* eslint-disable indent */
import { CacheableResponsePlugin } from 'workbox-cacheable-response'
import { cacheNames, clientsClaim } from 'workbox-core'
import { ExpirationPlugin } from 'workbox-expiration'
import { initialize as googleAnalyticsInitialize } from 'workbox-google-analytics'
import {
cleanupOutdatedCaches,
createHandlerBoundToURL,
matchPrecache,
precache
} from 'workbox-precaching'
import { NavigationRoute, registerRoute, setCatchHandler } from 'workbox-routing'
import { CacheFirst, NetworkFirst, NetworkOnly, StaleWhileRevalidate } from 'workbox-strategies'
// network timeout for network first strategies - if not response in this time, serve from cache
const NETWORK_TIMEOUT_SECONDS = 0.5
// default html document to serve
const SINGLE_PAGE_URL = '/index.html'
precache([
SINGLE_PAGE_URL,
'/js/scripts.js',
'/css/main.css'
], {
ignoreURLParametersMatching: true
})
cleanupOutdatedCaches()
const handler = createHandlerBoundToURL(SINGLE_PAGE_URL)
const navigationRoute = new NavigationRoute(handler, {
denylist: [/\/api\//]
})
registerRoute(navigationRoute)
// Catch routing errors, like if the user is offline
setCatchHandler(async ({ event }) => {
// Return the precached offline page if a document is being requested
if (event.request.destination === 'document') {
return matchPrecache(SINGLE_PAGE_URL)
}
return Response.error()
})
// the primary resources that are precached
registerRoute(
/\/(js\/scripts\.js|css\/main\.css)$/,
new NetworkFirst({
cacheName: cacheNames.precache,
networkTimeoutSeconds: NETWORK_TIMEOUT_SECONDS
})
)
// nomenclature data
registerRoute(
/^.*\/api\/(staging\/)?(i18n|locations|nomenclature|organization|species|user|zone|visit)/,
new StaleWhileRevalidate({
cacheName: 'api-cache'
})
)
// every other api request
registerRoute(
/^.*\/api\//,
new NetworkOnly()
)
registerRoute(
/\.json$/,
new NetworkFirst({
cacheName: 'stats-cache',
networkTimeoutSeconds: NETWORK_TIMEOUT_SECONDS
})
)
registerRoute(
// Cache HTML views files.
/\/views\/.*\.html$/,
// Use cache but update in the background.
new StaleWhileRevalidate({
// Use a custom cache name.
cacheName: 'view-cache'
})
)
registerRoute(
// Cache image files.
/\/img\/.*\.(?:png|jpg|jpeg|svg|gif)$/,
// Use the cache if it's available.
new CacheFirst({
// Use a custom cache name.
cacheName: 'image-cache',
plugins: [
new ExpirationPlugin({
// Cache only 20 images.
maxEntries: 20,
// Cache for a maximum of a week.
maxAgeSeconds: 7 * 24 * 60 * 60,
// Purge this cache if storage quota has exceeded
purgeOnQuotaError: true
})
]
})
)
registerRoute(
// Cache CSS files.
/\.css$/,
// Use cache but update in the background.
new StaleWhileRevalidate({
// Use a custom cache name.
cacheName: 'css-cache'
})
)
registerRoute(
// Cache fonts.
/\/fonts\//,
// Use cache but update in the background.
new StaleWhileRevalidate({
// Use a custom cache name.
cacheName: 'font-cache',
plugins: [
new ExpirationPlugin({
// Cache for a maximum of a year.
maxAgeSeconds: 365 * 24 * 60 * 60
})
]
})
)
// Cache the Google Fonts stylesheets with a stale-while-revalidate strategy.
registerRoute(
/^https:\/\/fonts\.googleapis\.com/,
new StaleWhileRevalidate({
cacheName: 'google-fonts-stylesheets'
})
)
// Cache the underlying font files with a cache-first strategy for 1 year.
registerRoute(
/^https:\/\/fonts\.gstatic\.com/,
new CacheFirst({
cacheName: 'google-fonts-webfonts',
plugins: [
new CacheableResponsePlugin({
statuses: [0, 200]
}),
new ExpirationPlugin({
maxAgeSeconds: 60 * 60 * 24 * 365,
maxEntries: 30,
purgeOnQuotaError: true
})
]
})
)
googleAnalyticsInitialize()
registerRoute(
/^https:\/\/maps\.googleapis\.com/,
new StaleWhileRevalidate({
cacheName: 'gmaps-cache'
})
)
registerRoute(
/^https:\/\/maps\.gstatic\.com/,
new CacheFirst({
cacheName: 'gmaps-static-cache',
plugins: [
new CacheableResponsePlugin({
statuses: [0, 200]
}),
new ExpirationPlugin({
maxAgeSeconds: 60 * 60 * 24 * 365,
maxEntries: 30,
purgeOnQuotaError: true
})
]
})
)
self.skipWaiting()
clientsClaim()