dynamic routes and 404
Answered
American black bear posted this in #help-forum
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()
}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/redirectAmerican 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
ðŸ‘
[
notFound docs incase you need it](https://nextjs.org/docs/app/api-reference/functions/not-found)