forked from thisdot/ginger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice-worker.js
More file actions
75 lines (67 loc) · 1.68 KB
/
service-worker.js
File metadata and controls
75 lines (67 loc) · 1.68 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
/* global importScripts workbox */
importScripts(
'https://storage.googleapis.com/workbox-cdn/releases/5.1.2/workbox-sw.js'
);
const { CacheFirst, StaleWhileRevalidate } = workbox.strategies;
const { ExpirationPlugin } = workbox.expiration;
const { precacheAndRoute } = workbox.precaching;
const { registerRoute } = workbox.routing;
const models = [
'gingerhead.json',
'gingerheadband.json',
'gingerheadphones.json',
'gingerlefteye.json',
'gingerrighteye.json',
'gingerteethbot.json',
'gingerteethtop.json',
'gingertongue.json',
];
// TODO: Setup proper pre-caching.
// Cache Google Fonts with a stale-while-revalidate strategy.
registerRoute(
({ url }) =>
url.origin === 'https://fonts.googleapis.com' ||
url.origin === 'https://fonts.gstatic.com',
new StaleWhileRevalidate({
cacheName: 'google-fonts',
plugins: [new ExpirationPlugin({ maxEntries: 20 })],
})
);
registerRoute(
({ request }) =>
request.destination === 'script' || request.destination == 'document',
new StaleWhileRevalidate({
cacheName: 'script-cache',
})
);
registerRoute(
({ request }) => request.destination === 'image',
new CacheFirst({
cacheName: 'image-cache',
plugins: [
new ExpirationPlugin({
maxEntries: 20,
maxAgeSeconds: 7 * 24 * 60 * 60, // 1 week
}),
],
})
);
registerRoute(
({ request }) => {
for (const modelName of models) {
if (request.url.includes(modelName)) {
return true;
}
}
return false;
},
new CacheFirst({
cacheName: 'model-cache',
plugins: [
new ExpirationPlugin({
maxEntries: 20,
maxAgeSeconds: 7 * 24 * 60 * 60, // 1 week
}),
],
})
);