New middleware slows down routing in production
See original GitHub issueVerify 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
Issue Analytics
- State:
- Created a year ago
- Reactions:8
- Comments:24 (2 by maintainers)
Top Results From Across the Web
My Next.js API Routes Were Slow, Until I ... - YouTube
Next.js api routes can be made much faster with this easy trick. The trick is to convert the api route to use edge...
Read more >Node.js Performance: The Most Common Piece of Missing ...
js Performance: The Most Common Piece of Missing Middleware for Servers in Production. Ideally, Node.js isn't used for serving static assets, at ...
Read more >Routing slow - how to optimize? - Laracasts
In our application we have problems with performance. After some investigation it seems that adding routes have a big impact. Some tests in...
Read more >Routing in ASP.NET Core - Microsoft Learn
The sample is not intended for production use. A more complete version of an audit log middleware would: Log to a file or...
Read more >Performance Best Practices Using Express in Production
Use the compression middleware for gzip compression in your Express app. For example: const compression = require('compression') const express ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Without a fix for this, the framework is basically unusable right now …
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.
Is enough to drop the response time