#Is there a way to to redirects from

1 messages · Page 1 of 1 (latest)

mossy cliff
plain swiftBOT
#

Hi @mossy cliff. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

mossy cliff
#

So in this example from the docs:

plain swiftBOT
#

Sorry @mossy cliff, I seem to be having a bit of trouble. Don't worry though, I've pinged my supprt and they will try to fix me up! :technologist:

Perhaps try again in 15mins?

mossy cliff
#
import { cookies } from 'next/headers';
 
export default function AddToCart({ productId }) {
  async function addItem(data) {
    'use server';
 
    const cartId = cookies().get('cartId')?.value;
    await saveToDb({ cartId, data });
  }
 
  return (
    <form action={addItem}>
      <button type="submit">Add to Cart</button>
    </form>
  );

I'd like to redirect the user once something is saved to the database.

raven anvil
#

have you tried using redirect()?

mossy cliff
#

Yea, I'm getting this error:

Unhandled Runtime Error
Error: NEXT_REDIRECT
#

Looking at the call stack, it looks like it's using use hooks

#

Which makes me think it's not meant to be called serverside.

raven anvil
#

it should work though AnisThink assuming you call it inside the server action and not outside

#

report it as a bug

#

it is in alpha anyway, bugs are expected

mossy cliff
#

Just to confirm it's not a me issue, do you know if this is the correct import?

import { redirect } from 'next/navigation';
#

It says:

Invoking the redirect() function throws a NEXT_REDIRECT error and terminates rendering

#

Which is the error that I'm getting

#

It seems like the client is not correctly catching and handling the redirect

#

Though, in the example, it's being used on the client