forked from rampatra/wedding-website
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathpwa-service-worker.js
110 lines (101 loc) · 2.72 KB
/
pwa-service-worker.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
/* global workbox */
// eslint-disable-next-line
importScripts("https://storage.googleapis.com/workbox-cdn/releases/4.3.1/workbox-sw.js");
const DEBUG = false;
if (workbox) {
if (DEBUG) {
// eslint-disable-next-line no-console
console.log("Yay! Workbox is loaded 🎉");
}
/******************************************
* Workbox main configuration
******************************************/
workbox.setConfig({
debug: DEBUG
});
workbox.core.skipWaiting();
workbox.core.clientsClaim();
/***** End *****/
/******************************************
* Ressource forced cache
******************************************/
//workbox.precaching.precacheAndRoute(FILES_TO_CACHE);
/***** End *****/
/******************************************
* Register the default retention strategy
* NetworkFirst: First from the network then the cache if offline
* Max entries: 256
* Maxe ages: 1 week
* Statuses 0, 200, see opaque responses for more details
******************************************/
var defaultStrategy = new workbox.strategies.NetworkFirst({
cacheName: "default-cache",
plugins: [
new workbox.expiration.Plugin({
maxEntries: 256,
maxAgeSeconds: 7 * 24 * 60 * 60,
purgeOnQuotaError: true
}),
new workbox.cacheableResponse.Plugin({
statuses: [0, 200]
})
]
});
workbox.routing.setDefaultHandler(
(args) => {
if (args.event.request.method === "GET") {
return defaultStrategy.handle(args);
}
return fetch(args.event.request);
}
);
/***** End *****/
/******************************************
* Handle the display of the offline page
******************************************/
workbox.routing.registerRoute(
new workbox.routing.NavigationRoute(({
event
}) => {
return new workbox.strategies.NetworkOnly().handle({
event
})
.catch(() => caches.match(OFFLINE_PAGE));
}));
// workbox.routing.setCatchHandler(({
// event
// }) => {
// switch (event.request.destination) {
// case "document":
// return caches.match(OFFLINE_PAGE);
// default:
// return Response.error();
// }
// });
/***** End *****/
/******************************************
* Declare the differents cache strategy
******************************************/
workbox.routing.registerRoute(
/\.js$/,
new workbox.strategies.StaleWhileRevalidate({
cacheName: "js-cache"
})
);
workbox.routing.registerRoute(
/\.css$/,
new workbox.strategies.StaleWhileRevalidate({
cacheName: "css-cache"
})
);
workbox.routing.registerRoute(
/\.(?:png|jpg|jpeg|svg|gif)$/,
new workbox.strategies.CacheFirst({
cacheName: "image-cache"
})
);
/***** End *****/
} else {
// eslint-disable-next-line no-console
console.error("Boo! Workbox didn't load 😬");
}