Skip to content

Build your personal knowledge base with Trilium Notes

fix(client): hide the splash when the app renders, not when its module loads

Elian Doranc0810a172352
diff --git a/apps/client/src/components/app_context.ts b/apps/client/src/components/app_context.ts
index 9e4c689683..5a853c8042 100644
--- a/apps/client/src/components/app_context.ts
+++ b/apps/client/src/components/app_context.ts
@@ -13,6 +13,7 @@ import type LoadResults from "../services/load_results.js";
import type { CreateNoteOpts } from "../services/note_create.js";
import options from "../services/options.js";
import type { ShortcutHintSection } from "../services/shortcut_hints.js";
+import { reportSplashPhase } from "../services/splash.js";
import toast from "../services/toast.js";
import utils from "../services/utils.js";
import { ReactWrappedWidget } from "../widgets/basic_widget.js";
@@ -653,6 +654,7 @@ export class AppContext extends Component {
this.initComponents();
this.renderWidgets();
+ reportSplashPhase("notes");
await froca.initializedPromise;
this.tabManager.loadTabs();
diff --git a/apps/client/src/desktop.ts b/apps/client/src/desktop.ts
index 7d81dfd0e2..e7dfc8b17c 100644
--- a/apps/client/src/desktop.ts
+++ b/apps/client/src/desktop.ts
@@ -18,23 +18,31 @@ import { preloadCommonNoteTypes } from "./widgets/note_types.js";
await appContext.earlyInit();
-bundleService.getWidgetBundlesByParent().then(async (widgetBundles) => {
+/**
+ * Resolves once the layout is rendered and froca has the note tree. index.ts keeps the splash up
+ * until then, so the bundle, layout and tree requests below are not made behind a blank page. A
+ * failed start still resolves it: the toast that reports the failure has to become visible.
+ */
+export const ready = bundleService.getWidgetBundlesByParent().then(async (widgetBundles) => {
// A dynamic import is required for layouts since they initialize components which require translations.
const DesktopLayout = (await import("./layouts/desktop_layout.js")).default;
appContext.setLayout(new DesktopLayout(widgetBundles));
- appContext.start().then(() => {
- reportFullRenderStartupMetric();
- preloadCommonNoteTypes();
- }).catch((e) => {
+ try {
+ await appContext.start();
+ } catch (e) {
toastService.showPersistent({
id: "critical-error",
title: t("toast.critical-error.title"),
icon: "alert",
- message: t("toast.critical-error.message", { message: e.message })
+ message: t("toast.critical-error.message", { message: e instanceof Error ? e.message : String(e) })
});
console.error("Critical error occured", e);
- });
+ return;
+ }
+
+ reportFullRenderStartupMetric();
+ preloadCommonNoteTypes();
});
glob.setupGlobs();
diff --git a/apps/client/src/index.ts b/apps/client/src/index.ts
index d4af94102c..beb2f2610d 100644
--- a/apps/client/src/index.ts
+++ b/apps/client/src/index.ts
@@ -148,32 +148,37 @@ function setBodyAttributes() {
}
async function loadScripts() {
+ const entry = await importEntry();
+
+ // Every entry point renders after its module has finished evaluating: the desktop layout, the
+ // note tree and the locale catalogue are all fetched from there. Waiting for the `ready` it
+ // exports keeps the splash up until the screen is actually populated, instead of handing the
+ // user a blank page for the seconds those fetches take on a slow connection.
+ reportSplashPhase("interface");
+ await entry.ready;
+}
+
+function importEntry(): Promise<{ ready?: Promise<unknown> }> {
if (!glob.dbInitialized) {
- await import("./setup.js");
- return;
+ return import("./setup.js");
}
if (glob.passwordSet === false) {
- await import("./set_password.js");
- return;
+ return import("./set_password.js");
}
if (glob.loggedIn === false) {
- await import("./login.js");
- return;
+ return import("./login.js");
}
switch (glob.device) {
case "mobile":
- await import("./mobile.js");
- break;
+ return import("./mobile.js");
case "print":
- await import("./print.js");
- break;
+ return import("./print.js");
case "desktop":
default:
- await import("./desktop.js");
- break;
+ return import("./desktop.js");
}
}
diff --git a/apps/client/src/login.tsx b/apps/client/src/login.tsx
index 9a08a9f89c..6c010efef7 100644
--- a/apps/client/src/login.tsx
+++ b/apps/client/src/login.tsx
@@ -145,7 +145,6 @@ function initialSsoError(config: typeof window.glob.login): string | null {
return null;
}
-// Skip the bootstrap render under test, where the components are imported directly.
-if (import.meta.env.MODE !== "test") {
- void main();
-}
+// index.ts holds the splash up until the page has rendered. The render under test imports
+// the components directly, so it skips this one.
+export const ready = import.meta.env.MODE !== "test" ? main() : Promise.resolve();
diff --git a/apps/client/src/mobile.ts b/apps/client/src/mobile.ts
index 5bf5b4ea7e..45b8c8f519 100644
--- a/apps/client/src/mobile.ts
+++ b/apps/client/src/mobile.ts
@@ -18,4 +18,6 @@ setupClipboardImageEmbed();
const MobileLayout = (await import("./layouts/mobile_layout.js")).default;
appContext.setLayout(new MobileLayout());
-void appContext.start().then(preloadCommonNoteTypes);
+
+/** Resolves once the layout is rendered and froca has the note tree; see desktop.ts. */
+export const ready = appContext.start().then(preloadCommonNoteTypes);
diff --git a/apps/client/src/print.tsx b/apps/client/src/print.tsx
index 3c7ee275dc..721c738698 100644
--- a/apps/client/src/print.tsx
+++ b/apps/client/src/print.tsx
@@ -206,4 +206,5 @@ export async function loadCustomCss(note: FNote) {
await Promise.allSettled(loadPromises);
}
-main();
+/** index.ts holds the splash up until the note has rendered. */
+export const ready = main();
diff --git a/apps/client/src/services/splash.ts b/apps/client/src/services/splash.ts
index fc7b00b42a..f465d1e780 100644
--- a/apps/client/src/services/splash.ts
+++ b/apps/client/src/services/splash.ts
@@ -34,7 +34,9 @@ export interface SplashPhase {
*/
export const CLIENT_STARTUP_PHASES: SplashPhase[] = [
{ id: "bootstrap", weight: 1, status: "Opening your notes…" },
- { id: "application", weight: 2, status: "Loading the application…" }
+ { id: "application", weight: 3, status: "Loading the application…" },
+ { id: "interface", weight: 2, status: "Building the interface…" },
+ { id: "notes", weight: 2, status: "Loading the note tree…" }
];
let phases: SplashPhase[] = [];
diff --git a/apps/client/src/set_password.tsx b/apps/client/src/set_password.tsx
index db3240df96..89371af019 100644
--- a/apps/client/src/set_password.tsx
+++ b/apps/client/src/set_password.tsx
@@ -84,7 +84,6 @@ export function App() {
);
}
-// Skip the bootstrap render under test, where the component is imported directly.
-if (import.meta.env.MODE !== "test") {
- void main();
-}
+// index.ts holds the splash up until the page has rendered. The render under test imports
+// the component directly, so it skips this one.
+export const ready = import.meta.env.MODE !== "test" ? main() : Promise.resolve();
diff --git a/apps/client/src/setup.tsx b/apps/client/src/setup.tsx
index b21885c227..b47bd68e8d 100644
--- a/apps/client/src/setup.tsx
+++ b/apps/client/src/setup.tsx
@@ -921,7 +921,6 @@ function onSetupFinished() {
}
}
-// Skip the bootstrap render under test, where the components are imported directly.
-if (import.meta.env.MODE !== "test") {
- void main();
-}
+// index.ts holds the splash up until the page has rendered. The render under test imports
+// the components directly, so it skips this one.
+export const ready = import.meta.env.MODE !== "test" ? main() : Promise.resolve();
diff --git a/apps/standalone/src/main.spec.ts b/apps/standalone/src/main.spec.ts
index a8ff552ab2..a49ebbaa43 100644
--- a/apps/standalone/src/main.spec.ts
+++ b/apps/standalone/src/main.spec.ts
@@ -132,9 +132,9 @@ describe("bootstrap", () => {
await vi.waitFor(() => expect(sw.register).toHaveBeenCalled());
expect(document.getElementById("splash-status")?.textContent)
.toBe("Setting up offline support…");
- // Seven weighted phases, the first of which covers 1/17 of the bar.
+ // Nine weighted phases, the first of which covers 1/20 of the bar.
const fill = document.querySelector<HTMLElement>(".splash-bar-fill");
- expect(fill?.style.width).toBe("6%");
+ expect(fill?.style.width).toBe("5%");
});
it("lets the worker's phases through after the client reports its own", async () => {
diff --git a/apps/standalone/src/main.ts b/apps/standalone/src/main.ts
index 27c102aed6..43d2aa3165 100644
--- a/apps/standalone/src/main.ts
+++ b/apps/standalone/src/main.ts
@@ -11,7 +11,8 @@ import { announceLeadership, attachServiceWorkerBridge, downloadDatabase, regist
* the relative cost of each step on a first visit, where the two large downloads — the SQLite
* WASM binary and the core bundle — dominate; a warm start runs the same sequence from cache.
*
- * The worker reports the middle of this sequence over `STARTUP_PROGRESS` (see local-bridge.ts).
+ * The worker reports the middle of this sequence over `STARTUP_PROGRESS` (see local-bridge.ts),
+ * and the client reports the last two once its entry point is loading its layout and note tree.
* A follower tab has no worker of its own, so it sits on the first phase until the leader answers
* its `/bootstrap` and the client's own phases carry the bar the rest of the way.
*
@@ -27,7 +28,9 @@ const STANDALONE_STARTUP_PHASES: SplashPhase[] = [
{ id: "database", weight: 2, status: "Opening the database…" },
{ id: "core", weight: 4, status: "Loading Trilium…" },
{ id: "becca", weight: 2, status: "Reading your notes…" },
- { id: "application", weight: 4, status: "Loading the application…" }
+ { id: "application", weight: 4, status: "Loading the application…" },
+ { id: "interface", weight: 2, status: "Building the interface…" },
+ { id: "notes", weight: 1, status: "Loading the note tree…" }
];
async function waitForServiceWorkerControl(): Promise<void> {