ott-player / sw.js
dongsik22's picture
Claude Opus 4.7 (1M context)
perf: HTML stale-while-revalidate + ์žฌ์ƒ ์ค‘ reload ๋ณด๋ฅ˜
e18993f
Raw History Blame Contribute Delete
4.43 kB
// ์ •์  ์ž์› ์บ์‹œ ์ „๋žต:
// - index.html: stale-while-revalidate. ์บ์‹œ ์ฆ‰์‹œ ๋ฐ˜ํ™˜ โ†’ ๋ถ€ํŒ… 0์ดˆ ์‹œ์ž‘.
// ๋ฐฑ๊ทธ๋ผ์šด๋“œ๋กœ ์ƒˆ HTML fetch ํ›„ ์‘๋‹ต์ด ์บ์‹œ์™€ ๋‹ค๋ฅด๋ฉด ํด๋ผ์ด์–ธํŠธ์— sw-updated ์•Œ๋ฆผ โ†’ ์ž๋™ reload.
// - ๊ทธ ์™ธ ์ •์ (favicon, manifest, sw.js ์ž์ฒด ์ œ์™ธ): cache-first.
// - API / cdn / ์™ธ๋ถ€ ํ˜ธ์ŠคํŠธ: ํ•ญ์ƒ ๋„คํŠธ์›Œํฌ (์บ์‹œ ์šฐํšŒ).
const CACHE = 'tvchak-v8';
self.addEventListener('install', (e) => {
// ์ƒˆ SW๋Š” ์ฆ‰์‹œ ํ™œ์„ฑํ™” (์˜› SW ๋Œ€๊ธฐ ์•ˆ ํ•จ)
self.skipWaiting();
});
self.addEventListener('activate', (e) => {
e.waitUntil((async () => {
// ์˜› ์บ์‹œ ๋ชจ๋‘ ํ๊ธฐ
const keys = await caches.keys();
await Promise.all(keys.filter(k => k !== CACHE).map(k => caches.delete(k)));
// ์ฆ‰์‹œ ๋ชจ๋“  ํด๋ผ์ด์–ธํŠธ take over
await self.clients.claim();
// ์—ด๋ ค์žˆ๋Š” ํƒญ๋“ค์—๊ฒŒ ์ƒˆ SW ํ™œ์„ฑํ™” ์•Œ๋ฆผ โ†’ ์ž๋™ reload ๊ฐ€๋Šฅํ•˜๋„๋ก
const clients = await self.clients.matchAll({ type: 'window' });
for (const c of clients) c.postMessage({ type: 'sw-updated' });
})());
});
// ๋ณธ๋ฌธ ๋ณ€๊ฒฝ ๊ฐ์ง€์šฉ โ€” Cache API์˜ ResponseBody๋Š” ํ•œ ๋ฒˆ๋งŒ ์ฝ์œผ๋ฏ€๋กœ ๋ฏธ๋ฆฌ hash ๊ณ„์‚ฐํ•ด ์ €์žฅ.
// ๊ฐ€๋ฒผ์šด FNV-1a 32bit (HTML 50KB ๋Œ€์ƒ์ด๋ผ ์ถฉ๋ถ„).
async function hashOfResponse(resp) {
try {
const text = await resp.clone().text();
let h = 0x811c9dc5;
for (let i = 0; i < text.length; i++) {
h ^= text.charCodeAt(i);
h = (h + ((h << 1) + (h << 4) + (h << 7) + (h << 8) + (h << 24))) >>> 0;
}
return h.toString(16);
} catch { return null; }
}
// ์บ์‹œ ํ•ญ๋ชฉ์— ์šฐ๋ฆฌ๋งŒ ์“ฐ๋Š” ํ•ด์‹œ ํ—ค๋”๋ฅผ ๋ถ™์—ฌ ์ €์žฅ. ๋‹ค์Œ ๋น„๊ต ๋•Œ ์ด ํ—ค๋”๋ฅผ ํ‚ค๋กœ.
async function storeWithHash(cache, request, resp) {
const hash = await hashOfResponse(resp);
if (hash == null) { await cache.put(request, resp); return; }
const text = await resp.clone().text();
const newHeaders = new Headers(resp.headers);
newHeaders.set('X-Tvchak-Body-Hash', hash);
await cache.put(request, new Response(text, {
status: resp.status,
statusText: resp.statusText,
headers: newHeaders,
}));
}
self.addEventListener('fetch', (e) => {
const url = new URL(e.request.url);
if (e.request.method !== 'GET') return;
// API / cdn / ์™ธ๋ถ€ ํ˜ธ์ŠคํŠธ๋Š” ์บ์‹œ ์šฐํšŒ (ํ•ญ์ƒ ๋„คํŠธ์›Œํฌ)
if (url.pathname.startsWith('/api/') || url.pathname.startsWith('/cdn') || url.origin !== self.location.origin) return;
// HTML(/, /index.html) โ€” stale-while-revalidate
if (url.pathname === '/' || url.pathname === '/index.html') {
e.respondWith((async () => {
const cache = await caches.open(CACHE);
const cached = await cache.match(e.request);
// ๋ฐฑ๊ทธ๋ผ์šด๋“œ๋กœ ๋„คํŠธ์›Œํฌ ๊ฐฑ์‹  (await ์•ˆ ํ•จ โ€” ์บ์‹œ๋Š” ๋ฐ”๋กœ ๋ฐ˜ํ™˜)
const networkPromise = fetch(e.request).then(async (resp) => {
if (!resp || resp.status !== 200) return;
const oldHash = cached?.headers.get('X-Tvchak-Body-Hash');
const newHash = await hashOfResponse(resp);
await storeWithHash(cache, e.request, resp);
// ๋ณธ๋ฌธ์ด ๋‹ฌ๋ผ์กŒ์œผ๋ฉด ํด๋ผ์ด์–ธํŠธ์— ์•Œ๋ฆผ โ€” ์ž๋™ reload ํŠธ๋ฆฌ๊ฑฐ
if (cached && oldHash && newHash && oldHash !== newHash) {
const clients = await self.clients.matchAll({ type: 'window' });
for (const c of clients) c.postMessage({ type: 'sw-updated' });
}
}).catch(() => {});
if (cached) {
// ์บ์‹œ ์žˆ์œผ๋ฉด ์ฆ‰์‹œ ๋ฐ˜ํ™˜, ๋„คํŠธ์›Œํฌ๋Š” ๋ฐฑ๊ทธ๋ผ์šด๋“œ
e.waitUntil(networkPromise);
return cached;
}
// ์บ์‹œ ์—†์œผ๋ฉด(์ฒซ ๋ฐฉ๋ฌธ) ๋„คํŠธ์›Œํฌ ๊ฒฐ๊ณผ ๊ธฐ๋‹ค๋ฆผ
try {
const resp = await fetch(e.request);
if (resp && resp.status === 200) {
await storeWithHash(cache, e.request, resp.clone());
}
return resp;
} catch {
// ์˜คํ”„๋ผ์ธ + ์บ์‹œ ์—†์Œ โ€” ์ตœํ›„ ํด๋ฐฑ
return cache.match('/index.html') || new Response('offline', { status: 503 });
}
})());
return;
}
// ๊ทธ ์™ธ ์ •์ (favicon, manifest ๋“ฑ)์€ cache-first
e.respondWith(
caches.match(e.request).then(hit => hit || fetch(e.request).then(resp => {
if (resp && resp.status === 200 && resp.type === 'basic') {
const clone = resp.clone();
caches.open(CACHE).then(c => c.put(e.request, clone)).catch(() => {});
}
return resp;
}))
);
});