-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathserviceworker.js
40 lines (33 loc) · 1.21 KB
/
serviceworker.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
const CACHE_NAME = "pwa-assets";
const urlsToCache = ["/", "about.html", "styles.css", "js/app.js", "js/handlers.js",
"data/activities.json", "https://fonts.gstatic.com/s/materialicons/v67/flUhRq6tzZclQEJ-Vdg-IuiaDsNcIhQ8tQ.woff2"];
self.addEventListener("install", event => {
// Pre-cache the assets
event.waitUntil(
caches.open(CACHE_NAME).then(cache => {
return cache.addAll(urlsToCache);
})
);
})
// Cache First Policy
self.addEventListener("fetch", event => {
event.respondWith(
caches.match(event.request) // searching in the cache
.then(function(response) {
if (response) { // CACHE HIT
// The request is in the cache
return response;
} else { // CACHE MISS
// We need to go to the network
return fetch(event.request);
}
})
);
});
// PWA is requesting a file from the network
// self.addEventListener("fetch", event => {
// const request = event.request;
// console.log(request.url);
// const response = new Response(`URL requested: ${request.url}`);
// event.respondWith(response);
// })