Closed mweberxyz closed 3 months ago
Are you referring to calls to @fastify/send?
Scratch that, we dont use this in this repo.
Essentially, the implementation would be changing this:
fastify.decorateReply(propertyName, async function (page) {
if (!page) {
this.send(new Error('Missing page'))
}
try {
const result = await renderer.apply(this, arguments)
if (!this.getHeader('Content-Type')) {
this.header('Content-Type', 'text/html; charset=' + charset)
}
if (minify && !isPathExcludedMinification(this)) {
this.send(minify(result, globalOptions.htmlMinifierOptions))
} else {
this.send(result)
}
} catch (err) {
this.send(err)
}
return this
})
to this:
fastify.decorateReply(propertyName, async function (page) {
if (!page) {
throw new Error('Missing page')
}
const promise = renderer.apply(this, arguments)
.then((result) => {
if (!this.getHeader('Content-Type')) {
this.header('Content-Type', 'text/html; charset=' + charset)
}
return result
})
if (minify && !isPathExcludedMinification(this)) {
return promise.then((result) => minify(result, globalOptions.htmlMinifierOptions))
}
return promise
})
Writing it out I realize a few things:
.then
would solve #372 await
in the call to renderer.apply
), or a net loss due to the additional overhead of the .then
closures. If there is any appetite for this change, I can push forward enough to get some numbers.Can you put it behind a flag for backward compatibility?
Prerequisites
Issue
I'd like to propose a semver-major change: remove remaining calls to
send
throughout the plugin. Instead, the async reply decorator would always return a promise resolving to the rendered html.This would necessitate docs, test, and code changes. It would require a prominent "Breaking Changes" to be added to README.md.
Pros
fs
operations or the renderers themselves could be handled by developer's definedsetErrorHandler
, or tracked byonError
hooksviewDecorator
could be removed, except to expose the existingclearCache
functionality. This would ensure all calls to the renderer setthis
to a valid request object (resolving #394)Cons
Samples
README.md - Quick start
Alternatives
As a less-invasive alternative, perhaps a reply decorator such as
${propertyName}Safe
or${propertyName}AsPromise
could be implemented, with all of the semantics described above, and keep the existing reply decorator as-implemented.