Next.js Discord

Discord Forum

Error when attempting to redirect in server action.

Unanswered
Bengal posted this in #help-forum
Open in Discord
Original message was deleted.

60 Replies

European sprat
let's see the code
before you do anything
update to .12
debugging issues that may have been fixed is a waste of everyones time 🙂
(they are on .10)
Original message was deleted
you cant replace in server actions
replace is a window function
sure. Gotta go off the code provided
the issue isnt with the server action or the redirect according to the error
its the reducer
are you using redux or zustand in that component
thats not a component
thats a server action
can you show the component calling the server action
European sprat
i get the same error
@European sprat i get the same error
this equally as unhelpful as the missing code from the op. post your repro code
European sprat
what i don't understand is, why do the docs say this:
that is what we're seeing
@European sprat that is what we're seeing
show. your. work. man.
European sprat
trying something else. i'm close
European sprat
  const handleClick = () => {
    startTransition(() => SubmitBattleResultAction([]))
  }


this seems to work but there's a type error I'm not sure how to fix:

Type 'Promise<void>' is not assignable to type 'VoidOrUndefinedOnly'.ts(2322)
index.d.ts(1085, 38): The expected type comes from the return type of this signature.
(alias) SubmitBattleResultAction(roundResults: any[]): Promise<void>
import SubmitBattleResultAction


and there's also a brief moment of no content for the route before it shows loading
start transition is a totally different setup
why is no one showing the damn component
the server action is only relevant in the context of the component it is called from
European sprat
i don't have anything to share, it's a client component with a button and the handeClick function I shared. using useTransition was the only way i could get things to work
@Bengal i don't have anything else to share, good luck with this
https://github.com/vercel/next.js/issues/50757

i'm so confused about the expected behaviour
if you want redirect to work in a server action, it needs to be a server component
if its a client component, you should be calling router.push after
European sprat
👍
Original message was deleted
because its meant to be called as a form action handler when using redirect
when you introduce client logic, you need to introduce client routing.
hey, is it solved?
yes.
if you use your action in a server form action it can use redirect. if you use it in a client component you need to use client routing
Yes, but the issue was why ‘redirect’ was not working in server actions.

And I think the hack is we need to use the action with startTransition func.
you shouldnt be using redirect in a server action that is called from a client component
its mixing logic
if start transition hides that failure id say thats even worse because it will mask the issue even further
I got it. But I am saying that redirect should work in server action as per the nextjs docs. So, I just checked it out why it was not working. That's it.
@Moinul Moin I got it. But I am saying that redirect should work in server action as per the nextjs docs. So, I just checked it out why it was not working. That's it.
it does work in server actions. that are used as server actions for a server form in a server component
i do agree more documentation cant hurt
@DirtyCajunRice | AppDir My situation is also pretty weird.
I have a server action that does an async operation, and then I want to redirect the user so I use redirect('/dashboard')

For some reason the URL above changes but the page content does not
Here's my server action
export async function resetPasswordAction(
  prevState: any,
  formData: FormData,
): Promise<{ message: string | null }> {
  const client = getSupabaseServerActionClient();

  const passwordValidation = z
    .string()
    .trim()
    .nonempty()
    .min(6, 'Password must be at least 6 characters');
  const result = passwordValidation.safeParse(formData.get('password'));
  if (!result.success) {
    const message = result.error.issues[0].message;
    return { message };
  }

  const { data, error } = await client.auth.updateUser({
    password: result.data,
    data: { activated: true },
  });

  console.log(data);

  if (error) {
    return { message: 'Failed to reset password ' + error.message };
  }

  // await client.auth.signOut();
  // revalidatePath('/auth/activate');
  redirect('/dashboard');
}
@DirtyCajunRice | AppDir This is replicated in prod? or only dev
I didn't test it in prod yet but yeah this is a problem in dev
I converted my page from server to client component
Coz i thoguht maybe revalidatePath and server components might be the reason for it
but, it's the same case

The difference is that, while I had a server component and revalidatePath, the URL changed, and after sometime I got redirected. In this case however, I am still on the same page and the URL has changed
@DirtyCajunRice | AppDir always test prod.
What do you think will be a better approach?
Using the server component or the client one?
@Usman What do you think will be a better approach? Using the server component or the client one?
doesnt matter for this. If you need client features use client, otherwise server.
@DirtyCajunRice | AppDir doesnt matter for this. If you need client features use client, otherwise server.
hmm okay
I'll build the app and test it rather than deploying
That should work right?
yep
@DirtyCajunRice | AppDir I Finally resolved the problem using supabase's refreshSession
I used getSession with supabase and that had the old user metadata which caused the page to not redirect properly