We're using the middleware and we just found out it executes things twice, even the application itself. I know that middleware is being used in resources as well, so I put a matcher and removed the "favicon" because I heard it can cause this bug.
When we use the middleware, and console.log in "_app.tsx", we see that it renders twice. Also I put a counter in middleware.ts and every page I enter I see that it increases the counter by two.
My middleware.ts page looks like:
import { NextRequest, NextResponse } from "next/server";
let counter = 0;
export function middleware(request: NextRequest) {
const response = NextResponse.next();
counter++;
console.log("middleware #", counter);
return response;
}
Github repo to reproduce: https://github.com/ornakash/reproduction-template-nextjs
I only see it twice in the server's terminal (in browser I see only one console.log). Do you have any idea why it happens?