Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
perf(standalone): answer API calls from the tab that owns the worker
Elian Doran0984df34ce85
diff --git a/apps/client/src/index.ts b/apps/client/src/index.tsindex beb2f2610d..86565b7bc7 100644--- a/apps/client/src/index.ts+++ b/apps/client/src/index.ts@@ -41,8 +41,18 @@ async function initJQuery() {}async function setupGlob() {- const response = await fetch(`./bootstrap${window.location.search}`);+ const url = `./bootstrap${window.location.search}`;+ // The standalone tab that owns the SQLite worker answers this itself; every other build (and a+ // follower tab, which has no worker) fetches, on standalone through the service worker.+ const localFetch = window.standaloneApi?.localFetch;+ const startedAt = performance.now();+ const response = localFetch ? await localFetch(new Request(url)) : await fetch(url);const json = await response.json();+ if (import.meta.env.DEV && localFetch) {+ // The worker answers this one only once it has finished starting up, so the time it+ // reports is mostly that wait rather than the request.+ console.debug(`[api] GET bootstrap ${(performance.now() - startedAt).toFixed(1)}ms`);+ }window.global = globalThis; /* fixes https://github.com/webpack/webpack/issues/10035 */window.glob = {diff --git a/apps/client/src/services/server.ts b/apps/client/src/services/server.tsindex 5590cebe8e..ccd048cc19 100644--- a/apps/client/src/services/server.ts+++ b/apps/client/src/services/server.ts@@ -188,6 +188,16 @@ interface CallOptions {*/const DEFAULT_TIMEOUT = 60000;+/**+ * Whether a standalone tab that owns the SQLite worker answers its own API calls, rather than+ * letting the XHR below reach that same worker through the service worker's forwarding.+ *+ * Build with `VITE_DISABLE_LOCAL_FETCH=true` to take the service worker's route instead, which is+ * what compares the two transports. An unset variable keeps the direct path, so a build that does+ * not name the variable still gets it.+ */+const USE_LOCAL_FETCH = import.meta.env.VITE_DISABLE_LOCAL_FETCH !== "true";+async function call<T>(method: string, url: string, componentId?: string, options: CallOptions = {}) {const headers = await getHeaders({"trilium-component-id": componentId@@ -210,6 +220,14 @@ async function call<T>(method: string, url: string, componentId?: string, option}function ajax(url: string, method: string, data: unknown, headers: Headers, opts: CallOptions): Promise<Response> {+ // The standalone leader tab defines `standaloneApi.localFetch`, which the in-page SQLite+ // worker answers directly — the XHR below would reach the same worker through the service+ // worker's forwarding. Follower tabs and the other builds leave it unset.+ const localFetch = USE_LOCAL_FETCH ? window.standaloneApi?.localFetch : undefined;+ if (localFetch) {+ return localAjax(localFetch, url, method, data, headers, opts);+ }+return new Promise((res, rej) => {const options: JQueryAjaxSettings = {url: window.glob.baseApiUrl + url,@@ -294,6 +312,98 @@ function ajax(url: string, method: string, data: unknown, headers: Headers, opts});}+/**+ * The `ajax()` fast path for the standalone leader tab: the same request shape, response contract+ * and error handling, but answered by `standaloneApi.localFetch` instead of an XHR.+ */+async function localAjax(+ localFetch: (request: Request) => Promise<globalThis.Response>,+ url: string,+ method: string,+ data: unknown,+ headers: Headers,+ opts: CallOptions+): Promise<Response> {+ const requestHeaders: Record<string, string> = {};+ for (const [ name, value ] of Object.entries(headers)) {+ if (value) {+ requestHeaders[name] = value;+ }+ }++ let body: string | undefined;+ if (data) {+ try {+ body = JSON.stringify(data);+ requestHeaders["content-type"] = "application/json";+ } catch (e) {+ console.log("Can't stringify data: ", data, " because of error: ", e);+ }+ }++ // The worker carries on with a request this stops waiting for, the same way the XHR path+ // abandons one still in flight. Nothing else bounds the wait: a worker that died rejects what+ // it was holding, but one stuck inside a request never answers it.+ const timeoutMs = opts.timeoutMs ?? DEFAULT_TIMEOUT;+ let timeoutHandle: ReturnType<typeof setTimeout> | undefined;+ const expiry = new Promise<never>((_, rej) => {+ timeoutHandle = setTimeout(() => rej(new Error(`Timed out after ${timeoutMs}ms: ${method} ${url}`)), timeoutMs);+ });++ const request = new Request(window.glob.baseApiUrl + url, { method, headers: requestHeaders, body });+ const startedAt = performance.now();++ let resp: globalThis.Response;+ try {+ resp = await Promise.race([ localFetch(request), expiry ]);+ } finally {+ clearTimeout(timeoutHandle);+ if (import.meta.env.DEV) {+ // This transport opens no XHR, so devtools' network panel lists none of these.+ console.debug(`[api] ${method} ${url} ${(performance.now() - startedAt).toFixed(1)}ms`);+ }+ }++ const responseText = await resp.text();++ if (!resp.ok) {+ // If the CSRF token is stale (e.g. session expired while tab was backgrounded),+ // refresh it and retry the request once.+ if (!opts.csrfRetried && isCsrfError(resp.status, responseText)) {+ await refreshCsrfToken();+ // Rebuild headers so the fresh glob.csrfToken is picked up+ const retryHeaders = await getHeaders({ "trilium-component-id": headers["trilium-component-id"] });+ return await ajax(url, method, data, retryHeaders, { ...opts, csrfRetried: true });+ }++ const silenced = (opts.silentNotFound && resp.status === 404)+ || (opts.silentInternalServerError && resp.status === 500)+ || (opts.silentUnauthorized && resp.status === 401);++ if (!silenced) {+ try {+ await reportError(method, url, resp.status, responseText);+ } catch {+ // reportError may throw (e.g. ValidationError); the throw below signals the caller either way.+ }+ }++ throw responseText;+ }++ const respHeaders: Headers = {};+ for (const [ name, value ] of resp.headers.entries()) {+ respHeaders[name] = value;+ }++ let respBody: unknown = responseText;+ if (!opts.raw && (resp.headers.get("content-type") ?? "").includes("json")) {+ respBody = responseText ? JSON.parse(responseText) : null;+ }++ return { body: respBody, headers: respHeaders };+}+async function reportError(method: string, url: string, statusCode: number, response: unknown) {let message = response;diff --git a/apps/client/src/vite-env.d.ts b/apps/client/src/vite-env.d.tsindex d874482510..c81a92ed8f 100644--- a/apps/client/src/vite-env.d.ts+++ b/apps/client/src/vite-env.d.ts@@ -7,6 +7,8 @@ interface ViteTypeOptions {interface ImportMetaEnv {/** Whether to enable the CKEditor inspector (see https://ckeditor.com/docs/ckeditor5/latest/framework/develpment-tools/inspector.html). */readonly VITE_CKEDITOR_ENABLE_INSPECTOR?: "true" | "false";+ /** Sends standalone's API calls through the service worker instead of the worker-owning tab. */+ readonly VITE_DISABLE_LOCAL_FETCH?: "true" | "false";}interface ImportMeta {diff --git a/apps/standalone/src/main.spec.ts b/apps/standalone/src/main.spec.tsindex 1e3031142a..bd1599e76e 100644--- a/apps/standalone/src/main.spec.ts+++ b/apps/standalone/src/main.spec.ts@@ -7,6 +7,7 @@ const mocks = vi.hoisted(() => ({restoreBackup: vi.fn(),downloadDatabase: vi.fn(),announceLeadership: vi.fn(),+ localFetch: vi.fn(),capacitorHttpHandler: vi.fn(),saveUrlToDevice: vi.fn()}));@@ -21,7 +22,8 @@ vi.mock("./local-bridge.js", () => ({registerNativeHttpHandler: mocks.registerNativeHttpHandler,restoreBackup: mocks.restoreBackup,downloadDatabase: mocks.downloadDatabase,- announceLeadership: mocks.announceLeadership+ announceLeadership: mocks.announceLeadership,+ localFetch: mocks.localFetch}));vi.mock("./leader_election.js", () => ({claimLeadership: (onElected: () => void) => {@@ -90,6 +92,8 @@ describe("bootstrap", () => {// The service worker has to know which tab owns the worker so it can// route every tab's API traffic there.await vi.waitFor(() => expect(mocks.announceLeadership).toHaveBeenCalled());+ // The leader also answers the client's API calls directly, skipping that route.+ expect(window.standaloneApi?.localFetch).toBe(mocks.localFetch);});it("a follower tab starts no worker but still bridges the SW", async () => {@@ -102,6 +106,10 @@ describe("bootstrap", () => {// exclusive OPFS handles and silently fall back to an empty in-memory one.expect(mocks.startLocalServerWorker).not.toHaveBeenCalled();expect(mocks.announceLeadership).not.toHaveBeenCalled();++ // A follower must keep calling through the service worker: with no worker of its own,+ // a localFetch here would answer from nothing.+ expect(window.standaloneApi?.localFetch).toBeUndefined();});it("registers the native HTTP handler and the share-sheet save under Capacitor only", async () => {diff --git a/apps/standalone/src/main.ts b/apps/standalone/src/main.tsindex fd35a6378d..e75acb194e 100644--- a/apps/standalone/src/main.ts+++ b/apps/standalone/src/main.ts@@ -6,7 +6,7 @@ import {import { showErrorOverlay } from "./error-overlay.js";import { installIosInterceptors } from "./ios-interceptors.js";import { claimLeadership } from "./leader_election.js";-import { announceLeadership, attachServiceWorkerBridge, downloadDatabase, registerNativeHttpHandler, restoreBackup, saveDatabase, startLocalServerWorker } from "./local-bridge.js";+import { announceLeadership, attachServiceWorkerBridge, downloadDatabase, localFetch, registerNativeHttpHandler, restoreBackup, saveDatabase, startLocalServerWorker } from "./local-bridge.js";/*** What a cold standalone start passes through, drawn as the splash's progress bar. Weights are@@ -113,6 +113,10 @@ async function bootstrap() {// worker instead. See leader_election.ts.claimLeadership(() => {startLocalServerWorker();+ // The leader answers API requests from its own worker, so the client's server.ts+ // can skip the service-worker round trip. Followers leave this unset and their+ // requests reach this tab through the service worker.+ standaloneApi.localFetch = localFetch;announceLeadership();});diff --git a/packages/commons/src/lib/standalone_api_interface.ts b/packages/commons/src/lib/standalone_api_interface.tsindex fb9b459b80..a10209f5ee 100644--- a/packages/commons/src/lib/standalone_api_interface.ts+++ b/packages/commons/src/lib/standalone_api_interface.ts@@ -127,4 +127,11 @@ export interface StandaloneApi {backup: StandaloneBackupApi;/** Present only inside the Capacitor shell, where the browser saves no downloads itself. */save?: StandaloneSaveApi;+ /**+ * Answers an internal API request from the SQLite worker this page owns. Present only in the+ * leader tab — a follower has no worker and reaches the leader's through the service worker.+ * The client's `server.ts` prefers this over its XHR transport, which would pay the+ * XHR → service worker → page round trip to end up in the same worker.+ */+ localFetch?(request: Request): Promise<Response>;}