Skip to content

Build your personal knowledge base with Trilium Notes

fix(standalone): bound the leader tab's /bootstrap wait and honour the opt-out

Elian Doran64ba7cf2d9a3
diff --git a/apps/client/src/index.ts b/apps/client/src/index.ts
index 86565b7bc7..bb86866e5a 100644
--- a/apps/client/src/index.ts
+++ b/apps/client/src/index.ts
@@ -4,6 +4,21 @@ import {
} from "./services/splash";
import { buildThemeStylesheetRefs, createStylesheetLink, getThemeStyle, initThemeChangeNotifier, StylesheetRef } from "./services/theme";
+/**
+ * How long the tab that owns the SQLite worker waits for it to answer `/bootstrap`. Matches the
+ * service worker's forwarding timeout in `apps/standalone/src/sw.ts`, so a worker that never
+ * finishes starting up fails the page at the same point on either transport.
+ */
+const LOCAL_BOOTSTRAP_TIMEOUT_MS = 270_000;
+
+/**
+ * Whether the tab that owns the SQLite worker answers `/bootstrap` itself. Mirrors
+ * `USE_LOCAL_FETCH` in `services/server.ts`, which gates the same choice for every later request;
+ * `VITE_DISABLE_LOCAL_FETCH=true` therefore takes the service worker's route for the whole
+ * session. Read here rather than imported, which would pull `server.ts` into the startup chunk.
+ */
+const USE_LOCAL_FETCH = import.meta.env.VITE_DISABLE_LOCAL_FETCH !== "true";
+
async function bootstrap() {
// The splash from index.html covers the page until hideSplash(). Standalone reports a longer
// sequence of its own before this one, so these phases only take effect on server and desktop.
@@ -44,9 +59,11 @@ async function setupGlob() {
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 localFetch = USE_LOCAL_FETCH ? window.standaloneApi?.localFetch : undefined;
const startedAt = performance.now();
- const response = localFetch ? await localFetch(new Request(url)) : await fetch(url);
+ const response = localFetch
+ ? await withTimeout(localFetch(new Request(url)), LOCAL_BOOTSTRAP_TIMEOUT_MS)
+ : 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
@@ -63,6 +80,20 @@ async function setupGlob() {
window.glob.getThemeStyle = getThemeStyle;
}
+/**
+ * Rejects once `timeoutMs` has passed without `promise` settling, so a worker that never answers
+ * reaches `bootstrap()`'s error handler and the splash states the failure.
+ */
+function withTimeout<T>(promise: Promise<T>, timeoutMs: number): Promise<T> {
+ return new Promise<T>((resolve, reject) => {
+ const timer = setTimeout(
+ () => reject(new Error("the local database worker did not answer in time")),
+ timeoutMs
+ );
+ promise.then(resolve, reject).finally(() => clearTimeout(timer));
+ });
+}
+
function getDevice() {
// Respect user's manual override via URL.
const urlParams = new URLSearchParams(window.location.search);
diff --git a/apps/standalone/src/main.ts b/apps/standalone/src/main.ts
index e75acb194e..640ec2402f 100644
--- a/apps/standalone/src/main.ts
+++ b/apps/standalone/src/main.ts
@@ -114,8 +114,7 @@ async function bootstrap() {
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.
+ // can skip the service-worker round trip.
standaloneApi.localFetch = localFetch;
announceLeadership();
});
diff --git a/packages/commons/src/lib/standalone_api_interface.ts b/packages/commons/src/lib/standalone_api_interface.ts
index 03f1892ddf..a8e51d2bc0 100644
--- a/packages/commons/src/lib/standalone_api_interface.ts
+++ b/packages/commons/src/lib/standalone_api_interface.ts
@@ -131,10 +131,9 @@ export interface StandaloneApi {
/** 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.
+ * Answers an internal API request from the SQLite worker this page owns. The tab that wins the
+ * database lock sets it, and the client's `server.ts` prefers it 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>;
}