web:service worker 不再拦截导航(修 Safari OAuth 回跳 ~2min 卡顿)
SW 之前以 network-first 接管所有导航请求,疑似在 Safari 下令 Casdoor→应用的回跳导航长时间挂起(~2min 似卡在 Casdoor 页)。改为:导航、/api、跨源一律直通网络,SW 只对同源 hash 化静态资源做 stale-while-revalidate。cdrop 离线无意义,放弃导航壳缓存无损失。cache 版本 bump v2 清旧缓存。
This commit is contained in:
+20
-43
@@ -1,32 +1,24 @@
|
|||||||
// cdrop service worker — makes the web app installable and caches the static
|
// cdrop service worker — makes the web app installable and caches hashed static
|
||||||
// shell for instant launch.
|
// assets for faster repeat loads.
|
||||||
//
|
//
|
||||||
// HARD RULE: it MUST NOT intercept /api/* . The SSE stream (/api/hub/events),
|
// Deliberately minimal. It does NOT intercept:
|
||||||
// clipboard, WebRTC signaling and relay all need a live, unbuffered network
|
// - /api/* — SSE (/api/hub/events), clipboard, signaling, relay need a
|
||||||
// connection — those requests are passed straight through to the network. Same
|
// live, unbuffered connection; and the OAuth exchange/refresh
|
||||||
// goes for cross-origin requests (fonts / CDN): the browser handles them.
|
// must not be cached.
|
||||||
|
// - navigations — page loads / OAuth redirects go straight to the network.
|
||||||
|
// (A SW that mediates navigations was stalling the Casdoor →
|
||||||
|
// app redirect for ~2 min in Safari; cdrop is useless offline
|
||||||
|
// anyway, so there's nothing to gain by caching the shell.)
|
||||||
|
// - cross-origin — fonts / CDN are left to the browser.
|
||||||
//
|
//
|
||||||
// cdrop is useless offline (everything needs the backend), so this is a shell
|
// What's left: hashed JS/CSS/image assets, served stale-while-revalidate.
|
||||||
// cache for fast loads + installability, not a real offline app.
|
|
||||||
|
|
||||||
const CACHE = "cdrop-shell-v1";
|
const CACHE = "cdrop-assets-v2";
|
||||||
const SHELL = [
|
|
||||||
"/",
|
|
||||||
"/site.webmanifest",
|
|
||||||
"/favicon-96.png",
|
|
||||||
"/icon-192.png",
|
|
||||||
"/apple-touch-icon-180.png",
|
|
||||||
"/logo-mark.png",
|
|
||||||
];
|
|
||||||
|
|
||||||
self.addEventListener("install", (event) =>
|
self.addEventListener("install", (event) =>
|
||||||
{
|
{
|
||||||
event.waitUntil(
|
// No precache — assets are cached lazily on first fetch. Activate immediately.
|
||||||
caches.open(CACHE)
|
event.waitUntil(self.skipWaiting());
|
||||||
.then((cache) => cache.addAll(SHELL))
|
|
||||||
.then(() => self.skipWaiting())
|
|
||||||
.catch(() => self.skipWaiting()),
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
self.addEventListener("activate", (event) =>
|
self.addEventListener("activate", (event) =>
|
||||||
@@ -42,29 +34,14 @@ self.addEventListener("fetch", (event) =>
|
|||||||
{
|
{
|
||||||
const req = event.request;
|
const req = event.request;
|
||||||
if (req.method !== "GET") { return; }
|
if (req.method !== "GET") { return; }
|
||||||
|
if (req.mode === "navigate") { return; } // page loads / OAuth redirects → network
|
||||||
|
|
||||||
const url = new URL(req.url);
|
const url = new URL(req.url);
|
||||||
if (url.origin !== self.location.origin) { return; } // cross-origin (fonts/CDN) → browser default
|
if (url.origin !== self.location.origin) { return; } // cross-origin (fonts/CDN) → browser default
|
||||||
if (url.pathname.startsWith("/api/")) { return; } // live backend → never intercept
|
if (url.pathname.startsWith("/api/")) { return; } // live backend → never intercept
|
||||||
|
|
||||||
// SPA navigations: network-first so a new deploy serves fresh HTML (which
|
// Static assets (Vite emits content-hashed, immutable files): serve cache
|
||||||
// points at the new hashed assets); fall back to the cached shell offline.
|
// immediately, refresh in the background.
|
||||||
if (req.mode === "navigate")
|
|
||||||
{
|
|
||||||
event.respondWith(
|
|
||||||
fetch(req)
|
|
||||||
.then((resp) =>
|
|
||||||
{
|
|
||||||
const copy = resp.clone();
|
|
||||||
caches.open(CACHE).then((c) => c.put("/", copy)).catch(() => {});
|
|
||||||
return resp;
|
|
||||||
})
|
|
||||||
.catch(() => caches.match("/").then((r) => r || Response.error())),
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Static assets (hashed JS/CSS/images are immutable): stale-while-revalidate.
|
|
||||||
event.respondWith(
|
event.respondWith(
|
||||||
caches.match(req).then((cached) =>
|
caches.match(req).then((cached) =>
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user