next/navigation notFound() in app/api/somepath/route.ts does not use app/not-found.tsx?
Answered
Chinese perch posted this in #help-forum
Chinese perchOP
Instead the default browser 404 is being displayed, is this expected behaviour? If so, how can I show my user not-found.tsx in the same way navigating to a path that does not exist would.
Answered by joulev
Yes that is the expected behaviour, because you would want to guarantee the status to be 404 (the not-found.tsx file does not guarantee 404 response). To use the default not-found.tsx, just redirect to any non-existent path
9 Replies
@Chinese perch Instead the default browser 404 is being displayed, is this expected behaviour? If so, how can I show my user not-found.tsx in the same way navigating to a path that does not exist would.
Yes that is the expected behaviour, because you would want to guarantee the status to be 404 (the not-found.tsx file does not guarantee 404 response). To use the default not-found.tsx, just redirect to any non-existent path
Answer
@joulev Yes that is the expected behaviour, because you would want to guarantee the status to be 404 (the not-found.tsx file does **not** guarantee 404 response). To use the default not-found.tsx, just redirect to any non-existent path
Chinese perchOP
Thank you for clarifying, is that really the only option? Will be evident to someone making the request that a resource exists at that location if they are redirected
@Chinese perch Thank you for clarifying, is that really the only option? Will be evident to someone making the request that a resource exists at that location if they are redirected
route.ts files are supposed to be accessed by scripts, not human users.
For human users, it’s important that they see the not found message, hence notFound() in pages will display that message – and human users are not inspecting the network to see the actual status code.
For scripts, they don’t care about the html response. They care more about the 404 status code, and/or the json/binary response payload.
That’s to explain why they do that. As for whether that is the only option, yes.
For human users, it’s important that they see the not found message, hence notFound() in pages will display that message – and human users are not inspecting the network to see the actual status code.
For scripts, they don’t care about the html response. They care more about the 404 status code, and/or the json/binary response payload.
That’s to explain why they do that. As for whether that is the only option, yes.
@joulev route.ts files are supposed to be accessed by scripts, not human users.
For human users, it’s important that they see the not found message, hence notFound() in pages will display that message – and human users are not inspecting the network to see the actual status code.
For scripts, they don’t care about the html response. They care more about the 404 status code, and/or the json/binary response payload.
That’s to explain why they do that. As for whether that is the only option, yes.
Chinese perchOP
the rationale makes sense, it is a shame however as it exposes the existence of a resource at the path
@Chinese perch the rationale makes sense, it is a shame however as it exposes the existence of a resource at the path
I don’t think the existence of the route being known is a security risk. I want to attack your site and I know you have /hello/world, what can I do? Nothing
Of course, don’t expose private data there without proper authentication
@Chinese perch the rationale makes sense, it is a shame however as it exposes the existence of a resource at the path
Actually I think you can return NextResponse.rewrite(/any/404/route) to hide it completely
Chinese perchOP
yeah you're right, it isn't a security risk (and if it was i shouldn't be using obscurity as a means to securing it), just saw it more from the perspective of completeness (it is a pretty insignificant issue just thought id ask)
@joulev Actually I think you can return NextResponse.rewrite(/any/404/route) to hide it completely
Chinese perchOP
I'll have a look thanks