-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkbox-local.js
63 lines (58 loc) · 2.18 KB
/
workbox-local.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
importScripts('https://storage.googleapis.com/workbox-cdn/releases/5.1.2/workbox-sw.js');
// 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.NetworkFirst({
// Put all cached files in a cache named 'pages'
cacheName: 'pages',
plugins: [
// Ensure that only requests that result in a 200 status are cached
new workbox.cacheableResponse.CacheableResponsePlugin({
statuses: [0, 200],
}),
],
}),
);
// 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 === 'worker',
// Use a Stale While Revalidate caching strategy
new workbox.strategies.StaleWhileRevalidate({
// Put all cached files in a cache named 'assets'
cacheName: 'assets',
plugins: [
// Ensure that only requests that result in a 200 status are cached
new workbox.cacheableResponse.CacheableResponsePlugin({
statuses: [0, 200],
}),
],
}),
);
// 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: 'images',
plugins: [
// Ensure that only requests that result in a 200 status are cached
new workbox.cacheableResponse.CacheableResponsePlugin({
statuses: [0, 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
}),
],
}),
);
workbox.precaching.precacheAndRoute(self.__WB_MANIFEST);