Spaces:
Paused
Paused
Download sw.js from myott/ott-player: direct link, hf CLI and curl.
- Browser
- Download file 4.43 kB
-
https://huggingface.co/spaces/myott/ott-player/resolve/main/sw.js
- Command line
-
hf download hf://spaces/myott/ott-player/sw.js
-
curl -L -o sw.js https://huggingface.co/spaces/myott/ott-player/resolve/main/sw.js
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; | |
| })) | |
| ); | |
| }); | |