Skip to content

Build your personal knowledge base with Trilium Notes

fix(standalone): gate SW asset bypass on capacitor:// scheme, not isDev

Elian Doranb3da91a17346
diff --git a/apps/standalone/src/services/capacitor_http_handler.spec.ts b/apps/standalone/src/services/capacitor_http_handler.spec.ts
index fe3e2020eb..2e8804ebd0 100644
--- a/apps/standalone/src/services/capacitor_http_handler.spec.ts
+++ b/apps/standalone/src/services/capacitor_http_handler.spec.ts
@@ -81,12 +81,15 @@ describe("capacitorHttpHandler", () => {
expect(result.body).toBe('{"ok":true}');
});
- it("re-serializes object response data (CapacitorHttp pre-parses JSON)", async () => {
+ it("passes parsed object response data through via structured clone (no re-serialization)", async () => {
const capacitorHttp = vi.fn().mockResolvedValue({ status: 200, headers: {}, data: { ok: true } });
installCapacitor({ capacitorHttp });
const result = await capacitorHttpHandler({ method: "GET", url: "https://x", headers: {} });
- expect(result.body).toBe(JSON.stringify({ ok: true }));
+ // CapacitorHttp pre-parses JSON; the handler hands the object straight to the worker via
+ // structured clone (no intermediate string) rather than re-serializing it to `body`.
+ expect(result.data).toEqual({ ok: true });
+ expect(result.body).toBeUndefined();
});
it("passes arraybuffer responses through as the raw base64 string", async () => {
diff --git a/apps/standalone/src/sw.spec.ts b/apps/standalone/src/sw.spec.ts
index 85572a1a4f..b29da63141 100644
--- a/apps/standalone/src/sw.spec.ts
+++ b/apps/standalone/src/sw.spec.ts
@@ -143,6 +143,17 @@ describe("fetch routing", () => {
await event._response;
expect(fetch).toHaveBeenCalled();
});
+
+ it("bypasses SW routing on the capacitor:// scheme (assets load via the WebView)", async () => {
+ // On a Capacitor custom scheme the WebView serves assets natively and the SW cannot
+ // fetch() them, so it must not intercept — no respondWith, no SW-issued fetch.
+ vi.stubGlobal("location", { origin, protocol: "capacitor:" });
+ const handlers = await loadSw();
+ const event = fetchEvent(`${origin}/app.js`);
+ handlers.fetch(event);
+ expect(event._response).toBeUndefined();
+ expect(fetch).not.toHaveBeenCalled();
+ });
});
describe("forwardToClientLocalServer", () => {
diff --git a/apps/standalone/src/sw.ts b/apps/standalone/src/sw.ts
index 7c9ffdae60..f2ebfb9b61 100644
--- a/apps/standalone/src/sw.ts
+++ b/apps/standalone/src/sw.ts
@@ -192,10 +192,12 @@ self.addEventListener("fetch", (event) => {
return;
}
- // In dev mode, let the browser handle all non-API requests directly.
- // Custom URL scheme handlers (e.g. capacitor://) are not accessible via
- // fetch() from within a service worker context on iOS WKWebView.
- if (isDev) {
+ // On the Capacitor custom URL scheme (capacitor://) the WebView serves app assets
+ // through its native URLSchemeHandler, which a service worker cannot reach via fetch() —
+ // let those requests fall through to the WebView's own loader. In practice the SW is only
+ // registered on http/https origins (main.ts uses a fetch/XHR interceptor instead of a SW
+ // on capacitor://), so this is a defensive guard rather than a hot path.
+ if (self.location.protocol === "capacitor:") {
return;
}