diff --git a/app/public/sw.js b/app/public/sw.js index f3b0cf2..85faec5 100644 --- a/app/public/sw.js +++ b/app/public/sw.js @@ -1,9 +1,9 @@ -// PWA Service Worker v2 -// - 页面导航:网络优先(构建更新后强制拿到新 index.html),离线才回落缓存 -// - 静态资源:缓存优先 -// - /api:直连不缓存 -const CACHE = 'fp-static-v2'; -const CORE = ['/', '/manifest.webmanifest', '/icons/pwa-192.png', '/icons/pwa-512.png']; +// PWA Service Worker v3 +// - /api /auth /admin /sync 一律直连,绝不缓存(修复切换账号后 /auth/me 命中旧缓存的问题) +// - 仅指纹化静态资源(/assets/、/icons/)缓存优先 +// - 页面导航网络优先,离线回落缓存的 index.html +const CACHE = 'fp-static-v3'; +const CORE = ['/manifest.webmanifest', '/icons/pwa-192.png', '/icons/pwa-512.png']; self.addEventListener('install', (e) => { self.skipWaiting(); @@ -20,8 +20,25 @@ self.addEventListener('activate', (e) => { self.addEventListener('fetch', (e) => { const url = new URL(e.request.url); - if (e.request.method !== 'GET' || url.pathname.startsWith('/api')) return; + if (e.request.method !== 'GET') return; + // 后端接口直连(含 /api /auth /admin /sync),绝不缓存 + if (/^\/(api|auth|admin|sync)(\/|$)/.test(url.pathname)) return; + // 指纹化静态资源:缓存优先(内容不可变) + if (url.pathname.startsWith('/assets/') || url.pathname.startsWith('/icons/')) { + e.respondWith( + caches.match(e.request).then((hit) => hit || fetch(e.request).then((res) => { + if (res.ok && url.origin === location.origin) { + const copy = res.clone(); + caches.open(CACHE).then((c) => c.put(e.request, copy)); + } + return res; + })) + ); + return; + } + + // 页面导航:网络优先,离线回落 if (e.request.mode === 'navigate') { e.respondWith( fetch(e.request).then((res) => { @@ -32,14 +49,5 @@ self.addEventListener('fetch', (e) => { ); return; } - - e.respondWith( - caches.match(e.request).then((hit) => hit || fetch(e.request).then((res) => { - if (res.ok && url.origin === location.origin) { - const copy = res.clone(); - caches.open(CACHE).then((c) => c.put(e.request, copy)); - } - return res; - })) - ); + // 其余(manifest 等):网络直连 }); diff --git a/sync-server/src/db.mjs b/sync-server/src/db.mjs index 89dbb9d..ae687fc 100644 --- a/sync-server/src/db.mjs +++ b/sync-server/src/db.mjs @@ -93,6 +93,8 @@ CREATE TABLE IF NOT EXISTS attachments ( used_at TEXT, valid INTEGER NOT NULL DEFAULT 1 );`); + const hasRemark = db.prepare("SELECT COUNT(*) AS c FROM pragma_table_info('invite_codes') WHERE name='remark'").get().c; + if (Number(hasRemark) === 0) db.exec("ALTER TABLE invite_codes ADD COLUMN remark TEXT;"); return db; }