Next.js Discord

Discord Forum

dynamic routes and 404

Answered
American black bear posted this in #help-forum
Open in Discord
American black bearOP
I'm using app/dynamic routes and want to return a 404 when my data query can't find what I'm looking for. (plus another condition). How can I do this? Googling so far hasn't produced any info.
Answered by @ts-ignore
export default async function Page(){
const res = await(await fetch()).json();
if(!res.data) return notFound()
}
View full answer

10 Replies

American black bearOP
should I use a route handler?
@American black bear should I use a route handler?
if you're generating static pages then nextjs will do that automatically else just use redirect from next/redirect
American black bearOP
they're not static
so, redirect on 404? that's unexpected
I think there is a notFound()
Function iirc
export default async function Page(){
const res = await(await fetch()).json();
if(!res.data) return notFound()
}
Answer
Yeah that ^
American black bearOP
👍