Closed mweberxyz closed 5 months ago
The README after changes: https://github.com/mweberxyz/point-of-view/blob/feat/view-async/README.md
The only difference between fastify.view
and reply.viewAsync
when called to render the html into a string inside a route handler is that the Content-Type: text/html..
header will be set by the latter, if Content-Type has not already been set.
This has been sitting for awhile - anything needed to get it merged? It's semver-minor due to purely additive functionality.
Closes #394 and closes #412
The purpose of this PR is to implement a new reply decorator, called
viewAsync
by default.viewAsync
is similar toview
, except it does not callreply.send
on template render or error, and instead returns a promise (to be returned from the route handler and allow fastify hook chain to take over).See https://github.com/fastify/point-of-view/issues/394 and https://github.com/fastify/point-of-view/issues/412 for context. 412 will be closed by this PR, though maybe it should be revisited in the future, if the legacy
view
should be replaced withviewAsync
as the default - would be a semver-major change and would probably make most sense to coincide with a major fastify version bump.Approach
asyncRender
async functionfastify.viewAsync
decoratorasyncRender
for existingfastify.view
decoratorasyncRender
for existingreply.view
decoratorWith this PR, all exposed decorators utilize the same underlying implementation of the rendering function, so each only contains logic specific to the needs of that decorator.
Tests
Sufficient tests added to be thoroughly confident the changes work as expected. Of particular interest:
setErrorHandler
works as expectedasyncPropertyName
andpropertyName
options work as expectedBenchmarks
fastify-viewAsync
, identical tofastify.js
but usesreply.viewAsync
instead ofreply.view
All within run-to-run variance (my laptop, M1 mac @ Node v20):
Docs
Cleanup, re-organize for readability, and add documentation for new decorator:
viewAsync
Providing a layout on render
to be beforeRendering the template into a variable
, so the former can reference the latter and it makes sense reading from top-to-bottomMigrating from view to viewAsync
Checklist
npm run test
andnpm run benchmark