Skip to content

Build your personal knowledge base with Trilium Notes

feat(standalone): ask the browser to keep the database's storage

Elian Doran90fb33dfd9ed
diff --git a/apps/standalone/src/main.spec.ts b/apps/standalone/src/main.spec.ts
index bd1599e76e..1feeb60096 100644
--- a/apps/standalone/src/main.spec.ts
+++ b/apps/standalone/src/main.spec.ts
@@ -52,6 +52,10 @@ function setServiceWorker(sw: ServiceWorkerLike | undefined) {
Object.defineProperty(navigator, "serviceWorker", { value: sw, configurable: true });
}
+function setStorageManager(storage: { persist?: unknown } | undefined) {
+ Object.defineProperty(navigator, "storage", { value: storage, configurable: true });
+}
+
let reloadSpy: ReturnType<typeof vi.fn>;
beforeEach(() => {
@@ -64,7 +68,9 @@ beforeEach(() => {
value: { ...window.location, protocol: "https:", hostname: "localhost", reload: reloadSpy, search: "" },
configurable: true
});
+ setStorageManager({ persist: vi.fn().mockResolvedValue(true) });
vi.spyOn(console, "log").mockImplementation(() => {});
+ vi.spyOn(console, "warn").mockImplementation(() => {});
vi.spyOn(console, "error").mockImplementation(() => {});
});
@@ -112,6 +118,47 @@ describe("bootstrap", () => {
expect(window.standaloneApi?.localFetch).toBeUndefined();
});
+ it("asks the browser to keep the storage the database lives in", async () => {
+ const persist = vi.fn().mockResolvedValue(true);
+ setStorageManager({ persist });
+ setServiceWorker({ controller: {}, register: vi.fn(), ready: Promise.resolve() });
+ await runBootstrap();
+
+ await vi.waitFor(() => expect(persist).toHaveBeenCalled());
+ expect(console.log).toHaveBeenCalledWith("[Bootstrap] Storage is persistent");
+ });
+
+ it("reports a refusal and starts up regardless", async () => {
+ const persist = vi.fn().mockResolvedValue(false);
+ setStorageManager({ persist });
+ setServiceWorker({ controller: {}, register: vi.fn(), ready: Promise.resolve() });
+ await runBootstrap();
+
+ // A browser that declines gives no reason and takes no argument, so the only thing left
+ // to do about it is say so; the database still opens either way.
+ await vi.waitFor(() => expect(console.log)
+ .toHaveBeenCalledWith(expect.stringContaining("best-effort")));
+ expect(mocks.startLocalServerWorker).toHaveBeenCalled();
+ expect(document.body.innerHTML).toBe("");
+ });
+
+ it("survives a browser that rejects or lacks the request", async () => {
+ setStorageManager({ persist: vi.fn().mockRejectedValue(new Error("no quota manager")) });
+ setServiceWorker({ controller: {}, register: vi.fn(), ready: Promise.resolve() });
+ await runBootstrap();
+ await vi.waitFor(() => expect(console.warn).toHaveBeenCalledWith(
+ "[Bootstrap] Could not ask for persistent storage:", expect.any(Error)));
+ expect(document.body.innerHTML).toBe("");
+
+ // `navigator.storage` is missing outside a secure context; asking there would throw
+ // before the service worker's own error screen could explain why nothing works.
+ vi.mocked(console.warn).mockClear();
+ setStorageManager(undefined);
+ await runBootstrap();
+ await vi.waitFor(() => expect(mocks.startLocalServerWorker).toHaveBeenCalledTimes(2));
+ expect(console.warn).not.toHaveBeenCalled();
+ });
+
it("registers the native HTTP handler and the share-sheet save under Capacitor only", async () => {
setServiceWorker({ controller: {}, register: vi.fn(), ready: Promise.resolve() });
await runBootstrap();
diff --git a/apps/standalone/src/main.ts b/apps/standalone/src/main.ts
index 640ec2402f..a074a149ac 100644
--- a/apps/standalone/src/main.ts
+++ b/apps/standalone/src/main.ts
@@ -91,6 +91,10 @@ async function bootstrap() {
};
window.standaloneApi = standaloneApi;
+ // Never awaited: the verdict changes nothing about how startup proceeds, and the browser is
+ // free to take its time reaching one.
+ void requestPersistentStorage();
+
try {
// When running inside a Capacitor WebView, register the native HTTP
// handler so outbound sync requests bypass CORS and cookie restrictions.
@@ -144,6 +148,34 @@ async function bootstrap() {
}
}
+/**
+ * Asks the browser to keep the storage the database lives in. Without the grant that storage is
+ * best-effort: WebKit drops it after about a week without a visit and Chromium evicts it under
+ * pressure, taking the whole database with it.
+ *
+ * The browser answers from its own heuristics — how installed the site looks — and a page can
+ * neither prompt for it nor appeal it, so this reports the verdict and carries on. It runs here
+ * rather than beside the OPFS code because `StorageManager.persist()` is exposed on the window
+ * only; a worker can read `persisted()` but cannot ask.
+ *
+ * `navigator.storage` is absent outside a secure context, which standalone already refuses to
+ * start in.
+ */
+async function requestPersistentStorage() {
+ if (!navigator.storage?.persist) {
+ return;
+ }
+
+ try {
+ const granted = await navigator.storage.persist();
+ console.log(granted
+ ? "[Bootstrap] Storage is persistent"
+ : "[Bootstrap] Storage is best-effort, so the browser can evict the database");
+ } catch (err) {
+ console.warn("[Bootstrap] Could not ask for persistent storage:", err);
+ }
+}
+
async function loadScripts() {
await import("../../client/src/index.js");
}