question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

New middleware slows down routing in production

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:8
  • Comments:24 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
benfavrecommented, Nov 18, 2022

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

5reactions
cdev-entercommented, Nov 7, 2022

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found