-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice_worker.js.in
71 lines (64 loc) · 2.49 KB
/
service_worker.js.in
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
// SPDX-FileCopyrightText: Copyright 2022-2024 EDF (Électricité de France S.A.)
// SPDX-License-Identifier: BSD-3-Clause
// See README for all details on copyright, authorship and license.
//
// Originally based on Mozilla's PWA examples from
// https://github.com/mdn/pwa-examples/, published under CC0
// Populated by the build process, this ensures there is variation on the
// service worker file, so updates get eventually detected (although they might
// need a manual refresh, or even a double refresh)
const CACHE_NAME = "coap-ace-poc-webapp-cache-BUILDID";
const APP_STATIC_RESOURCES = [
'./',
'./manifest.json',
'./coap-ace-poc-webapp.js',
'./coap-ace-poc-webapp_bg.wasm',
'./icon.svg',
'./style.css',
];
self.addEventListener('install', function(e) {
// Those are cached in here; let's not get browser cache in the way.
//
// In a production environment it may be more suitable to serve files
// from different paths or to serve them with cache-control:
// must-revalidate (both would enable on-path proxies), but here we want
// things to update fast for development.
const headers = new Headers();
headers.set("Cache-Control", "max-age=0");
e.waitUntil(
caches.open(CACHE_NAME).then((cache) =>
cache.addAll(APP_STATIC_RESOURCES.map((f) => new Request(f, {"headers": headers})))
));
// The new service worker can already pass on all the requests (effectively
// that's what it does anyway); by the time it is done here, all files in the
// manifest were already loaded by the application, and will only be accessed
// again on a reload anyway.
self.skipWaiting();
});
// delete old caches on activate
self.addEventListener("activate", (event) => {
event.waitUntil(
(async () => {
const names = await caches.keys();
await Promise.all(
names.map((name) => {
if (name !== CACHE_NAME) {
return caches.delete(name);
}
})
);
await clients.claim();
})()
);
});
self.addEventListener('fetch', (e) => {
e.respondWith(
caches.match(e.request)
// It'd be tempting to leave that out, but if something at installation
// failed and the scripts are not accessible through the service worker, at least they can be fetched now.
//
// Moreover, at least during testing on localhost, this also handles
// requests that are made to the OAuth server and the AS.
.then(function (response) { if (response) { return response } else { return fetch(e.request); }})
);
});