- 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>
42 lines
1.1 KiB
JavaScript
42 lines
1.1 KiB
JavaScript
// sw.js — Network-First 서비스 워커
|
|
const APP_VERSION = 'v2026040101';
|
|
const CACHE_NAME = 'tkfb-' + APP_VERSION;
|
|
|
|
self.addEventListener('install', function() {
|
|
self.skipWaiting();
|
|
});
|
|
|
|
self.addEventListener('activate', function(event) {
|
|
event.waitUntil(
|
|
caches.keys().then(function(keys) {
|
|
return Promise.all(
|
|
keys.filter(function(k) { return k !== CACHE_NAME; })
|
|
.map(function(k) { return caches.delete(k); })
|
|
);
|
|
}).then(function() {
|
|
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);
|
|
})
|
|
);
|
|
});
|