How to send a 404 / not found for a dynamic page using app router?
Answered
American black bear posted this in #help-forum
American black bearOP
Hello, I've been trying to migrate to using the app router, but one hurdle I've ran into is how to replicate
Before, I would do something like:
How do I do this with app router?
getServerSideProps' notFound response. Basically, if I have a route such as /[id]/page.tsx, not every ID is going to actually exist, so I'd like to send a 404 response in that case.Before, I would do something like:
export const getServerSideProps = (context) => {
if(/* check if context.params.id doesn't exist */) {
return { notFound: true };
}
// Handle everything else...
};How do I do this with app router?
Answered by American black bear
Ahh, Next.JS Forum bot to the rescue! https://github.com/rafaelalmeidatk/nextjs-forum/blob/0142685c185f274ee2884d77c817746b5ffef35d/apps/web/app/post/%5Bid%5D/page.tsx#L156-L159
1 Reply
American black bearOP
Ahh, Next.JS Forum bot to the rescue! https://github.com/rafaelalmeidatk/nextjs-forum/blob/0142685c185f274ee2884d77c817746b5ffef35d/apps/web/app/post/%5Bid%5D/page.tsx#L156-L159
Answer