Next.js Discord

Discord Forum

How to send a 404 / not found for a dynamic page using app router?

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

1 Reply