I am currently working on a NextJS application that includes an ISR page fetching data from Supabase. There is a middleware in place to record a page visit before the page is rendered.
export async function middleware(
request: NextRequest,
fetchEvent: NextFetchEvent
) {
console.log('request.nextUrl.pathname', request.nextUrl.pathname);
if (request.nextUrl.pathname.split('/').length === 2) {
return linkVisitMiddleware(request, fetchEvent);
}
}
However, I've noticed that the middleware gets called 2 or 3 times (the numbers seem to fluctuate for some unknown reason).
Whenever this happens, the following entries are logged on the server:
request.nextUrl.pathname /slug
request.nextUrl.pathname /slug
request.nextUrl.pathname /slug
Interestingly, this behavior only occurs in the production environment when using the command next start
. When running the application locally with next serve
, it only runs once as expected.
I have read about the reactStrictMode
but implementing it did not resolve the issue.
Noteworthy mentioning, I have tested this on NextJS versions 13.1.1
, 13.1.6
& 13.3.0