Skip to content
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

Closed
justin-algoleaf opened this issue Nov 15, 2024 · 4 comments · Fixed by #73063
Labels
bug Issue was opened via the bug report template. locked Navigation Related to Next.js linking (e.g., <Link>) and navigation.

Comments

@justin-algoleaf
Copy link

justin-algoleaf commented Nov 15, 2024

Link to the code that reproduces this issue

https://github.com/justin-algoleaf/redirect-test

To Reproduce

  1. npm run dev
  2. Click "Redirect" button on "/" page

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

Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 11 Pro
  Available memory (MB): 16075
  Available CPU cores: 8
Binaries:
  Node: 22.11.0
  npm: 10.8.3
  Yarn: N/A
  pnpm: N/A
Relevant Packages:
  next: 15.0.3 // Latest available version is detected (15.0.3).
  eslint-config-next: N/A
  react: 19.0.0-rc-66855b96-20241106
  react-dom: 19.0.0-rc-66855b96-20241106
  typescript: N/A
Next.js Config:
  output: N/A

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!

@justin-algoleaf justin-algoleaf added the bug Issue was opened via the bug report template. label Nov 15, 2024
@github-actions github-actions bot added the Navigation Related to Next.js linking (e.g., <Link>) and navigation. label Nov 15, 2024
@kevinpruett-prime
Copy link

Experiencing the same issue. As a workaround, I'm returning a success flag from the server action and performing the redirect on the client:

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 };
}

@justin-algoleaf
Copy link
Author

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.

@justin-algoleaf
Copy link
Author

Thanks!

wyattjoh pushed a commit that referenced this issue Nov 28, 2024
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
Copy link
Contributor

github-actions bot commented Dec 6, 2024

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.

@github-actions github-actions bot added the locked label Dec 6, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. locked Navigation Related to Next.js linking (e.g., <Link>) and navigation.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants