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

[NEXT-696] New middleware slows down routing in production #38273

Open
1 task done
ch3rn1k opened this issue Jul 3, 2022 · 32 comments
Open
1 task done

[NEXT-696] New middleware slows down routing in production #38273

ch3rn1k opened this issue Jul 3, 2022 · 32 comments
Labels
bug Issue was opened via the bug report template. linear: next Confirmed issue that is tracked by the Next.js team.

Comments

@ch3rn1k
Copy link

ch3rn1k commented Jul 3, 2022

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
Platform: win32
Arch: x64
Version: Windows 10 Pro
Binaries:
Node: 16.14.2
npm: N/A
Yarn: N/A
pnpm: N/A
Relevant packages:
next: 12.2.0
eslint-config-next: 12.2.0
react: 18.2.0
react-dom: 18.2.0

What browser are you using? (if relevant)

Chrome 103.0.5060.66

How are you deploying your application? (if relevant)

next start

Describe the Bug

New middleware slows down on routing in production (dev is okay), example code that slows down:

import { NextResponse } from 'next/server';

const Middleware = (req) => {
  /** To lower case redirect. */
  if (req.nextUrl.pathname === req.nextUrl.pathname.toLowerCase())
    return NextResponse.next();

  return NextResponse.redirect(new URL(req.nextUrl.origin + req.nextUrl.pathname.toLowerCase()));
};

export default Middleware;

Expected Behavior

Work as expected with no slow down, with previous _middleware.js there was no problem

To Reproduce

Create middleware.js file under root folder and use code from example

NEXT-696

@ch3rn1k ch3rn1k added the bug Issue was opened via the bug report template. label Jul 3, 2022
@ch3rn1k ch3rn1k changed the title New middleware slows down routing New middleware slows down routing in production Jul 3, 2022
@imangm
Copy link

imangm commented Jul 3, 2022

@ch3rn1k I have the similar issue but the reason (at least for me) is that the pages are being re-rendered if I use middleware.js in the root. I described it here: #38267. Could you please check and confirm if you have the same issue?

@ch3rn1k
Copy link
Author

ch3rn1k commented Jul 3, 2022

@ch3rn1k I have the similar issue but the reason (at least for me) is that the pages are being re-rendered if I use middleware.js in the root. I described it here: #38267. Could you please check and confirm if you have the same issue?

Looks same so, but I got no error or slow work when no checks inside, like this:

import { NextResponse } from 'next/server';

const Middleware = (req) => {
 return NextResponse.next();
};

export default Middleware;

@willemliufdmg
Copy link

willemliufdmg commented Jul 4, 2022

I suppose this can also play a part in the performance degradation I filed in an issue a bit earlier:

@jalada
Copy link

jalada commented Sep 16, 2022

Also seeing this, also seeing the lack of slowdown when there are no checks / redirects as per @ch3rn1k's example. With 12.3, too.

@ch3rn1k
Copy link
Author

ch3rn1k commented Sep 16, 2022

Well, same as before. Removed middleware when problem is started and no in use for me until this will be fixed.

@TurkaljGoran
Copy link

Also seeing this, tried it on 12.3.2 canary and the issue persists, we're talking massive slowdown.
It seems to read many .json files all at once (loading SSR product detail page for example), pending for 10-20 seconds.
Everything goes back to normal if middleware.ts is removed.

Everything was normal in the old _middleware.ts which was used in /pages

@11koukou
Copy link
Contributor

I can confirm that the issue persists with node version 16.18.0 or 16.17.1. In any node version 16.5.0 and lower that I have tested the issue does not exist

@GrantGryczan
Copy link

GrantGryczan commented Oct 31, 2022

It may or may not be relevant that the middleware is now re-initialized on each request when in dev mode, rather than being initialized only once and then persisting like in previous versions.

@michal-hans
Copy link

We are experiencing slow down when middleware is used together with large number of redirects. Probably because it affects also client side routing as described in documentation: "Redirects are not applied to client-side routing (Link, router.push), unless Middleware is present and matches the path."

Any idea how we can handle 30-60k of redirects of old urls (which cannot be changed to rules)?
We are using Cloudlfront in front of our app but there are also smaller limits. So far we are working good with disabled middleware but thats only a temporary workaround.

@cdev-enter
Copy link

The problem also exists in the 13 release. Just the presence of a middleware increases the response time tenfold.

I did a test with a fresh next@canary release and a request to http://localhost:3000/vercel.svg takes 4ms without middleware.
The same request with a middleware present, takes 150 - 300ms on my local machine. There seems to be a problem with the middleware loader, as the middleware does not have to do anything to cause that kind of delay.

// middleware.ts

export function middleware() {}

Is enough to drop the response time

@baraeb92
Copy link

baraeb92 commented Nov 7, 2022

