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

[Routing] Using redirect from server action always uses "replace" redirect type #53911

Closed
1 task done
joshfullmer opened this issue Aug 11, 2023 · 4 comments · Fixed by #54458
Closed
1 task done

[Routing] Using redirect from server action always uses "replace" redirect type #53911

joshfullmer opened this issue Aug 11, 2023 · 4 comments · Fixed by #54458
Assignees
Labels
bug Issue was opened via the bug report template. linear: next Confirmed issue that is tracked by the Next.js team. locked Navigation Related to Next.js linking (e.g., <Link>) and navigation.

Comments

@joshfullmer
Copy link

joshfullmer commented Aug 11, 2023

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
      Platform: darwin
      Arch: arm64
      Version: Darwin Kernel Version 21.5.0: Tue Apr 26 21:08:37 PDT 2022; root:xnu-8020.121.3~4/RELEASE_ARM64_T6000
    Binaries:
      Node: 16.15.1
      npm: 8.11.0
      Yarn: 1.22.19
      pnpm: 8.6.11
    Relevant Packages:
      next: 13.4.14-canary.2
      eslint-config-next: 13.4.13
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 5.1.6
    Next.js Config:
      output: N/A

Which area(s) of Next.js are affected? (leave empty if unsure)

Routing (next/router, next/navigation, next/link)

Link to the code that reproduces this issue or a replay of the bug

https://github.com/joshfullmer/redirect-test

To Reproduce

Setup:

  • Have two pages (A and B)
  • Have server action that redirects to Page B, including the RedirectType.push option.
  • Page A implements a form that uses the server action with a button to submit the form

Steps:

  • Open new tab/window with clear browser history
  • Visit Page A
  • Click button in form

Expected:

  • Page A should be in the browser history
  • Clicking the browser's back button navigates from Page B to Page A

Actual:

  • Browser history is replaced, only Page B exists in the browser history
  • Clicking the browser's back button does nothing

Describe the Bug

When using the redirect function from next/navigation in a server function, the redirect type is always "replace".

The docs say that "push" is the default value for redirect type in the redirect function when used in server actions, but that does not appear to happen. The current route always appears to be replaced, even when the redirect type is explicitly RedirectType.push.

Expected Behavior

When redirect is used within a server action, the default value for redirect type of "push" should be respected, and browser history should include the previous page.

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

NEXT-1551

@joshfullmer joshfullmer added the bug Issue was opened via the bug report template. label Aug 11, 2023
@github-actions github-actions bot added the Navigation Related to Next.js linking (e.g., <Link>) and navigation. label Aug 11, 2023
@og2t
Copy link

og2t commented Aug 16, 2023

I can confirm this bug.

@joshfullmer Could you point to the code where:

the redirect type is always "replace"?

@fullyherge
Copy link

fullyherge commented Aug 16, 2023

Getting this behaviour as well. Worked around it for now by having my server action, which is called from within an onSubmit on a <form>, return the uri to redirect to and doing it in the client.

This will probably not cover all cases though.

Server Action, heavily redacted

"use server";

export async function runSearch(...) {
  // [...]
  if (result) {
    // redirect(`/device/cubic/${result.id}`);
    return {
      redirect: true,
      redirectUri: `/device/cubic/${result.id}`,
      result: [],
      error: null,
    };
  }
  // [...]
}

Form submit

"use client";

<form
  onSubmit={(e) => {
    e.preventDefault();
    setError(null);
    startTransition(async () => {
      try {
        const result = await runSearch(query, activeCategory);
        if (result?.redirect && result.redirectUri) {
          return router.push(result.redirectUri);
        }
        if (result?.result) {
          setResults(result?.result);
        }
      } catch (error) {
        setError(error as { status: number; message: string });
      }
    });
  }}
>

@shuding shuding self-assigned this Aug 22, 2023
@shuding shuding added the linear: next Confirmed issue that is tracked by the Next.js team. label Aug 22, 2023
@kodiakhq kodiakhq bot closed this as completed in #54458 Aug 23, 2023
kodiakhq bot pushed a commit that referenced this issue Aug 23, 2023
Closes #53911. When calling `redirect()` instead a Server Action, the previous route should exist in the history when it's handled by the framework.
@karlhorky
Copy link
Contributor

karlhorky commented Aug 24, 2023

Thanks for the super fast PR #54458 and review @shuding @ijjk 🙌

This change was released in next@13.4.20-canary.4. I upgraded to this version and can confirm that a new history state is added in my application when calling redirect() in a Server Action - the back button now returns to the previous page where the Server Action was called.

@github-actions
Copy link
Contributor

github-actions bot commented Sep 7, 2023

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 Sep 7, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 7, 2023
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. linear: next Confirmed issue that is tracked by the Next.js team. 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.

5 participants