From aeaa9b665106c754b094081df61d92c1e08244ef Mon Sep 17 00:00:00 2001 From: AdingApkgg Date: Sat, 27 Dec 2025 03:37:41 +0800 Subject: [PATCH] Refactor service worker fetch event handling and remove unused caching logic - Simplified fetch event logic in sw.js to only process same-origin GET requests, improving clarity and performance. - Consolidated caching strategies for documents and static resources, ensuring a more efficient response handling. - Removed the unused cacheFirstCrossOrigin function to streamline the service worker code. --- public/sw.js | 42 +++++++++++------------------------------- 1 file changed, 11 insertions(+), 31 deletions(-) diff --git a/public/sw.js b/public/sw.js index f531a46..db17593 100644 --- a/public/sw.js +++ b/public/sw.js @@ -64,32 +64,26 @@ self.addEventListener('fetch', (e) => { const { request } = e const url = new URL(request.url) - // 跳过:非 GET、非 HTTP、永不缓存、跨域图片 + // 只处理同源 GET 请求,跨域请求全部跳过(避免 CORS 问题) if ( request.method !== 'GET' || !url.protocol.startsWith('http') || - NO_CACHE.test(url.href) || - (url.origin !== location.origin && request.destination === 'image') + url.origin !== location.origin || + NO_CACHE.test(url.href) ) return - const isSameOrigin = url.origin === location.origin const isDocument = request.destination === 'document' || url.pathname === '/' || url.pathname.endsWith('.html') const isCacheable = CACHEABLE_EXTS.test(url.pathname) || CACHEABLE_PATHS.test(url.pathname) - if (isSameOrigin) { - if (isDocument) { - // HTML:网络优先,离线返回提示页 - e.respondWith(networkFirstWithOffline(request)) - } else if (isCacheable) { - // 静态资源:缓存优先 - e.respondWith(cacheFirst(request)) - } else { - // 其他:网络优先 - e.respondWith(networkFirst(request)) - } + if (isDocument) { + // HTML:网络优先,离线返回提示页 + e.respondWith(networkFirstWithOffline(request)) } else if (isCacheable) { - // 跨域可缓存资源 - e.respondWith(cacheFirstCrossOrigin(request)) + // 静态资源:缓存优先 + e.respondWith(cacheFirst(request)) + } else { + // 其他:网络优先 + e.respondWith(networkFirst(request)) } }) @@ -133,20 +127,6 @@ async function networkFirstWithOffline(req) { } } -async function cacheFirstCrossOrigin(req) { - const cached = await caches.match(req) - if (cached) return cached - try { - const res = await fetch(req, { mode: 'cors', credentials: 'omit' }) - if (res.ok && res.type !== 'opaque') { - const cache = await caches.open(CACHE_NAME) - cache.put(req, res.clone()) - } - return res - } catch { - return new Response('', { status: 503 }) - } -} // ============================================ // 离线页面