Redirecting to the not found page with app router
Unanswered
Crème D’Argent posted this in #help-forum
Crème D’ArgentOP
Hi. I have this simple component:
In that same directory I have a not-found.tsx file. In order to show that file though, I need to call
export default async function Location({ params }: LocationProps) {
const cityId = params.city.toLowerCase();
const { data: city } = await getCity(cityId);
return (
<main className="container mx-auto max-w-6xl">
Render city here...
</main>
);
}In that same directory I have a not-found.tsx file. In order to show that file though, I need to call
return notFound(). But, when getCity gets a 404, it throws an exception, so I can't do if (!city) return notFound(). Is there any way to automatically make it forward to the not found page when the API returns an 404?