From 1818ae1f72180c9e231b438d94e148e46d67b967 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Wed, 4 Jun 2025 19:55:04 +0300 Subject: [PATCH] fix(desktop): blank screen when starting (closes #2103) --- apps/desktop/src/electron-main.ts | 14 +++++++-- apps/edit-docs/src/utils.ts | 3 +- apps/server/src/main.ts | 3 +- apps/server/src/www.ts | 50 +++++++++++++++---------------- 4 files changed, 40 insertions(+), 30 deletions(-) diff --git a/apps/desktop/src/electron-main.ts b/apps/desktop/src/electron-main.ts index c8c28ac77..3decb970d 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(); + // 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 059e1e0cc..28740f3bd 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 { 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 b334c148d..072ff3229 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 2dcd74c80..58289fd1b 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 +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); -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); + }); - // 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(); -tmp.setGracefulCleanup(); -startTrilium(); - -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) {