Skip to content

Add the "view engines" setting: try several engines for a view name without a known extension - #72

Merged
abedmurrar merged 2 commits into
masterfrom
feat/views-engines
Nov 22, 2026
Merged

abedmurrar merged 2 commits into
masterfrom
feat/views-engines

Conversation

@abedmurrar

Copy link
Copy Markdown
Owner

Summary

Adds the opt-in view engines setting, for the ROADMAP row "View lookup tries every registered engine for names without a known extension" (expressjs#2708, expressjs#3485, expressjs#3569).

app.engine('pug', require('pug').__express)
app.engine('html', require('ejs').renderFile)
app.set('view engine', 'pug')
app.set('view engines', ['html', 'ejs'])

res.render('page') // page.pug, else page.html, else page.ejs

Why opt-in, and why a list

Upstream PR expressjs#3569 (boycce) tries every key of app.engines when the view engine file is missing, with no setting. That turns two errors existing apps get today into renders:

  • an app without view engine that relies on "No default engine was specified" (for example a fallback route that catches it), and
  • an app whose view engine file is missing, which today gets "Failed to lookup view" (a 500 or a custom 404), and would now render another engine's file of the same name.

An error becoming a render is a behaviour change an app can rely on, so this line keeps it behind a setting. Of the two opt-in forms, a list of extensions was chosen over a boolean ("try every engine in app.engines"):

  • Deterministic order. app.engines also caches engines that require() loaded on an earlier render (res.render('x.hbs') adds .hbs), so a boolean would make the lookup order depend on what was rendered before. A sub-app also walks its own engines before the inherited ones. The list is the order, in one place.
  • It settles the ambiguity raised on Auto file extension lookup with multiple view engines expressjs/express#3485 (home.ejs next to home.pug): the app says which wins. The issue author suggested an ordered list there too.
  • It needs no registration: a listed extension with no registered engine loads its module with require(), as view engine does.

Lookup order

  1. the view engine extension, when set (so an app that renders a file today renders the same one, even when another engine's file also exists);
  2. each view engines extension, in the listed order (leading dot optional, repeats skipped).

Each extension is tried in every views folder before the next extension (extension first, then folder). Folder first would let a view engines file in the first folder beat the view engine file in the second, which changes which file an app renders.

Names with a dot

With the setting on, an extension is "known" when an engine is registered for it (any case, as since #69), or it is the view engine or a listed extension (any case). A name with a known extension is looked up exactly as before. Any other extension is part of the name: user.profile renders user.profile.pug, where 5.3.0 tries require('profile') (expressjs#2708). We don't stat the name as given first: that would let an unlisted, unregistered index.ejs keep loading ejs through require(), but makes the rule depend on the file system; listing or registering ejs does the same thing.

Changes

  • src/view.ts: new extensions view option. When it lists something and the name has no known extension, lookupExtensions (a private function, not a new prototype method) tries each extension through view.lookup, so a subclass's lookup/resolve still apply, and loads only the found file's engine. The engine loading moved unchanged into loadEngine, shared by both paths. view.extensions records what was tried; it is set only on this path.
  • src/application.ts: app.render passes view engines as extensions only when it is set, so a custom view class gets the same options object as before. The lookup error gets with extension ".md", ".tmpl" or ".html" when view.extensions is set (the same list format as the folders). ApplicationSettings gets 'view engines'.
  • Docs: docs/api/application.md (settings table entry and a "Several template engines" section), docs/api/response.md, docs/guide/using-template-engines.md, docs/fork/differences.md (new "Additions" section), History.md (new "Additions" heading, since none of the existing headings fits), ROADMAP row.
  • Types: app.set takes any setting in typings/, so no declaration change is needed. test/types/api.ts sets it as an array and as a string.

Behaviour

  • Unset, [] or false: no change. The existing tests pass unchanged, and the differential harness has no new allowed entry.
  • Nothing found: Failed to lookup view "page" in views directory "/app/views" with extension ".pug", ".html" or ".ejs" (callback, or next(err) from res.render), with err.view.extensions set to the list. For a name with an unknown extension, this replaces 5.3.0's synchronous "Cannot find module" error, but only with the setting on.
  • view cache on: the found view is cached under the name, as before. A failed lookup isn't cached.
  • Sub-apps inherit view engines and the parent's engines (their own first). views isn't inherited, because it has a default, as before.

Proof

  • npm run lint, npm run typecheck and npm run test:types: clean.

  • npm run test-ci: 146 files, 8686 passed and 5 skipped. Coverage is 100% statements, branches, functions and lines, and the coverage-files check passes (all 23 source files).

  • 31 new tests in test/fork-view-engines.js. They cover: unset, [] and false; the custom view class options; order between engines; view engine first, even when listed later; leading dot and string form; folder index; engine case; require() fallback, loaded only for the found file; a missing module; several views folders; sub-apps (inherit and override); dotted names (unknown, registered in either case, listed, view engine, a trailing .); the error messages (one or several folders and extensions, a known extension, through res.render); and view caching (on, off, a failed lookup).

  • Planted breaks, each run against the full suite:

    Break Failing tests
    the core line removed (extensions always undefined) 20
    view engine tried last instead of first 3
    the known-extension check removed 5
    view.extensions not set (no list in the error) 4

Benchmark

Not on the request path. app.render reads one more setting, and only when it builds a view: always with view cache off, and once per name with it on. Nothing in src/router/**, src/request.ts, src/response.ts or app.handle changes.

Risks / follow-ups

  • View#engine stays unset when nothing is found on the new path. Nothing reads it then, because app.render errors first.
  • An engine that require() cached in app.engines counts as a known extension afterwards. That's the same object app.engines exposes, so it's consistent, but worth knowing.
  • No differential scenario: 5.3.0 ignores the setting, so any scenario would need an allowed entry for new behaviour only.
  • Merge conflicts: other additions PRs may add their own History "Additions" heading or differences.md section. Merge them into one.

A view name without a known extension tries the "view engine" extension,
then each extension listed in the new "view engines" setting, in order,
each one in every "views" folder before the next, and renders the first
file found (expressjs#2708, expressjs#3485; PR expressjs#3569 by boycce, reworked).

It is opt-in. Without the setting nothing changes: "No default engine was
specified" and "Failed to lookup view" stay as they are, and a custom
"view" class gets the same options object as before.

With it, an extension with no engine that is not listed is part of the
name ("user.profile" renders "user.profile.pug"), only the found file's
engine is loaded, and the lookup error lists the extensions tried.
@coveralls

Copy link
Copy Markdown

Coverage Report for CI Build 45026325404

Coverage remained the same at 100.0%

Details

  • Coverage remained the same as the base build.
  • Patch coverage: 182 of 182 lines across 2 files are fully covered (100%).
  • No coverage regressions found.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 9679
Covered Lines: 9679
Line Coverage: 100.0%
Coverage Strength: 31536.69 hits per line

💛 - Coveralls

@abedmurrar
abedmurrar merged commit e206598 into master Nov 22, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants