Skip to content

onRoute hook executed for endpoint declared before registering it #4044

@Eomm

Description

@Eomm

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

4.0.3

Plugin version

No response

Node.js version

18

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

latest

Description

When the onRoute hook is registered after a GET handler, it is executed for the HEAD route only.

Steps to Reproduce

const fastify = require('fastify')
const app = fastify()

app.addHook('onRoute', function inspector(routeOptions) {
  console.log('before ' + routeOptions.method)
})

app.get('/', function (request, reply) { })

app.addHook('onRoute', function inspector(routeOptions) {
  console.log('after ' + routeOptions.method)
})

app.ready()

Prints:

before GET
before HEAD
after HEAD

Expected Behavior

I think the after hook should not be executed.

It should print:

before GET
before HEAD

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugConfirmed bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions