Skip to content

Build your personal knowledge base with Trilium Notes

fix(desktop): blank screen when starting (closes #2103)

Elian Doran1818ae1f7218
diff --git a/apps/desktop/src/electron-main.ts b/apps/desktop/src/electron-main.ts
index c8c28ac779..3decb970d6 100644
--- a/apps/desktop/src/electron-main.ts
+++ b/apps/desktop/src/electron-main.ts
@@ -7,8 +7,11 @@ import tray from "@triliumnext/server/src/services/tray.js";
import options from "@triliumnext/server/src/services/options.js";
import electronDebug from "electron-debug";
import electronDl from "electron-dl";
+import { deferred } from "@triliumnext/server/src/services/utils.js";
async function main() {
+ const serverInitializedPromise = deferred<void>();
+
// Prevent Trilium starting twice on first install and on uninstall for the Windows installer.
if ((require("electron-squirrel-startup")).default) {
process.exit(0);
@@ -37,7 +40,11 @@ async function main() {
}
});
- electron.app.on("ready", onReady);
+ electron.app.on("ready", async () => {
+ await serverInitializedPromise;
+ console.log("Starting Electron...");
+ await onReady();
+ });
electron.app.on("will-quit", () => {
electron.globalShortcut.unregisterAll();
@@ -47,7 +54,10 @@ async function main() {
process.env["ELECTRON_DISABLE_SECURITY_WARNINGS"] = "true";
await initializeTranslations();
- await import("@triliumnext/server/src/main.js");
+ const startTriliumServer = (await import("@triliumnext/server/src/www.js")).default;
+ await startTriliumServer();
+ console.log("Server loaded");
+ serverInitializedPromise.resolve();
}
async function onReady() {
diff --git a/apps/edit-docs/src/utils.ts b/apps/edit-docs/src/utils.ts
index 059e1e0ccf..28740f3bd6 100644
--- a/apps/edit-docs/src/utils.ts
+++ b/apps/edit-docs/src/utils.ts
@@ -38,7 +38,8 @@ export function startElectron(callback: () => void): DeferredPromise<void> {
console.log("Electron is ready!");
// Start the server.
- await import("@triliumnext/server/src/main.js");
+ const startTriliumServer = (await import("@triliumnext/server/src/www.js")).default;
+ await startTriliumServer();
// Create the main window.
await windowService.createMainWindow(electron.app);
diff --git a/apps/server/src/main.ts b/apps/server/src/main.ts
index b334c148d4..072ff3229e 100644
--- a/apps/server/src/main.ts
+++ b/apps/server/src/main.ts
@@ -7,7 +7,8 @@ import { initializeTranslations } from "./services/i18n.js";
async function startApplication() {
await initializeTranslations();
- await import("./www.js");
+ const startTriliumServer = (await import("./www.js")).default;
+ await startTriliumServer();
}
startApplication();
diff --git a/apps/server/src/www.ts b/apps/server/src/www.ts
index 2dcd74c809..58289fd1bf 100644
--- a/apps/server/src/www.ts
+++ b/apps/server/src/www.ts
@@ -14,37 +14,35 @@ import type { Express } from "express";
const MINIMUM_NODE_VERSION = "20.0.0";
-// setup basic error handling even before requiring dependencies, since those can produce errors as well
-
-process.on("unhandledRejection", (error: Error) => {
- // this makes sure that stacktrace of failed promise is printed out
- console.log(error);
-
- // but also try to log it into file
- log.info(error);
-});
+export default async function startTriliumServer() {
+ // setup basic error handling even before requiring dependencies, since those can produce errors as well
+ process.on("unhandledRejection", (error: Error) => {
+ // this makes sure that stacktrace of failed promise is printed out
+ console.log(error);
+
+ // but also try to log it into file
+ log.info(error);
+ });
-function exit() {
- console.log("Caught interrupt/termination signal. Exiting.");
- process.exit(0);
-}
+ function exit() {
+ console.log("Caught interrupt/termination signal. Exiting.");
+ process.exit(0);
+ }
-process.on("SIGINT", exit);
-process.on("SIGTERM", exit);
+ process.on("SIGINT", exit);
+ process.on("SIGTERM", exit);
-if (utils.compareVersions(process.versions.node, MINIMUM_NODE_VERSION) < 0) {
- console.error();
- console.error(`The Trilium server requires Node.js ${MINIMUM_NODE_VERSION} and later in order to start.\n`);
- console.error(`\tCurrent version:\t${process.versions.node}`);
- console.error(`\tExpected version:\t${MINIMUM_NODE_VERSION}`);
- console.error();
- process.exit(1);
-}
+ if (utils.compareVersions(process.versions.node, MINIMUM_NODE_VERSION) < 0) {
+ console.error();
+ console.error(`The Trilium server requires Node.js ${MINIMUM_NODE_VERSION} and later in order to start.\n`);
+ console.error(`\tCurrent version:\t${process.versions.node}`);
+ console.error(`\tExpected version:\t${MINIMUM_NODE_VERSION}`);
+ console.error();
+ process.exit(1);
+ }
-tmp.setGracefulCleanup();
-startTrilium();
+ tmp.setGracefulCleanup();
-async function startTrilium() {
const app = await buildApp();
/**
@@ -98,7 +96,7 @@ function startHttpServer(app: Express) {
log.info(`Trusted reverse proxy: ${app.get("trust proxy")}`);
- let httpServer;
+ let httpServer: http.Server | https.Server;
if (config["Network"]["https"]) {
if (!config["Network"]["keyPath"] || !config["Network"]["keyPath"].trim().length) {