-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
126 lines (112 loc) · 3.08 KB
/
sw.js
File metadata and controls
126 lines (112 loc) · 3.08 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// NOTE: keep service worker at project root to control the full site scope (`./`).
const CACHE_NAME = "hediynailart-cache-v3";
const APP_SHELL = [
"./",
"./index.html",
"./manifest.webmanifest",
"./assets/css/base.css",
"./assets/css/layout.css",
"./assets/css/components.css",
"./assets/css/footer.css",
"./assets/js/theme-bootstrap.js",
"./assets/js/main.js",
"./assets/js/config.js",
"./assets/js/dom.js",
"./assets/js/state.js",
"./assets/js/ui.js",
"./assets/js/theme.js",
"./assets/js/pwa.js",
"./assets/js/date-utils.js",
"./assets/js/booking.js",
"./assets/js/gallery.js",
"./assets/images/favicon-32.png",
"./assets/images/favicon-192.png",
"./assets/images/favicon-512.png",
"./assets/images/apple-touch-icon-180.png",
"./assets/images/nail-logo.png",
];
self.addEventListener("install", (event) => {
event.waitUntil(
caches
.open(CACHE_NAME)
.then((cache) => cache.addAll(APP_SHELL))
.then(() => self.skipWaiting()),
);
});
self.addEventListener("activate", (event) => {
event.waitUntil(
caches
.keys()
.then((keys) =>
Promise.all(
keys
.filter((key) => key !== CACHE_NAME)
.map((key) => caches.delete(key)),
),
)
.then(() => self.clients.claim()),
);
});
function shouldCacheRequest(request) {
return ["style", "script", "image", "font", "manifest"].includes(
request.destination,
);
}
async function staleWhileRevalidate(request) {
const cache = await caches.open(CACHE_NAME);
const cached =
(await cache.match(request)) ||
(await cache.match(request, { ignoreSearch: true }));
const networkFetch = fetch(request)
.then((response) => {
if (response && (response.ok || response.type === "opaque")) {
cache.put(request, response.clone());
}
return response;
})
.catch(() => null);
return cached || (await networkFetch) || Response.error();
}
async function networkFirstPage(request) {
const cache = await caches.open(CACHE_NAME);
try {
const response = await fetch(request);
if (response && response.ok) {
cache.put(request, response.clone());
}
return response;
} catch {
return (
(await cache.match(request)) ||
(await cache.match("./index.html")) ||
Response.error()
);
}
}
self.addEventListener("fetch", (event) => {
const { request } = event;
if (request.method !== "GET") {
return;
}
const url = new URL(request.url);
if (request.mode === "navigate") {
event.respondWith(networkFirstPage(request));
return;
}
if (url.origin !== self.location.origin) {
if (request.destination === "image" || request.destination === "font") {
event.respondWith(staleWhileRevalidate(request));
}
return;
}
if (shouldCacheRequest(request)) {
event.respondWith(staleWhileRevalidate(request));
return;
}
event.respondWith(
fetch(request).catch(async () => {
const cache = await caches.open(CACHE_NAME);
return cache.match(request) || cache.match(request, { ignoreSearch: true });
}),
);
});