Custom 404 page for different kind of entities when using dynamic route segment [[...slug]]/page.tsx
Unanswered
Transvaal lion posted this in #help-forum
Transvaal lionOP
Most of our application is using dynamic route segments since much of the content is coming from a CMS.
So in [[...slug]]/page.tsx we "route" to different server components for each entity kinds template.
Since these server components (templates) are not wired into any static route segments we can't easily show a custom 404 page for each entity kind, since a not-found.tsx inside a server component is not getting triggered if it's not hooked up as a static route segment within the app router.
So right now we have to use the error boundary, to do the following:
First we throw a
We have to go through the error boundary to get the 404 status code, so we can't return NotFound directly.
Is there any better way to handle this? Ideally I'd love it I could avoid having to throw an error, since I'm very much in favor of the result pattern. But throwing would be fine, as long as I wouldn't have to go through the error boundary like this. It feels like a hack.
So in [[...slug]]/page.tsx we "route" to different server components for each entity kinds template.
Since these server components (templates) are not wired into any static route segments we can't easily show a custom 404 page for each entity kind, since a not-found.tsx inside a server component is not getting triggered if it's not hooked up as a static route segment within the app router.
So right now we have to use the error boundary, to do the following:
First we throw a
NEXT_NOT_FOUND error using notFound()return error.message === 'NEXT_NOT_FOUND' ? (
<NotFound />
) : (
<div>some error...</div>
)
)We have to go through the error boundary to get the 404 status code, so we can't return NotFound directly.
Is there any better way to handle this? Ideally I'd love it I could avoid having to throw an error, since I'm very much in favor of the result pattern. But throwing would be fine, as long as I wouldn't have to go through the error boundary like this. It feels like a hack.