ApplicationHooks have inconsistent reference to instance
Author: UzlopakCreated Aug 12, 2023Updated Sep 1, 2026
Labelsfeature requestgood first issuev5.xnotable-change
Prerequisites
- I have written a descriptive issue title
- I have searched existing issues to ensure the issue has not already been raised
Issue
While looking at the onListen hook PR, I realized it is not that strict on how the application hooks refer to the instance.
onRoutehas the reference to theinstancein thethisparameteronRegisterhas the reference to theinstancein theinstanceparameteronClosehas the reference to theinstancein theinstanceparameteronReadyhas the reference to theinstancein thethisreferenceonListenhas the reference to theinstancein thethisparameterpreClosehas the reference to theinstancein thethisreference
I dont see any pattern.
### Tasks
- [ ] https://github.com/fastify/fastify/pull/4972
- [ ] Document the this context more clearly for every hookHow to implement it:
We choose to convert the parameters into an object and bind the this context too:
https://github.com/fastify/fastify/issues/4967#issuecomment-1683826882
Source: fastify/fastify