-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
77 lines (73 loc) · 2.75 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
importScripts('https://storage.googleapis.com/workbox-cdn/releases/6.2.0/workbox-sw.js');
workbox.setConfig({
debug: false,
});
//workbox.precaching.precacheAndRoute([{url:'index.html',revision:'2021091504'}]);
// Cache page navigations (html) with a Network First strategy
workbox.routing.registerRoute(
// Check to see if the request is a navigation to a new page
({ request }) => request.mode === 'navigate',
// Use a Network First caching strategy
new workbox.strategies.StaleWhileRevalidate({
// Put all cached files in a cache named 'pages'
cacheName: 'la-cache',
plugins: [
// Ensure that only requests that result in a 200 status are cached
new workbox.cacheableResponse.CacheableResponsePlugin({
statuses: [200],
}),
new workbox.broadcastUpdate.BroadcastUpdatePlugin(),
],
}),
);
// Cache CSS, JS, and Web Worker requests with a Stale While Revalidate strategy
workbox.routing.registerRoute(
// Check to see if the request's destination is style for stylesheets, script for JavaScript, or worker for web worker
({ request }) =>
request.destination === 'style' ||
request.destination === 'script' ||
request.destination === 'font' ||
request.destination === 'manifest' ||
request.destination === 'worker',
// Use a Stale While Revalidate caching strategy
new workbox.strategies.StaleWhileRevalidate({
// Put all cached files in a cache named 'assets'
cacheName: 'mi-cache',
plugins: [
// Ensure that only requests that result in a 200 status are cached
new workbox.cacheableResponse.CacheableResponsePlugin({
statuses: [200],
}),
new workbox.broadcastUpdate.BroadcastUpdatePlugin(),
],
}),
);
// Cache images with a Cache First strategy
workbox.routing.registerRoute(
// Check to see if the request's destination is style for an image
({ request }) => request.destination === 'image',
// Use a Cache First caching strategy
new workbox.strategies.CacheFirst({
// Put all cached files in a cache named 'images'
cacheName: 'image-cache',
plugins: [
// Ensure that only requests that result in a 200 status are cached
new workbox.cacheableResponse.CacheableResponsePlugin({
statuses: [200],
}),
// Don't cache more than 50 items, and expire them after 30 days
new workbox.expiration.ExpirationPlugin({
maxEntries: 50,
maxAgeSeconds: 60 * 60 * 24 * 30, // 30 Days
}),
new workbox.broadcastUpdate.BroadcastUpdatePlugin(),
],
}),
);
workbox.routing.setCatchHandler(async ({ event }) => {
// Return the precached offline page if a document is being requested
if (event.request.destination === 'document') {
return matchPrecache('/index.html');
}
return Response.error();
});