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

Unexpected behaviour in redirect next/navigation function - Next.js 15 #74258

Open
leox182 opened this issue Dec 23, 2024 · 0 comments
Open

Unexpected behaviour in redirect next/navigation function - Next.js 15 #74258

leox182 opened this issue Dec 23, 2024 · 0 comments
Labels
bug Issue was opened via the bug report template. Navigation Related to Next.js linking (e.g., <Link>) and navigation.

Comments

@leox182
Copy link

leox182 commented Dec 23, 2024

Link to the code that reproduces this issue

https://github.com/leox182/next-unexpected-redirect-behavior

To Reproduce

the repository has two projects, next-14 and next-15 that contain the same code with only difference between versions (14.2.21 and 15.1.2 respectively).

  1. Install dependencies and start both projects in development mode.
  2. Click on the ‘Say Hello Action!’ button in each and check the differences in behaviour when redirecting.

Current vs. Expected behavior

Expected Behavior
Following the steps to reproduce, I expect the server action to execute its logic and then redirect the user without throwing an error (like on 14 version).

Actual Behavior
Instead of only redirecting, the server action throws an error, causing unexpected client-side behavior such as error messages.

Provide environment information

Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 11 Pro for Workstations
  Available memory (MB): 32671
  Available CPU cores: 12
Binaries:
  Node: 22.12.0
  npm: 10.5.0
  Yarn: N/A
  pnpm: 9.15.1
Relevant Packages:
  next: 15.1.2 // Latest available version is detected (15.1.2).
  eslint-config-next: 15.1.2
  react: 19.0.0
  react-dom: 19.0.0
  typescript: 5.7.2
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

After upgrading to Next.js 15, I noticed a change in the behavior of the redirect function from next/navigation when used in a server action.

In Next.js 14, calling redirect within a server action executes the action and then performs the redirect as expected. However, in Next.js 15, calling redirect not only redirects, but also causes the server action to throw an error that is cached on the client. This causes unexpected behaviour on the client, such as displaying an error message, even though no error has occurred.

While this can be handled on the client by checking if error.message === "NEXT_REDIRECT" and ignoring such errors, this change in behavior was not documented as a breaking change or improvement on Next.js release post. Hence, I believe it is a bug.

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

No branches or pull requests

1 participant