I have a route at /:username.
In my _middleware.ts file I would like do do something like this
export function middleware(req: NextRequest) {
const isDynamicRoute = req.nextUrl.pathname === "/[username]";
if (isDynamicRoute) {
return undefined
}
// handle other routes...
}
The issue is that I can't seem to access this dynamic route name in the middleware. I see /test as the pathname instead of /[username].
When I log the request I see what I'm looking for, but it is inside a [Symbol(internal request)] key (along with a separate symbol key that includes the real pathname). Is it possible to access this value reliably in the middleware file? Or do I need to handle this logic in _app.ts? (see log below)
[Symbol(internal request)]: {
credentials: 'same-origin',
headers: BaseHeaders [Headers] { [Symbol(map)]: [Object] },
method: 'GET',
referrer: 'about:client',
redirect: 'follow',
url: NextURL {
href: '/test',
origin: '',
protocol: '',
username: '',
password: '',
host: '',
hostname: '',
port: '',
pathname: '/test',
search: '',
searchParams: URLSearchParams {},
hash: ''
}
},
[Symbol(internal request)]: {
cookieParser: [Function: cookieParser],
geo: {},
ip: undefined,
page: { name: '/[username]', params: [Object] },
url: NextURL {
href: '/test',
origin: '',
protocol: '',
username: '',
password: '',
host: '',
hostname: '',
port: '',
pathname: '/test',
search: '',
searchParams: URLSearchParams {},
hash: ''
}
}