next/navigation router with query
Unanswered
Polar bear posted this in #help-forum
Polar bearOP
Hello, I've tried searching but I can't find how to use next/navigation router to push to another route by taking a query as a parameter. I've tried putting it directly in the url but it doesn't work. I can't use next/router and I get an error: Error: NextRouter was not mounted. https://nextjs.org/docs/messages/next-router-not-mounted
26 Replies
@Polar bear Hello, I've tried searching but I can't find how to use next/navigation router to push to another route by taking a query as a parameter. I've tried putting it directly in the url but it doesn't work. I can't use next/router and I get an error: Error: NextRouter was not mounted. https://nextjs.org/docs/messages/next-router-not-mounted
the code in the question should work if you are using the right router hook
@Ray the code in the question should work if you are using the right router hook
Polar bearOP
Hi, i'm using {useRouter} from next/navigation and when the function is called i'm move to /password/new-password but there isn't the ?token="" in url pathname
@Polar bear Hi, i'm using {useRouter} from next/navigation and when the function is called i'm move to /password/new-password but there isn't the ?token="" in url pathname
could you try this on other page
<button onClick={() => {
router.push("/password/new-password?token=123")
}}>
test
</button>@Ray could you try this on other page
ts
<button onClick={() => {
router.push("/password/new-password?token=123")
}}>
test
</button>
Polar bearOP
it doesn't work i'm move to /password/new-password :/
@Polar bear it doesn't work i'm move to /password/new-password :/
could you show a screenshot of the folder structure
Polar bearOP
It's clean archi, in new-passeword and reset-passeword sections i have my logics and i use presenter in components
@Polar bear it doesn't work i'm move to /password/new-password :/
ok could you try
router.push("/?token=123")@Ray ok could you try `router.push("/?token=123")`
Polar bearOP
moved to localhost:3000/fr with nothing else
@Polar bear moved to localhost:3000/fr with nothing else
you have middleware?
or rewrite/redirect in next.config?
@Ray you have middleware?
Polar bearOP
i have middleware for i18n, nothing in next.config
@Polar bear i have middleware for i18n, nothing in next.config
could you rename the middleware file to _middleware.ts and try again?
@Ray could you rename the middleware file to _middleware.ts and try again?
Polar bearOP
i get the query now, but i have 404 error because i dont have the lng in front of my url
@Polar bear i get the query now, but i have 404 error because i dont have the lng in front of my url
which mean the middleware is removing the query
Polar bearOP
yes, something to look into
maybe this ?
const refererUrl = new URL(req.headers.get('referer") , req.url)maybe not lol
Polar bearOP
nope indeed
I'll talk to my dev lead about it tomorrow
@Polar bear nope indeed
try return null instead of return
NextResponse.next()Polar bearOP
i will try
at the beginging and the last
Polar bearOP
@Polar bear Click to see attachment
keep this, because it need to set the cookie
return null when there is nothing to do