-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsw.js
More file actions
161 lines (135 loc) · 4.25 KB
/
sw.js
File metadata and controls
161 lines (135 loc) · 4.25 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
const CACHE_VERSION = "v2";
const PRECACHE = `precache-${CACHE_VERSION}`;
const RUNTIME = `runtime-${CACHE_VERSION}`;
const OFFLINE_URL = "/offline.html";
const PRECACHE_URLS = [OFFLINE_URL, "/", "/index.html"];
const isDevLike = self.location.hostname === "localhost";
const RUNTIME_MAX_ENTRIES = 200;
self.addEventListener("install", (event) => {
event.waitUntil(
caches.open(PRECACHE).then((cache) => cache.addAll(PRECACHE_URLS))
);
self.skipWaiting();
});
self.addEventListener("activate", (event) => {
event.waitUntil(
(async () => {
// 네비게이션 프리로드 켜기(지원 브라우저)
if ("navigationPreload" in self.registration) {
try {
await self.registration.navigationPreload.enable();
} catch {}
}
const keys = await caches.keys();
await Promise.all(
keys
.filter((k) => k !== PRECACHE && k !== RUNTIME)
.map((k) => caches.delete(k))
);
})()
);
self.clients.claim();
});
const isHTMLRequest = (req) =>
req.mode === "navigate" ||
req.destination === "document" ||
(req.headers.get("accept") || "").includes("text/html");
const shouldBypassForDev = (url) => {
if (!isDevLike) return false;
if (url.origin !== self.location.origin) return false;
return (
url.pathname.startsWith("/@vite") ||
url.pathname.startsWith("/@react-refresh") ||
url.pathname.startsWith("/src/") ||
url.pathname.endsWith("/vite.svg")
);
};
async function trimRuntimeCache() {
const cache = await caches.open(RUNTIME);
const keys = await cache.keys();
if (keys.length > RUNTIME_MAX_ENTRIES) {
await cache.delete(keys[0]);
}
}
async function networkFirstWithOffline(event) {
// navigation preload가 있으면 먼저 사용
try {
if (event.preloadResponse) {
const preloaded = await event.preloadResponse;
if (preloaded) return preloaded;
}
} catch {}
const request = event.request;
const cache = await caches.open(RUNTIME);
try {
const fresh = await fetch(request);
// opaque(크로스오리진)도 캐싱 가능하나 상황에 따라 제외 가능
cache.put(request, fresh.clone());
await trimRuntimeCache();
return fresh;
} catch {
const cached = await caches.match(request, { ignoreSearch: true });
if (cached) return cached;
// 문서 요청이면 오프라인 페이지 대체
const offline = await caches.match(OFFLINE_URL);
if (offline) return offline;
// 최종 안전장치
return new Response("오프라인 상태입니다.", {
status: 503,
headers: { "Content-Type": "text/plain; charset=utf-8" },
});
}
}
self.addEventListener("fetch", (event) => {
const { request } = event;
if (request.method !== "GET") return;
const url = new URL(request.url);
const sameOrigin = url.origin === self.location.origin;
if (isHTMLRequest(request)) {
event.respondWith(networkFirstWithOffline(event));
return;
}
if (request.destination === "image") {
event.respondWith(cacheFirst(request));
return;
}
if (sameOrigin) {
event.respondWith(staleWhileRevalidate(request));
return;
}
event.respondWith(fetch(request).catch(() => caches.match(request)));
});
async function networkFirst(request) {
const cache = await caches.open(RUNTIME);
try {
const fresh = await fetch(request);
cache.put(request, fresh.clone());
return fresh;
} catch (err) {
const cached = await caches.match(request);
if (cached) return cached;
return new Response("오프라인 상태입니다.", {
status: 503,
headers: { "Content-Type": "text/plain; charset=utf-8" },
});
}
}
async function cacheFirst(request) {
const cached = await caches.match(request);
if (cached) return cached;
const cache = await caches.open(RUNTIME);
const res = await fetch(request);
if (res && res.ok) cache.put(request, res.clone());
return res;
}
async function staleWhileRevalidate(request) {
const cache = await caches.open(RUNTIME);
const cached = await caches.match(request);
const networkPromise = fetch(request)
.then((res) => {
if (res && res.ok) cache.put(request, res.clone());
return res;
})
.catch(() => undefined);
return cached || networkPromise || fetch(request).catch(() => cached);
}