Repository navigation
feat(node): let the node adapter set locals for every view - #18396
Conversation
micalevisk
left a comment
There was a problem hiding this comment.
Verified locally: unit spec and the Express+Node e2e cases pass, and both fail with setLocal is not a function when the three source files are swapped back to master, so the new behaviour is pinned (including the defaultContext < setLocal < res.locals < handler-data order). Lint and the full unit suite are clean apart from the known Node 26 io/ws specs. Merges cleanly with #18395.
This closes the gap the MVC docs already assume: the NodeAdapter hint there names only res.render() as missing, while the hbs layout section calls app.setLocal('layout', ...). Shape mirrors how setLocal landed on Express in #5818 (adapter + application interface only, e2e in integration/).
Follow-up in docs.nestjs.com: add setLocal() to the NestNodeApplication method list in content/http/performance.md (L51).
Co-authored-by: Micael Levi L. Cavalcante <micalevisk@gmail.com>
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Issue Number: N/A
NestNodeApplicationhas nosetLocal()whileNestExpressApplicationhas itMVC docs use it for hbs layout (
app.setLocal('layout', 'layouts/app')) on same page as hint that same calls work withNodeAdapter, hint names onlyres.render()as missingWith
NestNodeApplicationtype this is compile error, on plainINestApplicationapp crashes at bootstrap withTypeError: app.setLocal is not a function, so MVC app ported from Express does not startOnly way to get application wide value into views on Node is object form
setViewEngine({ engine, defaultContext }), string formsetViewEngine('hbs')from docs has nowhere to put itWhat is the new behavior?
NodeAdapter.setLocal(key, value)stores value inViewRenderer,render()merges{ ...defaultContext, ...locals, ...data }sores.localsand handler data win over it, same order Express uses forapp.localsSeperate
localsobject,setViewEngine()does not reset it, so call before engine setup works like in docs, method returnsthisfor chainingValues only reach templates rendered with
@Render(),req.app.localsstays unsupported on Node sincereq.appthere is frozen settings stub with justget()Unit test in
node-adapter.spec.tsand shared Express/Node cases inintegration/http-adapters/e2e/views.spec.ts(value in every view,res.localswins over it) fail without the changeDoes this PR introduce a breaking change?
Method is new, without
setLocal()callrender()passes same object to engine as beforeOther information
Production cache keeps only compiled render function, data is passed per call, so
setLocal()after first render takes efect tooMVC docs already show
app.setLocal()in layout section, so no docs change here, list ofNestNodeApplicationmethods in performance.md can getsetLocal()in docs follow up