#NextResponse.redirect() hangs

2 messages · Page 1 of 1 (latest)

nocturne shuttle
#

I've created an MVP on CodesSandbox to demonstrate: https://codesandbox.io/p/sandbox/priceless-violet-xsjjwd?file=%2Fapp%2Fformhandler%2Froute.ts

When submitting the form, /formhandler/route.ts handles the POST action but does not successfully redirect to /redirectedpage. Going to the route directly renders just fine.

In case it's a clue— in the actual non-MVP, redirecting to a non-existing page works (while giving a 404 at that route) whereas redirecting to the intended+existing page gets stuck. "stuck" means nothing in UI changes, the address bar URL doesn't change, but the browser's loading spinner spins indefinitely. There are no errors in browser or server consoles.

What am I missing? FYI I am using Next 13.4 with app directory and app router. Also, returning a NextResponse.json() instead has no issue.

Edit: Okay I've figured out that the NextResponse.redirect() preserves the request method, so it is sending a POST to /redirectedpage instead of a GET request. Changing it to the request() from 'next/navigation' does the same. I guess my question now is how would you do this? (and not by using JS + onSubmit + useRouter given the HTML form POST action, obviously):

HTML form POST action -> API POST route handler -> re-direct clientside route to new route

CodeSandbox is an online editor tailored for web applications.

warm flintBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

      🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in <id:customize>

      ✅ You can mark a message as the answer for your post with `Right click -> Apps -> Mark Solution`
      (if you don't see the option, try refreshing Discord with Ctrl + R)