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

v12.2.x Middleware is invoked multiple times for a page for the same route request #38515

Closed
1 task done
bennettdams opened this issue Jul 11, 2022 · 3 comments
Closed
1 task done
Labels
bug Issue was opened via the bug report template. Middleware Related to Next.js Middleware

Comments

@bennettdams
Copy link
Contributor

bennettdams commented Jul 11, 2022

Verify canary release

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

Provide environment information

Operating System:
  Platform: linux
  Arch: x64
  Version: #1 SMP Fri Apr 2 22:23:49 UTC 2021
Binaries:
  Node: 16.15.0
  npm: 8.5.5
  Yarn: 1.22.15
  pnpm: N/A
Relevant packages:
  next: 12.2.2
  eslint-config-next: 12.2.2
  react: 18.2.0
  react-dom: 18.2.0

What browser are you using? (if relevant)

Chrome & Firefox

Describe the Bug

When using Middleware for a page, it is invoked multiple times for the same route request.

Here's a very basic example, an app with another /test route. In the Middleware, we log whenever it is invoked by a request for this page:

// Note: I'm using a root wildcard here, because in my code, I want to protect every single route for authentication
export const config = {
  matcher: ["/:path*"],
};

export default async function _middleware(req: NextRequest) {
  const pathname = req.nextUrl.pathname;

  if (pathname === "/test") {
    console.log(`🔵 ${new Date().toISOString()} | Request for "Test" page | Method: ${req.method}`);
  }

  return NextResponse.next();
}

Going to this page in the browser, the result is two console.logs, even though it should be one:

image


There is a similar recent issue, but there, the problem were HEAD requests. Looking at my example, the double invocation is done with two GET requests, so I think this issue is not related (or not fixed with whatever was the problem there).

Expected Behavior

Only one invokation per page request.

Link to reproduction

https://github.com/bennettdams/nextjs-middleware-invocation-bug

To Reproduce

Here is a reproduction repo you can use.

Or, here are the steps to manually reproduce the issue:

  1. Install via npx create-next-app@latest --ts --use-npm .

  2. Add a test page

    // test.tsx
    import { NextPage } from "next/types"
    
    const Test: NextPage = () => {
        return <p>Test</p>
    }
    
    export default Test
  3. Add Middleware that logs whenever it is invoked for our new Test page

    // middleware.ts
    import { NextRequest, NextResponse } from "next/server";
    
    // Note: I'm using a root wildcard here, because in my code, I want to protect every single route for authentication
    export const config = {
      matcher: ["/:path*"],
    };
    
    export default async function _middleware(req: NextRequest) {
      const pathname = req.nextUrl.pathname;
    
      if (pathname === "/test") {
          console.log(`🔵 ${new Date().toISOString()} | Request for "Test" page | Method: ${req.method}`);
      }
    
      return NextResponse.next();
    }
  4. npm run build and npm run start

  5. Go to /test in the browser. Reload the page.


I experienced this behavior for v12.2.0, v12.2.1 and v12.2.2.

As a comparison, I also checked this for v12.1.6. There, this problem does not exist. There's only one invocation as expected. To reproduce in the same repo:

  1. npm i next@12.1.6
  2. Rename middleware.ts to _middleware.ts and move it to the "pages" folder.
@bennettdams bennettdams added the bug Issue was opened via the bug report template. label Jul 11, 2022
@bennettdams bennettdams changed the title v12.2.0 Middleware is invoked multiple times for a page for the same route request v12.2.2 Middleware is invoked multiple times for a page for the same route request Jul 11, 2022
@bennettdams bennettdams changed the title v12.2.2 Middleware is invoked multiple times for a page for the same route request v12.2.x Middleware is invoked multiple times for a page for the same route request Jul 11, 2022
@balazsorban44 balazsorban44 added the Middleware Related to Next.js Middleware label Jul 11, 2022
@andrewfritz86
Copy link

I am seeing this as well, middleware being called multiple times per request.

@ijjk
Copy link
Member

ijjk commented Jul 11, 2022

Closing as a duplicate of #38267

@ijjk ijjk closed this as completed Jul 11, 2022
@github-actions
Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 11, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. Middleware Related to Next.js Middleware
Projects
None yet
Development

No branches or pull requests

4 participants