vercel / vercel/next.js

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

Open
#38,273 39 comments 20 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug linear: next
Dominant language
JavaScript
Stars
142k
Forks
32.4k
Avg merge
2d 14h
Merged PRs (30d)
351

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the root-level middleware.js reproduction and run it in production with next start, comparing the behavior against the previous _middleware.js setup. Investigate the production routing slowdown described by the example, and consider the issue done when production routing no longer regresses while the lowercase redirect still works.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, next.js
Domain
backend, web-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.