Repository navigation
Add the "view engines" setting: try several engines for a view name without a known extension - #72
Merged
Merged
Conversation
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.
Coverage Report for CI Build 45026325404Coverage remained the same at 100.0%Details
Uncovered ChangesNo uncovered changes found. Coverage RegressionsNo coverage regressions found. Coverage Stats
💛 - Coveralls |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds the opt-in
view enginessetting, for the ROADMAP row "View lookup tries every registered engine for names without a known extension" (expressjs#2708, expressjs#3485, expressjs#3569).Why opt-in, and why a list
Upstream PR expressjs#3569 (boycce) tries every key of
app.engineswhen theview enginefile is missing, with no setting. That turns two errors existing apps get today into renders:view enginethat relies on "No default engine was specified" (for example a fallback route that catches it), andview enginefile 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"):app.enginesalso caches engines thatrequire()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.home.ejsnext tohome.pug): the app says which wins. The issue author suggested an ordered list there too.require(), asview enginedoes.Lookup order
view engineextension, when set (so an app that renders a file today renders the same one, even when another engine's file also exists);view enginesextension, in the listed order (leading dot optional, repeats skipped).Each extension is tried in every
viewsfolder before the next extension (extension first, then folder). Folder first would let aview enginesfile in the first folder beat theview enginefile 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 engineor 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.profilerendersuser.profile.pug, where 5.3.0 triesrequire('profile')(expressjs#2708). We don't stat the name as given first: that would let an unlisted, unregisteredindex.ejskeep loadingejsthroughrequire(), but makes the rule depend on the file system; listing or registeringejsdoes the same thing.Changes
src/view.ts: newextensionsview option. When it lists something and the name has no known extension,lookupExtensions(a private function, not a new prototype method) tries each extension throughview.lookup, so a subclass'slookup/resolvestill apply, and loads only the found file's engine. The engine loading moved unchanged intoloadEngine, shared by both paths.view.extensionsrecords what was tried; it is set only on this path.src/application.ts:app.renderpassesview enginesasextensionsonly when it is set, so a customviewclass gets the same options object as before. The lookup error getswith extension ".md", ".tmpl" or ".html"whenview.extensionsis set (the same list format as the folders).ApplicationSettingsgets'view engines'.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.app.settakes any setting intypings/, so no declaration change is needed.test/types/api.tssets it as an array and as a string.Behaviour
[]orfalse: no change. The existing tests pass unchanged, and the differential harness has no newallowedentry.Failed to lookup view "page" in views directory "/app/views" with extension ".pug", ".html" or ".ejs"(callback, ornext(err)fromres.render), witherr.view.extensionsset 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 cacheon: the found view is cached under the name, as before. A failed lookup isn't cached.view enginesand the parent's engines (their own first).viewsisn't inherited, because it has a default, as before.Proof
npm run lint,npm run typecheckandnpm 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,[]andfalse; the customviewclass options; order between engines;view enginefirst, even when listed later; leading dot and string form; folderindex; engine case;require()fallback, loaded only for the found file; a missing module; severalviewsfolders; 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, throughres.render); and view caching (on, off, a failed lookup).Planted breaks, each run against the full suite:
extensionsalways undefined)view enginetried last instead of firstview.extensionsnot set (no list in the error)Benchmark
Not on the request path.
app.renderreads one more setting, and only when it builds a view: always withview cacheoff, and once per name with it on. Nothing insrc/router/**,src/request.ts,src/response.tsorapp.handlechanges.Risks / follow-ups
View#enginestays unset when nothing is found on the new path. Nothing reads it then, becauseapp.rendererrors first.require()cached inapp.enginescounts as a known extension afterwards. That's the same objectapp.enginesexposes, so it's consistent, but worth knowing.allowedentry for new behaviour only.differences.mdsection. Merge them into one.