Skip to content

Build your personal knowledge base with Trilium Notes

fix(server): honour trilium-device cookie on desktop builds, closes #10720

Elian Doran9279692bec8f
diff --git a/apps/server/src/routes/index.spec.ts b/apps/server/src/routes/index.spec.ts
index 5f40ef0f97..858ea625d9 100644
--- a/apps/server/src/routes/index.spec.ts
+++ b/apps/server/src/routes/index.spec.ts
@@ -34,14 +34,20 @@ vi.mock("@triliumnext/core", () => ({
getLog: () => ({ info: () => {}, error: () => {} })
}));
-const { bootstrap } = await import("./index.js");
+const { bootstrap, getView } = await import("./index.js");
-function makeReq(overrides: { loggedIn?: boolean; internal?: boolean } = {}): Request {
+function makeReq(overrides: {
+ loggedIn?: boolean;
+ internal?: boolean;
+ query?: Record<string, unknown>;
+ cookies?: Record<string, string>;
+ userAgent?: string;
+} = {}): Request {
const req = {
session: { loggedIn: overrides.loggedIn ?? false },
- query: {},
- headers: {},
- cookies: {}
+ query: overrides.query ?? {},
+ headers: overrides.userAgent ? { "user-agent": overrides.userAgent } : {},
+ cookies: overrides.cookies ?? {}
} as unknown as Request;
if (overrides.internal) {
markAsInternalElectronRequest(req);
@@ -92,3 +98,45 @@ describe("bootstrap auth gating (desktop build, #10589)", () => {
expect(body().wsBaseUrl).toBeTruthy();
});
});
+
+describe("getView view resolution (desktop build, #10720)", () => {
+ it("resolves the mobile view when a browser hits a desktop build with the trilium-device=mobile cookie", () => {
+ // "Switch to Mobile Version" sets this cookie and reloads. On a desktop
+ // build the process-wide isElectron flag is true, but this browser request
+ // is NOT the trusted renderer, so the cookie must still be honoured.
+ // Previously getView short-circuited on the global flag and always returned
+ // "desktop", so the mobile layout never activated.
+ const req = makeReq({ internal: false, cookies: { "trilium-device": "mobile" } });
+
+ expect(getView(req)).toBe("mobile");
+ });
+
+ it("keeps the desktop view for the trusted Electron renderer even with a mobile cookie", () => {
+ const req = makeReq({ internal: true, cookies: { "trilium-device": "mobile" } });
+
+ expect(getView(req)).toBe("desktop");
+ });
+
+ it("honours the print override above everything else", () => {
+ const req = makeReq({ query: { print: "" }, cookies: { "trilium-device": "mobile" } });
+
+ expect(getView(req)).toBe("print");
+ });
+
+ it("respects the ?mobile and ?desktop query overrides on a browser request", () => {
+ expect(getView(makeReq({ query: { mobile: "" } }))).toBe("mobile");
+ expect(getView(makeReq({ query: { desktop: "" } }))).toBe("desktop");
+ });
+
+ it("respects an explicit trilium-device=desktop cookie", () => {
+ expect(getView(makeReq({ cookies: { "trilium-device": "desktop" } }))).toBe("desktop");
+ });
+
+ it("falls back to user-agent detection when no override is present", () => {
+ const mobileUa = "Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15";
+ expect(getView(makeReq({ userAgent: mobileUa }))).toBe("mobile");
+
+ const desktopUa = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome";
+ expect(getView(makeReq({ userAgent: desktopUa }))).toBe("desktop");
+ });
+});
diff --git a/apps/server/src/routes/index.ts b/apps/server/src/routes/index.ts
index 79d494e712..f3443b52cc 100644
--- a/apps/server/src/routes/index.ts
+++ b/apps/server/src/routes/index.ts
@@ -10,7 +10,7 @@ import { getLog } from "@triliumnext/core";
import { isInternalElectronRequest } from "../services/electron_request.js";
import port from "../services/port.js";
import openID from "../services/open_id.js";
-import { isDev, isElectron, isMac, supportsBackgroundMaterial } from "../services/utils.js";
+import { isDev, isMac, supportsBackgroundMaterial } from "../services/utils.js";
import totp from "../services/totp.js";
import { generateCsrfToken } from "./csrf_protection.js";
@@ -186,14 +186,18 @@ export function bootstrap(req: Request, res: Response) {
} satisfies BootstrapDefinition);
}
-function getView(req: Request): View {
+export function getView(req: Request): View {
// Special override for printing.
if ("print" in req.query) {
return "print";
}
- // Electron always uses the desktop view.
- if (isElectron) {
+ // The trusted Electron renderer always uses the desktop view. This must key
+ // off the per-request marker, not the process-wide `isElectron` flag: on a
+ // desktop build a plain browser hits the same HTTP listener, and forcing
+ // "desktop" for it ignored the `trilium-device=mobile` cookie set by
+ // "Switch to Mobile Version" (#10720). Same per-request vs. global fix as #10589.
+ if (isInternalElectronRequest(req)) {
return "desktop";
}