Repository navigation
Conversation
Puppo
marked this pull request as ready for review
November 13, 2026 09:58
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Standardize how all application lifecycle hooks expose the associated Fastify instance so both
functiondeclarations and arrow functions have consistent access to the encapsulated scope (instance === this).Motivation
Previously, application hooks exposed the Fastify instance inconsistently (#4967):
onRoute,onReady,onListen, andpreCloseboundthisto the Fastify instance without passing it as an argument, preventing arrow functions from accessing the encapsulated instance.onCloseboundthisand passedinstanceas the first argument.onRegisterboundthisto the parent instance where.register()was called, while passing the newly created child scope as the first argument (instance !== this).Hook Signatures &
thisBinding Overview (v5 vs. v6)thisto the associated Fastify instance and passes that sameinstance(instance === this) as the first positional argument. Zero-arg and single-arg sync/async lifecycle handlers (() => {},async () => {},async (instance) => {}) continue to work seamlessly; callback-style handlers now take(instance, done).lifecycleHooks): All 10 Request/Reply hooks (includingonTimeoutandonRequestAbort) already bindthisto the route's encapsulatedFastifyInstanceand expose the same instance to arrow functions viarequest.server(andreply.serverwherereplyis available), so their signatures remain unchanged (No changes).thisBinding (function)onRouteinstance(1st arg)(routeOptions)(instance, routeOptions)onRegisterinstance(parent),newInstance(child)(childInstance, opts)(instance, newInstance, opts)onReadyinstance(1st arg)(done)/async ()(instance, done)/async (instance)onListeninstance(1st arg)(done)/async ()(instance, done)/async (instance)preCloseinstance(1st arg)(done)/async ()(instance, done)/async (instance)onCloseinstance(1st arg)(instance, done)/async (instance)(instance, done)/async (instance)onRequestFastifyInstancerequest.server/reply.server(request, reply, done)/async (request, reply)(request, reply, done)/async (request, reply)preParsingFastifyInstancerequest.server/reply.server(request, reply, payload, done)/async (request, reply, payload)(request, reply, payload, done)/async (request, reply, payload)preValidationFastifyInstancerequest.server/reply.server(request, reply, done)/async (request, reply)(request, reply, done)/async (request, reply)preHandlerFastifyInstancerequest.server/reply.server(request, reply, done)/async (request, reply)(request, reply, done)/async (request, reply)preSerializationFastifyInstancerequest.server/reply.server(request, reply, payload, done)/async (request, reply, payload)(request, reply, payload, done)/async (request, reply, payload)onSendFastifyInstancerequest.server/reply.server(request, reply, payload, done)/async (request, reply, payload)(request, reply, payload, done)/async (request, reply, payload)onResponseFastifyInstancerequest.server/reply.server(request, reply, done)/async (request, reply)(request, reply, done)/async (request, reply)onErrorFastifyInstancerequest.server/reply.server(request, reply, error, done)/async (request, reply, error)(request, reply, error, done)/async (request, reply, error)onTimeoutFastifyInstancerequest.server/reply.server(request, reply, done)/async (request, reply)(request, reply, done)/async (request, reply)onRequestAbortFastifyInstancerequest.server(request, done)/async (request)(request, done)/async (request)Summary of Changes
lib/hooks.js,lib/plugin-override.js,lib/route.js,fastify.js):instanceas the first argument inhookRunnerApplication(onReady,preClose),onListenHookRunner(onListen),onRoute, andonRegister.fn.length >= 2) andFST_ERR_HOOK_INVALID_ASYNC_HANDLERvalidation for the new parameter counts.types/hooks.d.ts,types/instance.d.ts,test/types/hooks.tst.ts):onRouteHookHandler,onRegisterHookHandler,onReady*,onListen*,preClose*,onClose*) and preserveTypeProvidergeneric propagation onfastify.addHook(...).test/hooks-application-instance.test.jscovering root, arrow functions, nested child/grandchild encapsulation,fastify-plugin, and 0/1-arg sync/async handlers, and assertinstance === thisacross existing hook test suites (includingthisandrequest.serverencapsulation checks ononTimeoutandonRequestAbort).docs/Reference/Hooks.md(covering both Application Hooks andthis/request.serveron Request/Reply Hooks),docs/Reference/TypeScript.md,docs/Guides/Plugins-Guide.md, andexamples/hooks.js.Closes #4967
Checklist
npm run test && npm run benchmark --if-presentand the Code of conduct