I'm trying to check if the user has safe data, but if they don't, they will be redirected to /waitlist. #67919
Replies: 1 comment
-
Hi, First a minor thing, but the Route Handlers, such as GET, POST, etc, do not receive a second response argument. Please change it to this: import { type NextRequest, NextResponse } from 'next/server';
export async function POST(req: NextRequest) { Now onto the actual issue. This one can catch people of all levels of experience, off guard.
That is to say that the document doesn't navigate, because the document didn't start the request, rather, it was the JavaScript fetch context, that did it. Of course, one could return a 200 in step 2, with a nextURL, and redirect users there, but that's also not very ideal... because now people who, outside of the browser context, reach for your POST endpoint, won't be redirect automatically to the waitlist. One way to do this with a Alternatively, a React Server Action, can also get you a somewhat similar experience. I've coded some, bad, but functional examples here: https://stackblitz.com/edit/nextjs-eusbca?file=app%2Fpage.tsx Maybe this post is also helpful to read: https://blog.jim-nielsen.com/2021/fetch-and-3xx-redirect-status-codes/ |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
`Why it is not working?
`export async function POST(req: NextRequest, res: NextResponse) {
try {
const json = await req.json();
const body = userJoinSchema.parse(json);
} catch (error) {
return NextResponse.json({ error: error }, { status: 500 });
}
}``
Beta Was this translation helpful? Give feedback.
All reactions