Repository navigation
Expand file tree
/
Copy pathsw.js
More file actions
56 lines (53 loc) · 1.39 KB
/
Copy pathsw.js
File metadata and controls
56 lines (53 loc) · 1.39 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
const CACHE_NAME = "big-clock-v7";
const ASSETS = [
"./",
"./index.html",
"./style.css",
"./app.js",
"./manifest.json",
"./icons/icon-192.png",
"./icons/icon-512.png",
];
self.addEventListener("install", function (event) {
event.waitUntil(
caches.open(CACHE_NAME).then(function (cache) {
return cache.addAll(ASSETS);
})
);
self.skipWaiting();
});
self.addEventListener("activate", function (event) {
event.waitUntil(
caches.keys().then(function (keys) {
return Promise.all(
keys
.filter(function (key) {
return key !== CACHE_NAME;
})
.map(function (key) {
return caches.delete(key);
})
);
})
);
self.clients.claim();
});
// Network-First 전략: 온라인일 때는 항상 최신 파일 즉시 반영, 오프라인일 때는 캐시 활용
self.addEventListener("fetch", function (event) {
if (event.request.method !== "GET") return;
event.respondWith(
fetch(event.request)
.then(function (response) {
if (response.ok && event.request.url.startsWith(self.location.origin)) {
const clone = response.clone();
caches.open(CACHE_NAME).then(function (cache) {
cache.put(event.request, clone);
});
}
return response;
})
.catch(function () {
return caches.match(event.request);
})
);
});