Skip to content

fix: callNotFound should run not-found preHandler regardless of registration order - #6965

Merged
climba03003 merged 2 commits into
fastify:mainfrom
mcollina:fix/call-not-found-prehandler-registration-order
Aug 23, 2026
Merged

climba03003 merged 2 commits into
fastify:mainfrom
mcollina:fix/call-not-found-prehandler-registration-order

Conversation

@mcollina

Copy link
Copy Markdown
Member

setContext() in four-oh-four.js takes a detached shallow snapshot of the
selected not-found context. When a route that calls reply.callNotFound()
is registered before setNotFoundHandler(), the snapshot is taken before
the not-found handler's preReady callback populates its lifecycle hooks,
so the copied context retains preHandler: null. reply.callNotFound() then
dispatches the not-found handler directly, skipping the preHandler that
setNotFoundHandler() declares.

This change uses the selected context as the prototype of the route-specific
404 context instead of copying it, keeping the route's own onSend value, so
the route-specific context stays linked to the live one and later hook
population is visible.

Adds a regression test covering the route-first declaration order.

…tration order

setContext() in four-oh-four.js took a detached shallow snapshot of the
selected not-found context. When a route that calls reply.callNotFound()
is registered before setNotFoundHandler(), the snapshot is taken before
the not-found handler's preReady callback populates its lifecycle hooks,
so the copied context retains preHandler: null. reply.callNotFound() then
dispatches the not-found handler directly, skipping the preHandler that
setNotFoundHandler() declares.

Use the selected context as the prototype instead, keeping the route's own
onSend value, so the route-specific 404 context stays linked to the live
one and later hook population is visible.

Adds a regression test covering the route-first declaration order.
Object.create already handles a null context; the ?? null fallback only
covered an unreachable undefined path and dropped four-oh-four.js branch
coverage below the enforced threshold.
@Tony133
Tony133 requested a review from a team August 19, 2026 07:43

@gurgunday gurgunday left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@climba03003 climba03003 added the backport 5.x Issue or pr that should be backported to Fastify v5 label Aug 23, 2026
@climba03003
climba03003 merged commit 4176096 into fastify:main Aug 23, 2026
32 checks passed
climba03003 pushed a commit that referenced this pull request Aug 23, 2026
…rdless of registration order (#6973)

Backport 4176096 from #6965.

Co-authored-by: Matteo Collina <[email protected]>
Co-authored-by: mcollina <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport 5.x Issue or pr that should be backported to Fastify v5

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants