how to know if the app is on the not found page in nextjs 14 app router #61823
-
SummaryI'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? Additional informationNo response ExampleNo response |
Beta Was this translation helpful? Give feedback.
Replies: 13 comments 21 replies
-
I don't know if I understand your problem very well. create your file in this path like this the page.jsx import Link from 'next/link'
export default function NotFound() {
return (
<div>
<h2>Not Found</h2>
<p>Could not find requested resource</p>
<Link href="/">Return Home</Link>
</div>
)
} then, if I put abcd in the route If that's not what you need, please. Please provide more details about your problem so I can try to help you. |
Beta Was this translation helpful? Give feedback.
-
@abdulvahidkp you can use a route group to escape the layout, a catch all route, with a page calling All of which is necessary to properly respond with 404 and exclude the 404's from the main layout. In the ![]() |
Beta Was this translation helpful? Give feedback.
-
Hi @abdulvahidkp, there's a solution to your issue, using route groups. Move your all of your pages to a directory named Keep everything you already have in your root
Create
Now you have two layouts wrapping the whole app but NextJS will only render the parent layout ( |
Beta Was this translation helpful? Give feedback.
-
Hi @rimoslav, Now, I want to hide that Sidebar in The problem starts here: now consider I move to any route that does not exist then the sidebar not hide like below: The main thing is, I wanted to keep my Sidebar hidden for some specific routes either dynamic or static but in some case that doesn't work as I have shown. I think same as for other dynamic routes that not working that is what @abdulvahidkp wanted to mention. |
Beta Was this translation helpful? Give feedback.
-
hey @abdulvahidkp, It's been a while since you've opened this discussion. Did you find a way to solve this issue? or any workaround? I'm facing a similar situation with a breadcrum component in my general layout. I want to hide it if I'm not in a specific page or when Nextjs identifies that it must shows the fallback page (not-found page) |
Beta Was this translation helpful? Give feedback.
-
Similar issue for me. Need to detect if i'm on not found or server error page to hide some stuff in my footer conditionally. |
Beta Was this translation helpful? Give feedback.
-
I found a kinda stupid way to handle this but I'm not content with it. On my not-found.tsx page I added notFound to the search params. I was getting some infinite reloads with router.push so maybe need it in a useEffect and guard like I have: `'use client' import type { Metadata } from 'next' export const metadata: Metadata = { const createQueryString = (name: string, value: string) => { export default function NotFound() { if (!searchParams.has('notFound')) { return ( 404 - Not FoundSorry, the page you are looking for can not be found Return Home ) }` Then I just used that conditionally render something in my footer doing something like this:
Hoping somebody can find a better way. Seems like there should be some global state to access if we are on 404 or 500 page. I feel like I rembmer there was a way with previous next version. |
Beta Was this translation helpful? Give feedback.
-
I'm also facing the same issue now, looking for a better approach. It would be great if somebody suggests a good fix for this issue. I have for you below my sample code of the root layout file it has a common header and side menu components that should load every page except 404 because the 404 page doesn't need to load the header and side menu according to my design. app/layout.tsx
|
Beta Was this translation helpful? Give feedback.
-
I attempted to resolve the issue using a tricky hack. I styled the parent div of the 404 page with "position: fixed" and a background color fill to hide common components behind the 404 page, such as the header and sidebar. In my situation, the 404 page design is a full-page design that only displays related content for when a page is not found. Below is a sample of the code:
|
Beta Was this translation helpful? Give feedback.
-
Here is a solution that i found and works properly: I have this:import { redirect } from "next/navigation"; export default function NotFound() { So now in your main layout component you can check if you are on 404 route and only show children if(route === 404){ I hope i can help someone! |
Beta Was this translation helpful? Give feedback.
-
I am trying for months to find a good fix for this. The main problem for me is this [...not-found]/page.js, SEO wise all the invalid routes return a 200 because the not-found catch all route is a valid route, this causes Soft 404 errors on google search console. Removing the catch all route will result in only redirecting to not-found.js when calling notFound() which won't cover all the cases so it will default to the black next js 404 page. Did someone find a solution to use this workaround and also change the status code to 404 ? import { notFound } from 'next/navigation'; export const metadata = { export default function NotFoundDummy() { |
Beta Was this translation helpful? Give feedback.
-
My workaround for the issue:
const [is404, setIs404] = useState(false);
useEffect(() => {
setIs404(true);
return () => setIs404(false);
}, []);
|
Beta Was this translation helpful? Give feedback.
-
This feels like something the framework should handle and provide tbh. like someone commented above I am using useSelectedLayoutSegments, but it doesn't actually help with 404 / not-found page. pathname, segments etc... none of these include this information which is fairly important for many scenarios I would imagine. Is there any chance of getting this added to the framework? |
Beta Was this translation helpful? Give feedback.
My workaround for the issue:
AppContext.js
is404
state insidenot-found.tsx
is404
from app global state/context wherever you need it.