SW가 /dashboard fetch → nginx proxy → gateway → 응답 → SW가 다시 fetch → 무한 루프. 프록시 경로는 SW 캐싱에서 제외. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
44 lines
1.3 KiB
JavaScript
44 lines
1.3 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;
|
|
var path = new URL(event.request.url).pathname;
|
|
if (path === '/dashboard' || path === '/login' || path === '/auth' || path.startsWith('/auth/')) 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);
|
|
})
|
|
);
|
|
});
|