feat(pwa): Network-First 서비스 워커 — 홈화면 앱 자동 갱신

- sw.js: Network-First 캐시 전략 (GET + same-origin + res.ok만 캐시)
- tkfb-core.js: SW 등록 + 업데이트 감지 시 자동 새로고침
  (최초 설치 시 토스트 방지: controller 체크)
- manifest.json: start_url → dashboard-new.html
- nginx: sw.js, manifest.json no-cache 헤더
- 배포 시 sw.js의 APP_VERSION만 변경하면 전 사용자 자동 갱신

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Hyungi Ahn
2026-04-01 15:36:36 +09:00
parent 46a1f8310d
commit 1cfd4da8ba
41 changed files with 88 additions and 58 deletions

View File

@@ -1,7 +1,8 @@
// sw.js - Service Worker 자체 해제 (캐시 초기화)
// 기존 SW를 교체하여 모든 캐시를 삭제하고 자체 해제합니다.
// sw.js — Network-First 서비스 워커
const APP_VERSION = 'v2026040101';
const CACHE_NAME = 'tkfb-' + APP_VERSION;
self.addEventListener('install', function(event) {
self.addEventListener('install', function() {
self.skipWaiting();
});
@@ -9,20 +10,32 @@ self.addEventListener('activate', function(event) {
event.waitUntil(
caches.keys().then(function(keys) {
return Promise.all(
keys.map(function(key) {
console.log('SW: 캐시 삭제:', key);
return caches.delete(key);
})
keys.filter(function(k) { return k !== CACHE_NAME; })
.map(function(k) { return caches.delete(k); })
);
}).then(function() {
console.log('SW: 모든 캐시 삭제 완료, 자체 해제');
return self.registration.unregister();
}).then(function() {
return self.clients.matchAll();
}).then(function(clients) {
clients.forEach(function(client) {
client.postMessage({ type: 'SW_CLEARED' });
});
return self.clients.claim();
})
);
});
self.addEventListener('fetch', function(event) {
// API, POST, cross-origin 요청은 캐싱하지 않음
if (event.request.url.includes('/api/')) return;
if (event.request.method !== 'GET') return;
if (!event.request.url.startsWith(self.location.origin)) return;
event.respondWith(
fetch(event.request)
.then(function(res) {
if (res.ok) {
var clone = res.clone();
caches.open(CACHE_NAME).then(function(c) { c.put(event.request, clone); });
}
return res;
})
.catch(function() {
return caches.match(event.request);
})
);
});