#Error when attempting to redirect in server action.

59 messages · Page 1 of 1 (latest)

sand bison
#

let's see the code

bleak mountain
#

before you do anything

#

update to .12

#

debugging issues that may have been fixed is a waste of everyones time 🙂

#

(they are on .10)

#

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

sand bison
#

i get the same error

bleak mountain
sand bison
#

what i don't understand is, why do the docs say this:

#

that is what we're seeing

bleak mountain
sand bison
#

trying something else. i'm close

#
  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

bleak mountain
#

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

sand bison
#

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

#

@lusty yacht i don't have anything else to share, good luck with this

bleak mountain
#

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

sand bison
#

👍

bleak mountain
#

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.

simple torrent
#

hey, is it solved?

bleak mountain
#

yes.

simple torrent
bleak mountain
#

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

simple torrent
#

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.

bleak mountain
#

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

simple torrent
#

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.

bleak mountain
#

i do agree more documentation cant hurt

random stone
#

@bleak mountain 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');
}
bleak mountain
random stone
#

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

bleak mountain
random stone
bleak mountain
random stone
bleak mountain
#

yep

random stone
#

@bleak mountain I Finally resolved the problem using supabase's refreshSession