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

404 when redirecting in server action with subdomain rewrite rule #65893

Open
lukemorton opened this issue May 17, 2024 · 2 comments
Open

404 when redirecting in server action with subdomain rewrite rule #65893

lukemorton opened this issue May 17, 2024 · 2 comments
Labels
bug Issue was opened via the bug report template. Navigation Related to Next.js linking (e.g., <Link>) and navigation.

Comments

@lukemorton
Copy link

lukemorton commented May 17, 2024

Link to the code that reproduces this issue

https://github.com/lukemorton/next-testing

To Reproduce

  1. Add admin.localhost to /etc/hosts
  2. Run development server
  3. Navigate to http://admin.localhost:3000/a
  4. Submit form

Current vs. Expected behavior

When you submit the form, the redirect in the server action attempts to redirect to /b but 404s instead even though the URL exists. If you refresh the page works.

In the example attached there is also a <Link /> on /a which navigates fine to /b.

Appears to be an issue with redirect specifically in server actions.

Could not reproduce without the subdomain rewrite rule in next.config.mjs.

Could not reproduce when redirecting with relative URLs — changing from redirect("/b") to redirect("./b") fixes the issue.

I have been able to reproduce on Vercel with similar subdomain rewrite rule so not just related to localhost.

Expected behaviour: no 404 occurs when redirecting in a server action when I have rewrite rules in my application.

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 22.1.0: Sun Oct  9 20:15:52 PDT 2022; root:xnu-8792.41.9~2/RELEASE_ARM64_T8112
  Available memory (MB): 24576
  Available CPU cores: 8
Binaries:
  Node: 20.13.1
  npm: 10.7.0
  Yarn: 1.22.19
  pnpm: N/A
Relevant Packages:
  next: 14.2.3 // Latest available version is detected (14.2.3).
  eslint-config-next: 14.2.3
  react: 18.3.1
  react-dom: 18.3.1
  typescript: 5.4.5
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), Vercel (Deployed)

Additional context

This issue started in 14.2.1. Appears to be fine in 14.1.3 and so we are holding back any further updates at our organisation.

@lukemorton lukemorton added the bug Issue was opened via the bug report template. label May 17, 2024
@github-actions github-actions bot added the Navigation Related to Next.js linking (e.g., <Link>) and navigation. label May 17, 2024
@lukemorton lukemorton changed the title 404 when redirecting in server action with subdomain 404 when redirecting in server action with subdomain rewrite rule May 17, 2024
@igorxmath
Copy link

I'm having the same issue with rewrites in middleware. Every time a redirect is called with server actions, Next.js shows a 404 page. However, when I reload the browser, the page loads correctly.

@lukemorton
Copy link
Author

@igorxmath Good point about refreshing. A refresh after the 404 does indeed show the right page.

Only consistent fix I’ve found is to start redirect URL with a dot “./location” instead of “/location” or fully qualified domain, e.g. “https://example.com/location”. That’s if redirecting to another location on the same domain. I have not tested cross domain redirects after a server action.

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

2 participants