Skip to content

feat(node): let the node adapter set locals for every view - #18396

Merged
kamilmysliwiec merged 2 commits into
nestjs:masterfrom
thekhegay:feat-node-set-local
Nov 4, 2026
Merged

kamilmysliwiec merged 2 commits into
nestjs:masterfrom
thekhegay:feat-node-set-local

Conversation

@thekhegay

Copy link
Copy Markdown
Contributor

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

Issue Number: N/A

NestNodeApplication has no setLocal() while NestExpressApplication has it
MVC docs use it for hbs layout (app.setLocal('layout', 'layouts/app')) on same page as hint that same calls work with NodeAdapter, hint names only res.render() as missing
With NestNodeApplication type this is compile error, on plain INestApplication app crashes at bootstrap with TypeError: app.setLocal is not a function, so MVC app ported from Express does not start
Only way to get application wide value into views on Node is object form setViewEngine({ engine, defaultContext }), string form setViewEngine('hbs') from docs has nowhere to put it

What is the new behavior?

NodeAdapter.setLocal(key, value) stores value in ViewRenderer, render() merges { ...defaultContext, ...locals, ...data } so res.locals and handler data win over it, same order Express uses for app.locals
Seperate locals object, setViewEngine() does not reset it, so call before engine setup works like in docs, method returns this for chaining
Values only reach templates rendered with @Render(), req.app.locals stays unsupported on Node since req.app there is frozen settings stub with just get()
Unit test in node-adapter.spec.ts and shared Express/Node cases in integration/http-adapters/e2e/views.spec.ts (value in every view, res.locals wins over it) fail without the change

Does this PR introduce a breaking change?

  • Yes
  • No

Method is new, without setLocal() call render() passes same object to engine as before

Other information

Production cache keeps only compiled render function, data is passed per call, so setLocal() after first render takes efect too
MVC docs already show app.setLocal() in layout section, so no docs change here, list of NestNodeApplication methods in performance.md can get setLocal() in docs follow up

@micalevisk
micalevisk self-requested a review November 4, 2026 17:05

@micalevisk micalevisk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
@thekhegay
thekhegay requested a review from micalevisk November 4, 2026 17:33
@kamilmysliwiec
kamilmysliwiec merged commit bee9379 into nestjs:master Nov 4, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants