Skip to content

Build your personal knowledge base with Trilium Notes

refactor(electron): deduplicate protocol registration

Elian Dorandadbcccda153
diff --git a/apps/desktop/src/main.ts b/apps/desktop/src/main.ts
index 2e363d671d..214b8e8f43 100644
--- a/apps/desktop/src/main.ts
+++ b/apps/desktop/src/main.ts
@@ -14,7 +14,7 @@ import windowService from "@triliumnext/server/src/services/window.js";
import WebSocketMessagingProvider from "@triliumnext/server/src/services/ws_messaging_provider.js";
import BetterSqlite3Provider from "@triliumnext/server/src/sql_provider.js";
import NodejsZipProvider from "@triliumnext/server/src/zip_provider.js";
-import { app, BrowserWindow,globalShortcut, protocol } from "electron";
+import { app, BrowserWindow,globalShortcut } from "electron";
import electronDebug from "electron-debug";
import electronDl from "electron-dl";
import fs from "fs";
@@ -24,6 +24,7 @@ import path, { join, resolve } from "path";
import { deferred, LOCALES } from "../../../packages/commons/src";
import { PRODUCT_NAME } from "./app-info";
import DesktopPlatformProvider from "./platform_provider";
+import { registerTriliumAppScheme } from "./protocol";
async function main() {
// Ignore EPIPE errors on stdout/stderr — these occur when the parent process
@@ -36,22 +37,7 @@ async function main() {
});
}
- // The renderer loads the entire UI from `trilium-app://app/` instead of the
- // HTTP origin. Registering the scheme as privileged (must happen before
- // `app.ready`) gives it a proper origin & cookie jar, fetch support, and
- // CORS, which lets the session + CSRF middleware on the Express side work
- // without any of the FakeRequest/cookie-injection workarounds.
- protocol.registerSchemesAsPrivileged([
- {
- scheme: "trilium-app",
- privileges: {
- standard: true,
- secure: true,
- supportFetchAPI: true,
- corsEnabled: true
- }
- }
- ]);
+ registerTriliumAppScheme();
const userDataPath = getUserData();
app.setPath("userData", userDataPath);
diff --git a/apps/desktop/src/protocol.ts b/apps/desktop/src/protocol.ts
new file mode 100644
index 0000000000..255f99020f
--- /dev/null
+++ b/apps/desktop/src/protocol.ts
@@ -0,0 +1,29 @@
+import { protocol } from "electron";
+
+/**
+ * Registers the `trilium-app://` custom scheme as privileged so the renderer
+ * can load the UI from `trilium-app://app/` with a proper origin & cookie jar,
+ * fetch support, and CORS. The actual request handler is installed elsewhere
+ * (apps/server/src/routes/electron.ts), once `app.ready` has fired.
+ *
+ * **Must be called before `app.ready`.** Electron only honours
+ * `registerSchemesAsPrivileged` if it runs synchronously during startup;
+ * otherwise Chromium treats the scheme as non-standard with an opaque origin
+ * and aborts navigation with `(blocked:origin)`.
+ *
+ * Shared between `apps/desktop` (main entry) and `apps/edit-docs`
+ * (edit-docs / edit-demo entry).
+ */
+export function registerTriliumAppScheme() {
+ protocol.registerSchemesAsPrivileged([
+ {
+ scheme: "trilium-app",
+ privileges: {
+ standard: true,
+ secure: true,
+ supportFetchAPI: true,
+ corsEnabled: true
+ }
+ }
+ ]);
+}
diff --git a/apps/edit-docs/src/utils.ts b/apps/edit-docs/src/utils.ts
index 2d9ee22378..f8bb9db205 100644
--- a/apps/edit-docs/src/utils.ts
+++ b/apps/edit-docs/src/utils.ts
@@ -1,4 +1,5 @@
import { BackupService, type ImageProvider,initializeCore } from "@triliumnext/core";
+import { registerTriliumAppScheme } from "@triliumnext/desktop/src/protocol.js";
import ClsHookedExecutionContext from "@triliumnext/server/src/cls_provider.js";
import NodejsCryptoProvider from "@triliumnext/server/src/crypto_provider.js";
import ServerPlatformProvider from "@triliumnext/server/src/platform_provider.js";
@@ -14,24 +15,10 @@ import path from "path";
import { deferred, type DeferredPromise } from "../../../packages/commons/src/index.js";
-// The renderer loads the entire UI from `trilium-app://app/` (see
-// apps/server/src/services/window.ts). The scheme has to be registered as
-// privileged before `app.ready` fires; otherwise Chromium treats it as a
-// non-standard scheme with an opaque origin and blocks the navigation with
-// `(blocked:origin)`. Desktop does the same in apps/desktop/src/main.ts. This
-// runs at module top-level so it executes during the import phase of
-// edit-docs.ts / edit-demo.ts, before either entry point's first `await`.
-electron.protocol.registerSchemesAsPrivileged([
- {
- scheme: "trilium-app",
- privileges: {
- standard: true,
- secure: true,
- supportFetchAPI: true,
- corsEnabled: true
- }
- }
-]);
+// Register the `trilium-app://` scheme synchronously at module load (before any
+// `await` in edit-docs.ts / edit-demo.ts) so it lands before `app.ready` fires
+// — otherwise Chromium blocks the navigation with `(blocked:origin)`.
+registerTriliumAppScheme();
// Stub backup service (not used in edit-docs, but required by initializeCore)
class StubBackupService extends BackupService {