forked from webspace-sdk/webspace-sdk.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webspace.service.js
48 lines (43 loc) · 1.58 KB
/
webspace.service.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
self.addEventListener("install", function(e) {
return e.waitUntil(self.skipWaiting());
});
self.addEventListener("activate", function(e) {
return e.waitUntil(self.clients.claim());
});
const addCoepHeaders = response => {
const headers = new Headers(response.headers);
headers.set("Cross-Origin-Embedder-Policy", "credentialless");
headers.set("Cross-Origin-Opener-Policy", "same-origin");
return new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers
});
};
self.addEventListener("fetch", function(event) {
if (event.request.mode === "navigate") {
event.respondWith(
(async () => {
try {
// First, try to use the navigation preload response if it's supported.
const preloadResponse = await event.preloadResponse;
if (preloadResponse) {
return preloadResponse;
}
// Always try the network first.
return addCoepHeaders(await fetch(event.request));
} catch (error) {
// catch is only triggered if an exception is thrown, which is likely
// due to a network error.
// If fetch() returns a valid HTTP response with a response code in
// the 4xx or 5xx range, the catch() will NOT be called.
console.log("Fetch failed; returning offline page instead.", error);
const offline = { status: 200, statusText: "Worker is offline" };
return new Response("Offline mode is not supported.", offline);
}
})()
);
} else {
event.respondWith(fetch(event.request));
}
});