#router.push dosnt wanna redirect
1 messages · Page 1 of 1 (latest)
Based on the information provided in the knowledge sources, it seems like you're experiencing an issue where router.push() is not redirecting as expected. This could be due to action queuing, where the action might return a revalidate payload so the client needs to wait. This behavior is expected in Next.js.
As a solution, it is recommended to use redirect instead of router.push. This will also save another round trip to the server. Here's an example of how to use redirect:
import { redirect } from 'next/navigation'
export async function GET(request: Request) {
redirect('<https://nextjs.org/'>)
}
This code is an example of how to use the redirect function in a server action. In your case, you would replace '<https://nextjs.org/'> with the URL you want to redirect to, which in your case would be /posts/${params.id}.
You can find more information about the redirect function in the Next.js documentation (https://nextjs.org/docs/app/api-reference/functions/redirect).
Please note that this is a suggestion based on the information provided in the knowledge sources and may not solve your issue completely. If the problem persists, it would be helpful to provide more context or consider opening an issue in the Next.js GitHub repository.
Sources:
- Github Issue (closed): next-navigation(useRouter().push("/url")) does not redirect to /url if server actions is used immediately after useRouter().push("/url") > Comments (https://github.com/vercel/next.js/issues/65287)
- Next.js > Docs > App > Building-your-application > Routing > Route Handlers > Examples > Redirects (https://nextjs.org/docs/app/building-your-application/routing/route-handlers#redirects)
- Next.js > Docs > App > Api-reference > Functions > redirect > Next Steps (https://nextjs.org/docs/app/api-reference/functions/redirect#next-steps)