Fastify hooks on the route opts type
See original GitHub issuePrerequisites
- I have written a descriptive issue title
- I have searched existing issues to ensure the feature has not already been requested
🚀 Feature Proposal
This would allow to simplify the custom request properties example on the readme https://github.com/fastify/fastify-nextjs#custom-properties-on-the-request-object
Instead of needing a register
with a prefix, you could add the hook on the .next
call directly.
fastify.next('/hello', {
onRequest: function (request, reply, done) {
// define a custom property on the request
request.raw.customProperty = { hello: "world" }
// OR make the instance of fastify-redis available in the request
request.raw.redisInstance = instance.redis
done()
}
});
Motivation
Currently it is not possible (as far as I can tell?) to apply a hook to the routes handled by the next plugin only, unless there is a route prefix like in the readme example.
Example
fastify.next('*', {
{
onRequest: () => {
// hook contents here
},
},
});
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
Hooks - Fastify
Hooks. Hooks are registered with the fastify.addHook method and allow you to listen to specific events in the application or request/response lifecycle.
Read more >Policy hooks using fastify.route method - Medium
In this blog, how policies are handled by middleware operations at the route level with help of the fastify route method using pre-handler...
Read more >Apply Hooks only to some routes · Issue #185 · fastify/help
What I understood is that if in my plugin I register the hooks like that module.exports = function (fastify, opts, done) { fastify.addHook(....
Read more >What is the order of execution of the same-type hooks in fastify?
This shows that the hooks in the options for the routes are always added after the addHook handlers. How Fastify executes hooks. This...
Read more >Hooks - Fastify
Hooks. By using the hooks you can interact directly inside the lifecycle of Fastify. There are five different Hooks that you can use...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
A PR would be highly welcomed.
I found that encapsulating the
.next(‘*’)
in a plugin (with.register
) fixed my issue - now theonRequest
is only called for requests handled by Next.js.I also have an early return in the
onRequest
handler for public static files:I was going to try to find a better way of doing this - maybe just disabling static file serving in the plugin and doing it with a separate plugin instead.