Skip to content

routerOptions cannot be reused across multiple fastify instances #6512

@barbeque-squared

Description

@barbeque-squared

Prerequisites

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

Fastify version

5.7.4

Plugin version

No response

Node.js version

25.6.1

Operating system

Linux

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

Archlinux

Description

After the upgrade to 5.7.4, we started getting the deprecation warning about how we need to use routerOptions instead of setting them directly. But when I changed this, I started running into #4068 but only in our tests. While trying to create a minimal reproducible example for it, I got yet another error:
TypeError: Cannot read properties of null (reading 'server')

Context: in one of our tests we have multiple fastify instances. Some of those instances are fastify+options+someplugins, other instances are fastify+options+allroutes, yet others are fastify+options+oneroute. One of the options is maxParamLength: 2048. We have this defined in a separate file, and all the fastify instances use the same options object. The bolded part is important because if you do:

  • const options = { maxParamLength: 2048 } = you'll get the deprecation warning a lot of times, but things will actually work
  • const options = { routerOptions: { maxParamLength: 2048 } } = tests start erroring and/or timing out

In the MRE I've reduced this down even further. and also included 3 ways of making it "work" again, but none of them make any sense to me. Essentially, the moment I include routerOptions in my options and it's set to something other than undefined, I suddenly can't reuse it anymore? The "fix" with calling .ready() is also strange, because historically we never called that, and in the MRE it's specifically app1 that it needs to be called on, which isn't where the error occurs.

We upgraded from a fastify 5.2.1 straight to 5.7.4, but as long as we don't address the deprecation warning, everything works fine. It's specifically addressing the deprecation warning that causes the issue.

Link to code that reproduces the bug

https://github.com/barbeque-squared/fastify-routeroptions

Expected Behavior

I think being able to reuse the options object? I don't know if not being able to is a deliberate choice or not.

Of course I can rewrite my code so that instead of an export const I make a function that returns the options object, but then I'd also need to annotate it that people shouldn't reuse the function result. I'm also okay with having to call .ready() the whole time, but then the error when you forget it (see the MRE) should be improved.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions