#4967·fastify

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.

  • onRoute has the reference to the instance in the this parameter
  • onRegister has the reference to the instance in the instance parameter
  • onClose has the reference to the instance in the instance parameter
  • onReady has the reference to the instance in the this reference
  • onListen has the reference to the instance in the this parameter
  • preClose has the reference to the instance in the this reference

I dont see any pattern.

### Tasks
- [ ] https://github.com/fastify/fastify/pull/4972
- [ ] Document the this context more clearly for every hook

How 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