#redirectTo doesnt fire when used in server action
1 messages · Page 1 of 1 (latest)
Hi @loud ivy. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
Based on the information provided in the knowledge sources, there are a few issues related to redirects in server actions in Next.js that may be relevant to your question:
-
There is a known issue where
redirect()inside server actions doesn't work as expected with the same domain. This was reported in GitHub Issue #62208 (https://github.com/vercel/next.js/issues/62208). The redirect does a "soft" redirect and doesn't actually refresh the router, which can lead to unexpected behavior. -
Another issue reported in GitHub Issue #66669 (https://github.com/vercel/next.js/issues/66669) shows that when
redirect()is used inside a try-catch block in a server action, it doesn't work as expected. The recommended solution is to callredirect()outside of the try-catch block. -
The official Next.js documentation on Server Actions and Mutations (https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions-and-mutations#redirecting) provides an example of how to use
redirect()in a server action:
'use server'
import { redirect } from 'next/navigation'
import { revalidateTag } from 'next/cache'
export async function createPost(id: string) {
try {
// ...
} catch (error) {
// ...
}
revalidateTag('posts') // Update cached posts
redirect(`/post/${id}`) // Navigate to the new post page
}
To ensure that your redirect() works correctly in a server action:
- Make sure you're calling
redirect()outside of any try-catch blocks. - Verify that you're using the latest version of Next.js, as some issues have been fixed in newer versions.
- If you're redirecting to the same domain, be aware that there might still be issues with the current implementation.