Skip to content

Build your personal knowledge base with Trilium Notes

fix(electron): vite websocket not working

Elian Doranbf80c2389693
diff --git a/apps/server/src/routes/assets.ts b/apps/server/src/routes/assets.ts
index 7a7b06f164..23bb346a7e 100644
--- a/apps/server/src/routes/assets.ts
+++ b/apps/server/src/routes/assets.ts
@@ -7,7 +7,7 @@ import type serveStatic from "serve-static";
import { assetUrlFragment } from "../services/asset_path.js";
import auth from "../services/auth.js";
import port from "../services/port.js";
-import { getResourceDir, isDev } from "../services/utils.js";
+import { getResourceDir, isDev, isElectron } from "../services/utils.js";
import { doubleCsrfProtection as csrfMiddleware } from "./csrf_protection.js";
// Allow serving assets even if the installation path contains a hidden (dot-prefixed) directory.
@@ -42,7 +42,12 @@ async function register(app: express.Application) {
// Derive a unique HMR port from the application port so
// multiple dev instances (e.g. server on 8080, desktop on
// 37742) don't all fight over Vite's default port 24678.
- port: port + 10
+ port: port + 10,
+ // Under Electron the page loads from `trilium-app://app/`,
+ // so Vite's default of using `window.location.hostname`
+ // would try `ws://app:…` — DNS fails. Pin the HMR client
+ // to loopback explicitly.
+ ...(isElectron ? { host: "127.0.0.1", protocol: "ws" as const } : {})
}
},
appType: "spa",