Next.js Discord

Discord Forum

how to know if the app is on the not found page in nextjs 14 app router

Unanswered
Champagne D’Argent posted this in #help-forum
Open in Discord
Champagne D’ArgentOP
I'm encountering difficulty determining if my Next.js application is on the Not Found page within the app router. I have a functioning Not Found page that displays when the URL doesn't match any routes defined in my app's router folder. However, there's a component called NewsLetter in the global Layout file, so that displays on every page, including the 404 page. I want to exclude this component from the 404 page and from the newsletter-confirmation page.

I handle the exclusion of the NewsLetter component on the /newsletter-confirmation page by checking the pathname and returning null before the component's return code if the pathname is "/newsletter-confirmation". However, I encountered an issue when trying to implement a similar logic for the 404 page. When I logged the pathname, I noticed that it returns the random pathname currently in the URL. For instance, if I type "abcd" in the pathname, I'll get the value "abcd" in the usePathname() function, making it unusable for my purposes.

I've tried searching extensively online but haven't found a solution. How can I reliably determine if my app is on the Not Found page within the app router, so I can handle the exclusion of the NewsLetter component appropriately?

0 Replies