Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global middleware not running on Nest > 10.3.2 (fastify, global prefix) #134

Closed
xtrinch opened this issue Apr 3, 2024 · 8 comments
Closed
Labels
external bug The problem related to a 3rd party tool

Comments

@xtrinch
Copy link

xtrinch commented Apr 3, 2024

When upgrading nestjs from 10.3.2 to anything above that, my global middleware stops running.

Namely, this middleware:

@Module({
  controllers: [AppController],
  providers: [AppService],
  imports: compact([
    ClsModule.forRoot({
      global: true,
      middleware: {
        mount: true,
        setup: (cls: ClsService<ClsStore>, req) => {
          // something -> this does not execute at all in 10.3.4
        },
      },
    }),
...

No issues in any of the below versions. Using latest nestjs-cls 4.3.0

Any ideas?

@Papooch Papooch added the investigation Why is this happening? label Apr 3, 2024
@Papooch
Copy link
Owner

Papooch commented Apr 3, 2024

Thanks for reporting, I'll look into it.

Let me ask you some clarifying questions:

  • Are you on express or fastify?
  • Are you using global prefix (app.setGlobalPrefix)?

@xtrinch
Copy link
Author

xtrinch commented Apr 3, 2024

Fastify,
global prefix /api

@Papooch
Copy link
Owner

Papooch commented Apr 3, 2024

I suspect the "fix" in this NestJS release might be the cause.

The mount option binds the ClsMiddleware to (.*). Could you try creating a custom middleware and mounting it to (.*), too, to see if it gets executed or not? If it does, then it's a bug in this library, otherwise it's a bug in NestJS and needs to be reported.

@Papooch Papooch changed the title Global middleware not running Global middleware not running on Nest > 10.3.2 (fastify, global prefix) Apr 3, 2024
@Papooch
Copy link
Owner

Papooch commented Apr 3, 2024

I have investigated on my side, updated NestJS to 10.3.7 and added many missing tests to the test suite involving Fastify and global prefix (see PR #135), but I was not able to reproduce the issue. All seems to work correctly.

Can I ask you for a minimal reproduction which shows the incorrect behavior?

@Papooch Papooch added the needs info Waiting for more information from the OP label Apr 3, 2024
@Papooch
Copy link
Owner

Papooch commented Apr 4, 2024

So the context gets initialized, but the setup function is not executed? That's beyond strange, execution of that code is controlled by ClsMiddleware, not Nest.

I tested that yesterday on latest version of everything with a basic console log and it did work.

Are you sure you only updated Nest and not other libraries that might interfere?

Can you share a minimal repro of your issue. I am not sure I'd be able to help you otherwise.

@xtrinch
Copy link
Author

xtrinch commented Apr 4, 2024

Sorry, I commented to hastily. I did some more testing, and for me, it turns out no middleware gets executed at all - not even a custom one that I add. I'll try to provide a reproduction repo but at the end of the day I don't think this is an issue with nestjs-cls at all. I might have some mismatched dependencies or something I'm not sure yet

@Papooch
Copy link
Owner

Papooch commented Apr 4, 2024

Make sure to also update @nestjs/platform-fastify to the latest version, and do not install fastify.

@xtrinch
Copy link
Author

xtrinch commented Apr 5, 2024

Managed to boil it down to the exclude param of global prefix - opened an issue here nestjs/nest#13401

@Papooch Papooch added external bug The problem related to a 3rd party tool and removed investigation Why is this happening? needs info Waiting for more information from the OP labels Apr 5, 2024
@Papooch Papooch closed this as completed Apr 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
external bug The problem related to a 3rd party tool
Projects
None yet
Development

No branches or pull requests

2 participants