#Is there a way to to redirects from
1 messages · Page 1 of 1 (latest)
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:
So in this example from the docs:
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?
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.
have you tried using redirect()?
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.
it should work though
assuming you call it inside the server action and not outside
report it as a bug
it is in alpha anyway, bugs are expected
Just to confirm it's not a me issue, do you know if this is the correct import?
import { redirect } from 'next/navigation';
Based on the docs, I am importing from the correct location
https://nextjs.org/docs/app/api-reference/functions/redirect#redirect
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