Next.js Discord

Discord Forum

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
Open in Discord
Avatar
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.
Answered by Jboncz
try doing return redirect('url')
View full answer

22 Replies

Avatar
Can you give a snipper of your code?
Avatar
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.
Avatar
@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
Image
Avatar
Grand Anglo-Français Blanc et OrangeOP
Yup, worked locally
Avatar
Testing on my end one sec
Avatar
Grand Anglo-Français Blanc et OrangeOP
Great, thank you!
Avatar
try doing return redirect('url')
Answer
Avatar
Image
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
Avatar
Grand Anglo-Français Blanc et OrangeOP
haha
okay ill try that
Avatar
Let me know what happens
Avatar
Grand Anglo-Français Blanc et OrangeOP
great, that fixed it! thank you 🙇‍♂️
Avatar
No problem! 🙂