fix(standalone): component ID not preserved in WS

This commit is contained in:
Elian Doran
2026-03-23 16:47:28 +02:00
parent 3210dbb6d8
commit 5ea014cc37
2 changed files with 15 additions and 0 deletions

View File

@@ -183,6 +183,7 @@ export class BrowserRouter {
path,
params,
query,
headers: headers ?? {},
body: parsedBody
};

View File

@@ -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));