From c9ecf7b658388ccaa2b8470aabad92aabde458a2 Mon Sep 17 00:00:00 2001 From: AkaHarshit <104684660+AkaHarshit@users.noreply.github.com> Date: Sun, 1 Feb 2026 08:21:17 +0530 Subject: [PATCH] feat: Allow passing null or undefined as the value for options in app.render (#6903) * fix: allow null options in app.render * fix: ensure options are initialized to an empty object in app.render * docs: add history entry --------- Co-authored-by: Sebastian Beltran --- History.md | 6 ++++++ lib/application.js | 2 +- test/app.render.js | 18 ++++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/History.md b/History.md index 0c5334c1..571cd0e4 100644 --- a/History.md +++ b/History.md @@ -4,6 +4,12 @@ * Improve HTML structure in `res.redirect()` responses when HTML format is accepted by adding ``, ``, and `<body>` tags for better browser compatibility - by [@Bernice55231](https://github.com/Bernice55231) in [#5167](https://github.com/expressjs/express/pull/5167) +* When calling `app.render` with options set to null, the locals object is handled correctly, preventing unexpected errors and making the method behave the same as when options is omitted or an empty object is passed - by [AkaHarshit](https://github.com/AkaHarshit) in [#6903](https://github.com/expressjs/express/pull/6903) + + ```js + app.render('index', null, callback); // now works as expected + ``` + ## ⚡ Performance * Avoid duplicate Content-Type header processing in `res.send()` when sending string responses without an explicit Content-Type header - by [@bjohansebas](https://github.com/bjohansebas) in [#6991](https://github.com/expressjs/express/pull/6991) diff --git a/lib/application.js b/lib/application.js index 838b882a..47be2a20 100644 --- a/lib/application.js +++ b/lib/application.js @@ -523,7 +523,7 @@ app.render = function render(name, options, callback) { var cache = this.cache; var done = callback; var engines = this.engines; - var opts = options; + var opts = options || {}; var view; // support callback function as second arg diff --git a/test/app.render.js b/test/app.render.js index ca15e761..bd65ce10 100644 --- a/test/app.render.js +++ b/test/app.render.js @@ -331,6 +331,24 @@ describe('app', function(){ }) }) + it('should accept null or undefined options', function (done) { + var app = createApp() + + app.set('views', path.join(__dirname, 'fixtures')) + app.locals.user = { name: 'tobi' } + + app.render('user.tmpl', null, function (err, str) { + if (err) return done(err); + assert.strictEqual(str, '<p>tobi</p>') + + app.render('user.tmpl', undefined, function (err2, str2) { + if (err2) return done(err2); + assert.strictEqual(str2, '<p>tobi</p>') + done() + }) + }) + }) + describe('caching', function(){ it('should cache with cache option', function(done){ var app = express();