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
fix(standalone): reload once when a startup module fails to download
Elian Doranf7546d074cb3
diff --git a/apps/standalone/src/error-overlay.spec.ts b/apps/standalone/src/error-overlay.spec.tsindex 0bfa63f126..dc75edd452 100644--- a/apps/standalone/src/error-overlay.spec.ts+++ b/apps/standalone/src/error-overlay.spec.ts@@ -9,6 +9,7 @@ async function freshOverlay(): Promise<typeof import("./error-overlay.js")> {beforeEach(() => {document.body.innerHTML = "";+ sessionStorage.clear();});afterEach(() => {@@ -74,4 +75,41 @@ describe("showErrorOverlay", () => {Object.defineProperty(window, "location", { value: original, configurable: true });});++ it("reloads once instead of showing a chunk that failed to download", async () => {+ const reload = vi.fn();+ const original = window.location;+ Object.defineProperty(window, "location", {+ value: { ...original, reload },+ configurable: true+ });++ try {+ document.body.innerHTML = `<div id="splash"></div>`;+ let { showErrorOverlay } = await freshOverlay();+ showErrorOverlay("Trilium couldn't start", "Importing a module script failed.");+ expect(reload).toHaveBeenCalledTimes(1);+ expect(document.getElementById("trilium-error-overlay")).toBeNull();+ expect(document.getElementById("splash")).not.toBeNull();++ // The page after the reload fails the same way: the overlay shows rather than a loop.+ ({ showErrorOverlay } = await freshOverlay());+ showErrorOverlay("Trilium couldn't start", "Importing a module script failed.");+ expect(reload).toHaveBeenCalledTimes(1);+ expect(document.getElementById("trilium-error-overlay")?.textContent)+ .toContain("Importing a module script failed.");+ } finally {+ Object.defineProperty(window, "location", { value: original, configurable: true });+ }+ });++ it("recognizes each engine's module load failure and nothing else", async () => {+ const { isModuleLoadFailure } = await freshOverlay();+ expect(isModuleLoadFailure("Importing a module script failed.")).toBe(true);+ expect(isModuleLoadFailure("Failed to fetch dynamically imported module: https://x/a.js"))+ .toBe(true);+ expect(isModuleLoadFailure("error loading dynamically imported module: https://x/a.js"))+ .toBe(true);+ expect(isModuleLoadFailure("SQLITE_CORRUPT: database disk image is malformed")).toBe(false);+ });});diff --git a/apps/standalone/src/error-overlay.ts b/apps/standalone/src/error-overlay.tsindex 90cc9c4223..fb057dd895 100644--- a/apps/standalone/src/error-overlay.ts+++ b/apps/standalone/src/error-overlay.ts@@ -31,6 +31,10 @@ export function showErrorOverlay(title: string, message: string, detail?: string}shown = true;+ if (isModuleLoadFailure(message) && reloadOnce()) {+ return;+ }+// The startup splash sits above everything and would cover the overlay.document.getElementById("splash")?.remove();@@ -77,6 +81,38 @@ export function showErrorOverlay(title: string, message: string, detail?: stringdocument.body.append(overlay);}+/**+ * True for the message an engine gives when a script module could not be fetched, which during+ * startup is usually a connection that dropped and works again on the next try.+ */+export function isModuleLoadFailure(message: string) {+ return message.includes("Importing a module script failed")+ || message.includes("Failed to fetch dynamically imported module")+ || message.includes("error loading dynamically imported module");+}++const RELOAD_MARKER_KEY = "trilium-module-failure-reload";+const RELOAD_INTERVAL_MS = 60_000;++/**+ * Reloads the page unless it already reloaded for the same reason in the last minute, so a+ * connection that stays down ends on the overlay rather than in a loop. Returns whether it reloaded.+ */+function reloadOnce() {+ try {+ const lastReload = Number(sessionStorage.getItem(RELOAD_MARKER_KEY));+ if (Date.now() - lastReload < RELOAD_INTERVAL_MS) {+ return false;+ }+ sessionStorage.setItem(RELOAD_MARKER_KEY, String(Date.now()));+ } catch {+ return false;+ }++ location.reload();+ return true;+}+const WARNING_SVG = `<svg viewBox="0 0 24 24" width="56" height="56" fill="none" aria-hidden="true" focusable="false"><path d="M12 3.4 22.2 20.4H1.8L12 3.4Z" fill="currentColor" fill-opacity="0.14"/>