Spaces:
Paused
Paused
pwa: network-first HTML + auto-reload on SW update (cache busting)
Browse files- index.html +22 -3
- sw.js +32 -12
index.html
CHANGED
|
@@ -2765,11 +2765,30 @@ function setupNextCountdownButtons() {
|
|
| 2765 |
});
|
| 2766 |
}
|
| 2767 |
|
| 2768 |
-
// βββ B15: PWA service worker λ±λ‘ βββ
|
| 2769 |
function setupPWA() {
|
| 2770 |
if (!('serviceWorker' in navigator)) return;
|
| 2771 |
-
/
|
| 2772 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2773 |
}
|
| 2774 |
|
| 2775 |
(async function boot() {
|
|
|
|
| 2765 |
});
|
| 2766 |
}
|
| 2767 |
|
| 2768 |
+
// βββ B15: PWA service worker λ±λ‘ + μλ μ
λ°μ΄νΈ βββ
|
| 2769 |
function setupPWA() {
|
| 2770 |
if (!('serviceWorker' in navigator)) return;
|
| 2771 |
+
navigator.serviceWorker.register('/sw.js', { scope: '/' })
|
| 2772 |
+
.then(reg => {
|
| 2773 |
+
// 1μκ°λ§λ€ μ
λ°μ΄νΈ 체ν¬
|
| 2774 |
+
setInterval(() => reg.update().catch(() => {}), 60 * 60 * 1000);
|
| 2775 |
+
})
|
| 2776 |
+
.catch((e) => console.warn('SW reg failed:', e));
|
| 2777 |
+
// μ SWκ° νμ±νλλ©΄ μλ reload (ν λ²λ§)
|
| 2778 |
+
let _reloading = false;
|
| 2779 |
+
navigator.serviceWorker.addEventListener('message', (e) => {
|
| 2780 |
+
if (e.data?.type === 'sw-updated' && !_reloading) {
|
| 2781 |
+
_reloading = true;
|
| 2782 |
+
console.log('[sw] new version activated β reloading');
|
| 2783 |
+
setTimeout(() => location.reload(), 200);
|
| 2784 |
+
}
|
| 2785 |
+
});
|
| 2786 |
+
// controllerchangeλ reload (λ€λ₯Έ νμμ update νΈλ¦¬κ±°λμμ λ)
|
| 2787 |
+
navigator.serviceWorker.addEventListener('controllerchange', () => {
|
| 2788 |
+
if (_reloading) return;
|
| 2789 |
+
_reloading = true;
|
| 2790 |
+
setTimeout(() => location.reload(), 200);
|
| 2791 |
+
});
|
| 2792 |
}
|
| 2793 |
|
| 2794 |
(async function boot() {
|
sw.js
CHANGED
|
@@ -1,33 +1,53 @@
|
|
| 1 |
-
//
|
| 2 |
-
|
| 3 |
-
const
|
| 4 |
|
| 5 |
self.addEventListener('install', (e) => {
|
| 6 |
-
|
| 7 |
self.skipWaiting();
|
| 8 |
});
|
| 9 |
|
| 10 |
self.addEventListener('activate', (e) => {
|
| 11 |
-
e.waitUntil(
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
});
|
| 16 |
|
| 17 |
self.addEventListener('fetch', (e) => {
|
| 18 |
const url = new URL(e.request.url);
|
| 19 |
if (e.request.method !== 'GET') return;
|
| 20 |
-
// API / cdn / μΈλΆ νΈμ€νΈλ μΊμ μ°ν
|
| 21 |
if (url.pathname.startsWith('/api/') || url.pathname.startsWith('/cdn') || url.origin !== self.location.origin) return;
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
e.respondWith(
|
| 24 |
caches.match(e.request).then(hit => hit || fetch(e.request).then(resp => {
|
| 25 |
-
// 200μ΄λ©΄ μΊμμ μ μ₯ (basic auth ν΅κ³Όν μλ΅ ν¬ν¨)
|
| 26 |
if (resp && resp.status === 200 && resp.type === 'basic') {
|
| 27 |
const clone = resp.clone();
|
| 28 |
caches.open(CACHE).then(c => c.put(e.request, clone)).catch(() => {});
|
| 29 |
}
|
| 30 |
return resp;
|
| 31 |
-
})
|
| 32 |
);
|
| 33 |
});
|
|
|
|
| 1 |
+
// μ μ μμλ§ μΊμ. API/cdnμ νμ λ€νΈμν¬.
|
| 2 |
+
// index.htmlμ νμ λ€νΈμν¬ μ°μ (μ½λ μ¦μ λ°μμ©). μ€νλΌμΈμΌ λλ§ μΊμ fallback.
|
| 3 |
+
const CACHE = 'tvchak-v2';
|
| 4 |
|
| 5 |
self.addEventListener('install', (e) => {
|
| 6 |
+
// μ SWλ μ¦μ νμ±ν (μ SW λκΈ° μ ν¨)
|
| 7 |
self.skipWaiting();
|
| 8 |
});
|
| 9 |
|
| 10 |
self.addEventListener('activate', (e) => {
|
| 11 |
+
e.waitUntil((async () => {
|
| 12 |
+
// μ μΊμ λͺ¨λ νκΈ°
|
| 13 |
+
const keys = await caches.keys();
|
| 14 |
+
await Promise.all(keys.filter(k => k !== CACHE).map(k => caches.delete(k)));
|
| 15 |
+
// μ¦μ λͺ¨λ ν΄λΌμ΄μΈνΈ take over
|
| 16 |
+
await self.clients.claim();
|
| 17 |
+
// μ΄λ €μλ νλ€μκ² μ SW νμ±ν μλ¦Ό β μλ reload κ°λ₯νλλ‘
|
| 18 |
+
const clients = await self.clients.matchAll({ type: 'window' });
|
| 19 |
+
for (const c of clients) c.postMessage({ type: 'sw-updated' });
|
| 20 |
+
})());
|
| 21 |
});
|
| 22 |
|
| 23 |
self.addEventListener('fetch', (e) => {
|
| 24 |
const url = new URL(e.request.url);
|
| 25 |
if (e.request.method !== 'GET') return;
|
| 26 |
+
// API / cdn / μΈλΆ νΈμ€νΈλ μΊμ μ°ν (νμ λ€νΈμν¬)
|
| 27 |
if (url.pathname.startsWith('/api/') || url.pathname.startsWith('/cdn') || url.origin !== self.location.origin) return;
|
| 28 |
+
|
| 29 |
+
// HTML(/, /index.html)μ network-first β μ μ½λ μ¦μ λ°μ
|
| 30 |
+
if (url.pathname === '/' || url.pathname === '/index.html') {
|
| 31 |
+
e.respondWith(
|
| 32 |
+
fetch(e.request).then(resp => {
|
| 33 |
+
if (resp && resp.status === 200) {
|
| 34 |
+
const clone = resp.clone();
|
| 35 |
+
caches.open(CACHE).then(c => c.put(e.request, clone)).catch(() => {});
|
| 36 |
+
}
|
| 37 |
+
return resp;
|
| 38 |
+
}).catch(() => caches.match(e.request).then(hit => hit || caches.match('/index.html')))
|
| 39 |
+
);
|
| 40 |
+
return;
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
// κ·Έ μΈ μ μ (favicon, manifest λ±)μ cache-first
|
| 44 |
e.respondWith(
|
| 45 |
caches.match(e.request).then(hit => hit || fetch(e.request).then(resp => {
|
|
|
|
| 46 |
if (resp && resp.status === 200 && resp.type === 'basic') {
|
| 47 |
const clone = resp.clone();
|
| 48 |
caches.open(CACHE).then(c => c.put(e.request, clone)).catch(() => {});
|
| 49 |
}
|
| 50 |
return resp;
|
| 51 |
+
}))
|
| 52 |
);
|
| 53 |
});
|