Next.js Discord

Discord Forum

Wrong api route does NOT return 404

Answered
European pilchard posted this in #help-forum
Open in Discord
European pilchardOP
Hi, I got a bit of a weird issue. I need my api routes to return 404 code when they dont exist, but I'm getting some weird html instead with code 200 and errors from layout from totally different directory.
I got GlobalNotFound component setup like this:
export default function GlobalNotFound() {
    return (
        <html>
            <body>
                <h1>404 - Page Not Found</h1>
                <p>This page does not exist.</p>
            </body>
        </html>
    )
}

But I'm getting a response, that's too long so its a text document with this message.

Also, for some reason it only does this, when its an existing directory inside the api, but non-existent route. So it works fine if theres something like /api/something, but returns the weird html when it something like /api/products (there is a products directory in my api, but there isn't a route.ts file).

I'm using next.js 15 app directory and the experimental global-not-found.tsx.

Any help appreciated!
Answered by European pilchard
I think, it's happening because I have /api on the same level as /[lang], so when it doesn't find an existing api route, it tries to find it inside the /[lang] and gets stuck on /[lang]/layout.tsx, because it can't find a dictionary for "api language"
View full answer

6 Replies

@European pilchard did you turn on the experimental feature in the next config?
@CARSON.ts <@553671767839735850> did you turn on the experimental feature in the next config?
European pilchardOP
Yes, as I said, it works fine, until it's a non-existent route inside an existing directory
Oh, I think I might've figured it out
European pilchardOP
I think, it's happening because I have /api on the same level as /[lang], so when it doesn't find an existing api route, it tries to find it inside the /[lang] and gets stuck on /[lang]/layout.tsx, because it can't find a dictionary for "api language"
Answer
This app routing is always confusing lol 🤣