Skip to content

Build your personal knowledge base with Trilium Notes

fix(standalone): component ID not preserved in WS

Elian Doran5ea014cc370c
diff --git a/apps/client-standalone/src/lightweight/browser_router.ts b/apps/client-standalone/src/lightweight/browser_router.ts
index 9a601e6200..d41b70fb93 100644
--- a/apps/client-standalone/src/lightweight/browser_router.ts
+++ b/apps/client-standalone/src/lightweight/browser_router.ts
@@ -183,6 +183,7 @@ export class BrowserRouter {
path,
params,
query,
+ headers: headers ?? {},
body: parsedBody
};
diff --git a/apps/client-standalone/src/lightweight/browser_routes.ts b/apps/client-standalone/src/lightweight/browser_routes.ts
index 69332a99a7..8cce05152e 100644
--- a/apps/client-standalone/src/lightweight/browser_routes.ts
+++ b/apps/client-standalone/src/lightweight/browser_routes.ts
@@ -32,6 +32,18 @@ function toExpressLikeReq(req: BrowserRequest) {
};
}
+/**
+ * Extracts context headers from the request and sets them in the execution context,
+ * mirroring what the server does in route_api.ts.
+ */
+function setContextFromHeaders(req: BrowserRequest) {
+ const headers = req.headers ?? {};
+ const ctx = getContext();
+ ctx.set("componentId", headers["trilium-component-id"]);
+ ctx.set("localNowDateTime", headers["trilium-local-now-datetime"]);
+ ctx.set("hoistedNoteId", headers["trilium-hoisted-note-id"] || "root");
+}
+
/**
* Wraps a core route handler to work with the BrowserRouter.
* Core handlers expect an Express-like request object with params, query, and body.
@@ -41,6 +53,7 @@ function toExpressLikeReq(req: BrowserRequest) {
function wrapHandler(handler: (req: any) => unknown, transactional: boolean) {
return (req: BrowserRequest) => {
return getContext().init(() => {
+ setContextFromHeaders(req);
const expressLikeReq = toExpressLikeReq(req);
if (transactional) {
return getSql().transactional(() => handler(expressLikeReq));
@@ -72,6 +85,7 @@ function createRoute(router: BrowserRouter) {
return (method: HttpMethod, path: string, _middleware: any[], handler: (req: any) => unknown, resultHandler?: ((req: any, res: any, result: unknown) => unknown) | null) => {
router.register(method, path, (req: BrowserRequest) => {
return getContext().init(() => {
+ setContextFromHeaders(req);
const expressLikeReq = toExpressLikeReq(req);
const result = getSql().transactional(() => handler(expressLikeReq));