nextjs routing me to not-found on 303 from server action redirect()
Answered
Grand Anglo-Français Blanc et Or… posted this in #help-forum
Grand Anglo-Français Blanc et OrangeOP
As far as I can tell when I use a server action and call redirect() it's returning a 303, which is correct according to documentation, but its taking users to the not-found.tsx page, which seems incorrect. And this is only in production. Any ideas? nextjs version 14.2.3.
22 Replies
Can you give a snipper of your code?
Grand Anglo-Français Blanc et OrangeOP
Yes! Here you go
/* eslint-disable import/prefer-default-export */
'use server';
import { revalidatePath } from 'next/cache';
import { redirect } from 'next/navigation';
import { createDispatcherClient } from 'src/services/dispatcher';
import getServerSession from 'src/utility/getServerSession';
export async function deleteMemories(uuids: string[]): Promise<void> {
const session = await getServerSession();
const dispatcher = createDispatcherClient({ token: session?.accessToken! });
await dispatcher.bulkDeleteMemories(uuids);
revalidatePath('/captures', 'page');
redirect('/captures');
}
and then calling it
const handleOnClick = async () => {
setLoading(true);
await deleteMemories(uuids);
setLoading(false);
};
that's in a client component
im refactoring it to remove the server action anyway, since i don't want to revalidate that path for all users, but im still wondering if there is a bug there or i am using server actions incorrectly.
@Grand Anglo-Français Blanc et Orange and you said it works properly in dev but not build?
Looks like your doing it properly. Ill have to set this up in my environment and see what happens
Grand Anglo-Français Blanc et OrangeOP
Yup, worked locally
Testing on my end one sec
Grand Anglo-Français Blanc et OrangeOP
Great, thank you!
try doing return redirect('url')
Answer
in dev I didnt have to return, in build I did
and now its working in build without a return... really strange. I was getting an error
Oh heh, I didnt save
Grand Anglo-Français Blanc et OrangeOP
haha
okay ill try that
Let me know what happens
Grand Anglo-Français Blanc et OrangeOP
great, that fixed it! thank you 🙇♂️
No problem! 🙂