#redirectTo doesnt fire when used in server action

1 messages · Page 1 of 1 (latest)

loud ivy
#

redirectTo doesnt fire when used in server action

gleaming oasisBOT
#

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:

  1. 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.

  2. 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 call redirect() outside of the try-catch block.

  3. 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:

  1. Make sure you're calling redirect() outside of any try-catch blocks.
  2. Verify that you're using the latest version of Next.js, as some issues have been fixed in newer versions.
  3. If you're redirecting to the same domain, be aware that there might still be issues with the current implementation.
#

If you're still experiencing problems after following these guidelines, it might be worth creating a minimal reproduction of your issue and reporting it on the Next.js GitHub repository.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: