Next.js Discord

Discord Forum

NextResponse.redirect() hangs

Unanswered
American black bear posted this in #help-forum
Open in Discord
American black bearOP
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

0 Replies