middleware render two times in dynamic routes
Unanswered
Transvaal lion posted this in #help-forum
Transvaal lionOP
I have a simple middleware, why in dynamic routes it runs two times?
I want to only run once because i need to refresh token.
middleware: GET /admin/agencys/a736d0c0-3db2-4bca-9d78-b9267e9406d2
middleware: GET /admin/agencys/a736d0c0-3db2-4bca-9d78-b9267e9406d2
GET /admin/agencys/a736d0c0-3db2-4bca-9d78-b9267e9406d2 200 in 62ms
GET /admin/agencys/a736d0c0-3db2-4bca-9d78-b9267e9406d2 200 in 47ms
I want to only run once because i need to refresh token.
middleware: GET /admin/agencys/a736d0c0-3db2-4bca-9d78-b9267e9406d2
middleware: GET /admin/agencys/a736d0c0-3db2-4bca-9d78-b9267e9406d2
GET /admin/agencys/a736d0c0-3db2-4bca-9d78-b9267e9406d2 200 in 62ms
GET /admin/agencys/a736d0c0-3db2-4bca-9d78-b9267e9406d2 200 in 47ms
28 Replies
Asiatic Lion
Hello, i have the same problem : (
In middleware, you can use this.
export const config = {
matcher: ['/((?!_next|static).*)'],
};
export const config = {
matcher: ['/((?!_next|static).*)'],
};
@Asiatic Lion , @Transvaal lion
Transvaal lionOP
No, same problem
middleware: GET /admin/agencys/9b4244bd-963e-4675-aff7-a9e034914ff8
middleware: GET /admin/agencys/9b4244bd-963e-4675-aff7-a9e034914ff8
GET /admin/agencys/9b4244bd-963e-4675-aff7-a9e034914ff8 200 in 70ms
GET /admin/agencys/9b4244bd-963e-4675-aff7-a9e034914ff8 200 in 47ms
middleware: GET /admin/agencys/9b4244bd-963e-4675-aff7-a9e034914ff8
GET /admin/agencys/9b4244bd-963e-4675-aff7-a9e034914ff8 200 in 70ms
GET /admin/agencys/9b4244bd-963e-4675-aff7-a9e034914ff8 200 in 47ms
This is the logs
this is the middleware:
export async function middleware(request) {
console.log('middleware:', request.method, request.nextUrl.pathname);
}
export const config = {
matcher: ["/admin", "/admin/:path((?!_next|api|.\..).)", "/login", '/((?!_next|static).)'],
};
export async function middleware(request) {
console.log('middleware:', request.method, request.nextUrl.pathname);
}
export const config = {
matcher: ["/admin", "/admin/:path((?!_next|api|.\..).)", "/login", '/((?!_next|static).)'],
};
export const config = {
matcher: ['/((?!_next|static).*)'],
};
matcher: ['/((?!_next|static).*)'],
};
not admin
Transvaal lionOP
but i want to this middleware only run on admin
But stills the same
hm...
Transvaal lionOP
I make a middleware to refresh token, and get this problem because it refresh two times and i get a error
But then i simplify the middleware and saw that he runs 2 times only in dynamic routes i think
so strange
Transvaal lionOP
yes, it is
Asiatic Lion
yes is the same as me
@Transvaal lion yes, it is
can you invite me your simple project?
Transvaal lionOP
Is a long project ahaha
With authentication
np
Transvaal lionOP
Can we call?
Transvaal lionOP
Ok np
Falas portugues?
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
// Skip Next.js internals and data requests
if (
pathname.startsWith('/_next') ||
request.headers.get('x-nextjs-data')
) {
return NextResponse.next();
}
console.log('Middleware running for:', pathname);
// Your refresh logic here
// ...
return NextResponse.next();
}
export const config = {
matcher: ['/admin/:path*'],
};
import type { NextRequest } from 'next/server';
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
// Skip Next.js internals and data requests
if (
pathname.startsWith('/_next') ||
request.headers.get('x-nextjs-data')
) {
return NextResponse.next();
}
console.log('Middleware running for:', pathname);
// Your refresh logic here
// ...
return NextResponse.next();
}
export const config = {
matcher: ['/admin/:path*'],
};