-
Notifications
You must be signed in to change notification settings - Fork 0
/
service-worker.js
92 lines (80 loc) · 3.13 KB
/
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
// Define constants
const CORE_CACHE = 1
const CORE_CACHE_NAME = `core-v${CORE_CACHE}`
// Array of core assets to be cached
const CORE_ASSETS = [
"/manifest.json",
"/offline",
"/css/style.css",
"/images/background_header.webp",
"/static/images/melkmeisje.webp",
"/images/icon-192x192.png",
"/fonts/PannoText-Normal.woff",
"/fonts/PannoText-Normal.woff2",
"/fonts/PannoText-Bold.woff",
"/fonts/PannoText-Bold.woff2"
]
// Install event that is triggered when the service worker is first installed
self.addEventListener('install', (event) => {
console.log("Installed service worker")
event.waitUntil(
// Open the cache and add all the core assets to it
caches.open(CORE_CACHE_NAME)
.then(cache => cache.addAll(CORE_ASSETS))
// Activate the service worker immediately once the core assets have been cached
.then(() => self.skipWaiting())
)
})
//Activate event that is triggered when the service worker is activated
self.addEventListener("activate", (event) => {
console.log("Activating service worker")
// Ensure that the service worker takes control of all pages within its scope
event.waitUntil(clients.claim())
})
// Fetch event that is triggered when a resource/url is fetched
self.addEventListener("fetch", (event) => {
const req = event.request;
console.log("Fetch event:" + req.url);
// Check if the request is a GET request
if (isCoreGetRequest(req)) {
console.log('Core get request: ', req.url);
event.respondWith(
caches.open(CORE_CACHE_NAME)
.then(cache => cache.match(req.url)) // Check if the request is already cached
);
} else if (isHtmlGetRequest(req)) { // Check if the request is a GET request for HTML
console.log('Html get request', req.url);
event.respondWith(
caches.open('html-cache')
.then(cache => cache.match(req.url))
.then(response => response ? response : fetchAndCache(req, 'html-cache'))
.catch(e => {
return caches.open(CORE_CACHE_NAME)
.then(cache => cache.match('/offline'))
})
)
}
});
function fetchAndCache(request, cacheName) {
return fetch(request)
.then(response => {
if (!response.ok) {
throw new TypeError('Bad response status');
}
const clone = response.clone()
caches.open(cacheName).then((cache) => cache.put(request, clone))
return response
})
}
// Checks if a request is a GET request for HTML and if the URL is in the CORE_ASSETS array
function isHtmlGetRequest(request) {
return request.method === 'GET' && (request.headers.get('accept') !== null && request.headers.get('accept').indexOf('text/html') > -1);
}
// Checks if a request is a GET request and if the URL is in the CORE_ASSETS array
function isCoreGetRequest(request) {
return request.method === 'GET' && CORE_ASSETS.includes(getPathName(request.url));
}
function getPathName(requestUrl) {
const url = new URL(requestUrl);
return url.pathname;
}