-
Notifications
You must be signed in to change notification settings - Fork 27.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Next 15 migration: next/navigation "redirect" method does not execute from server action #72842
Comments
Experiencing the same issue. As a workaround, I'm returning a form.tsx "use client";
import { useActionState, useEffect } from "react";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { useRouter } from "next/navigation";
import { testServerAction } from "./actions";
export default function Form() {
// Nextjs
const router = useRouter();
// Server action
const [state, formAction, pending] = useActionState<
{ error: string; success: boolean },
FormData
>(testServerAction, { error: "", success: false });
// Effects
useEffect(() => {
if (state.success) {
console.log("success, trigger redirect");
router.push("/dashboard");
}
}, [router, state.success]);
return (
<form action={formAction}>
<Input type="text" name="phone_number" placeholder="Phone Number" />
<Button>{pending ? "..." : "Submit"}</Button>
</form>
);
} actions.ts export async function testServerAction(state: ActionState, payload: FormData) {
// redirect not working in server action, instead
// returning a success flag that is handled in the client component
return { error: "", success: true };
} |
Thanks for sharing. I considered the same client-side workaround but ended up returning a redirect from my express server instead. I'd still like to see the Next redirect method work properly. |
Thanks! |
When handling a redirect through a server action, we pass in the current href rather than the destination href to `handleExternalUrl`, which is incorrect since that won't reflect the updated URL from a server action's redirect response. While looking into this, I noticed a handful of server action branches that weren't properly rejecting or resolving the action promises. This would cause the transitions to stall until they were fulfilled, so I updated all of the existing branches where we early return to also resolve the promise. Fixes #72842
This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Link to the code that reproduces this issue
https://github.com/justin-algoleaf/redirect-test
To Reproduce
Current vs. Expected behavior
Current Behavior
Form submission runs server action which should execute redirect('/dashboard'). I get a 303 for the redirect, but the redirect is not performed in the browser. Server action completes and nothing happens.
Expected Behavior
Form submission runs server action which executes successful redirect to '/dashboard'
Provide environment information
Which area(s) are affected? (Select all that apply)
Navigation
Which stage(s) are affected? (Select all that apply)
next dev (local)
Additional context
This is the exact project structure and route grouping I used in all of my Next 14 builds. I have the (auth) group and page, which should redirect users via server action to the /dashboard page with successful login. After migrating to Next 15 along with next/form Form and useActionState, redirect does not properly execute. The minimal recreation repo I provided should demonstrate the issue clearly.
Thanks for your help!
The text was updated successfully, but these errors were encountered: