-
Notifications
You must be signed in to change notification settings - Fork 6.3k
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
fix: use correct matcher in middleware.ts
#5604
Conversation
Signed-off-by: Balázs Orbán <info@balazsorban.com>
Signed-off-by: Balázs Orbán <info@balazsorban.com>
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
middleware.ts
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, but I aint going to approve this change. I understand that this enables the static analysis at build-time, but this PR doesn't fix any issue, actually, it breaks the custom "middleware" matching we've introduced.
The whole point of the createMiddleware
is allowing us to merge multiple middlewares or have one or more. Since very unfortunately, Next.js does not support more than one middleware.
Let me know if there are any alternatives or if this change is required for well, route matching to work at all, and if that's the case, I still cannot approve this PR without a solution that would allow us to have more than one matcher, or a matcher that comes from our "custom" middleware creator. (I'm genuinely all fine with any change on the custom setup, if you feel like it, you can completely refactor this code in a way that makes you happy or that solves our issues 🙇) Now, suppose there's no possibility at all to have something similar to what we're designing. In that case, I'd request the change to altogether remove our custom middleware engine and simply apply the only middleware we have now. (And I'd request pretty please for Vercel to consider supporting multiple middlewares) |
@balazsorban44 a quick question, since the matcher is ignored, does that mean the middleware runs for every route? Wouldn't https://nextjs.org/docs/app/building-your-application/routing/middleware#conditional-statements conditional statements still apply here? Because the docs say this: So would we still get "billed" (aka the middleware run, even if there are these conditional statements)? |
Hey @styfle sorry for pinging you! Not sure if @balazsorban44 has been busy lately. Do you have any extra insights here? I'm definitely curious about this, because I legit see a huge amount of invocations on the Edge Middleware and I wonder if (by any chance) this would alleviate it? |
Sorry, haven't seen this before now. When you have a conditional statement within the Middleware code, it's still going to run for the route, you can just choose to not do execute any logic by returning early. The matcher config is the one that can determine on the platform (like Vercel) when the Middleware should be skipped. This configuration needs to be static so it's predictable at build-time. |
Yes, it would reduce middleware invocations. Thats why the |
Ty. I've read that documentation, some parts are kinda confusing, hence I thought that dynamic matching was going to work due to making rules during the invocation. Anyhow, can you review this PR? #5613 |
@ovflowd Just to be clear, you only want middleware to run on the homepage ( |
Yup, at least for that one. |
📦 Next.js Bundle Analysis for nodejs.orgThis analysis was generated by the Next.js Bundle Analysis action. 🤖 This PR introduced no changes to the JavaScript bundle! 🙌 |
Closing due to #5613 |
Description
The Next.js Middleware did not correctly export a matcher. There is no top-level export of
matcher
.Based on
nodejs.org/middlewares/detectLanguage.ts
Line 47 in 625b2ab
Validation
Refer to the Next.js docs: https://nextjs.org/docs/app/building-your-application/routing/middleware#matcher
Check List
npx turbo lint
to ensure the code follows the style guide. And runnpx turbo lint:fix
to fix the style errors if necessary.npx turbo format
to ensure the code follows the style guide.npx turbo test
to check if all tests are passing, and/ornpx turbo test:snapshot
to update snapshots if I created and/or updated React Components.