@ch3rn1k @imangm @willemliufdmg @jalada @TurkaljGoran @11koukou @michal-hans @cdev-enter

Does this change not fix the problem? #42547

@cdev-enter
Copy link

@baraeb92 in 13.0.3-canary.1 this still exists.

@artyom-dorokhov
Copy link

artyom-dorokhov commented Nov 10, 2022

updating from 13.0.2 to the latest canary release fixed the issue in my project

@gurleensethi
Copy link

Having the same problem with the latest release 😢

@benfavre
Copy link

Without a fix for this, the framework is basically unusable right now ...

@alexandru-wd
Copy link

It s not only in production. It happens in dev also. I just tested middleware.js with next 12.3.3 and it works perfectly. When replacing next version with 13.0.4, i get +1 second load time on my page.

@gurleensethi
Copy link

Do we have any update on this issue? Is it acknowledged by the Next team?

@metawise
Copy link

metawise commented Dec 6, 2022

Also having same problem. Wait time is too high. Because of that Web Core Vitals hurting.

@salokod
Copy link

salokod commented Dec 14, 2022

Also having this issue - is there any previous version of Next / middleware that doesn't suffer from this?

@mgreenw
Copy link

mgreenw commented Dec 14, 2022

I think 12.3.3 is safe - haven't seen any issues with it while using middleware for the past few weeks.

@baraeb92
Copy link

Can anyone provide a reproduction? I think it will get resolved faster if there is a reproduction that the NextJS team can take a look at?

@xyba1337
Copy link

Next.js v13.0.7 still extremely slow, to a point where middleware is literally unusable

@11koukou
Copy link
Contributor

Can't confirm this with v13.0.7. Strangely enough, everything looks fine , I had this issue with previous releases and I definitely know how it is. Tested in node version 16.14.1 and 16.18.0

@xyba1337
Copy link

Can't confirm this with v13.0.7. Strangely enough, everything looks fine , I had this issue with previous releases and I definitely know how it is. Tested in node version 16.14.1 and 16.18.0

Even in development environment? Tested it on pretty much every version from 13.0 to the latest one without any success.
Also tried node version 16.14.1 and 16.18.0 just like you did, still extremely slow. Not having this issue with everything under 13.0 where it still used the old _middleware.js inside pages.

@jasantaella15
Copy link

jasantaella15 commented Jan 2, 2023

Adding a matcher to exclude resources helped in my case, of course it depends on your use case, in my case I didn't need a middleware for api/resources calls:

  matcher: [
    /*
     * Match all request paths except for the ones starting with:
     * - api (API routes)
     * - _next/static (static files)
     * - favicon.ico (favicon file)
     * - assets (static assets)
     */
    '/((?!api|_next/static|favicon.ico|assets).*)'
  ]
};

@gurleensethi
Copy link

13.1 got released recently with some updates to middleware, is this issue fixed?

@wottpal
Copy link

wottpal commented Feb 11, 2023

Same issues. Middleware, even with a lot of exceptions, slows down (dev & production) builds.

@majidkuhail
Copy link

I've tested this out with: 13.1.6 canary release and its slowing down requests on dev mode (~80ms to 250ms delay per request).
But on a production build its not causing any noticeable delay after the first request has been made to the server.

So it appears #42547 did solve the issue for production only.

However, The delay is still quite disruptive while working on dev.

I understand cache is disabled on the dev build to allow the compiler to catch code changes, but since the middleware.ts code is rarely changed, Would it be wise to provide a config option to force middleware cache in dev mode too?

@feedthejim feedthejim added the linear: next Confirmed issue that is tracked by the Next.js team. label Feb 28, 2023
@feedthejim feedthejim changed the title New middleware slows down routing in production [NEXT-696] New middleware slows down routing in production Feb 28, 2023
@TechnoRahmon
Copy link

Adding a matcher to exclude resources helped in my case, of course it depends on your use case, in my case I didn't need a middleware for api/resources calls:

  matcher: [
    /*
     * Match all request paths except for the ones starting with:
     * - api (API routes)
     * - _next/static (static files)
     * - favicon.ico (favicon file)
     * - assets (static assets)
     */
    '/((?!api|_next/static|favicon.ico|assets).*)'
  ]
};

thanks this improved the performance a little bit

@mgreenw
Copy link

mgreenw commented Mar 16, 2023

I understand cache is disabled on the dev build to allow the compiler to catch code changes, but since the middleware.ts code is rarely changed, Would it be wise to provide a config option to force middleware cache in dev mode too?

Similar issues:
#43999
#44053

@yiyb0603
Copy link

yiyb0603 commented Aug 10, 2023

Has Anyone solved this problem?

@pepijn-vanvlaanderen

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue was opened via the bug report template. linear: next Confirmed issue that is tracked by the Next.js team.
Projects
None yet
Development

No branches or pull requests