Next.js 13 appDir not-found page should have access to params / searchParams #43179
Replies: 14 comments 17 replies
-
This should have access to params. My use case is when using internationalization. Our 404 pages have language-specific content that should be rendered. The only way around this right now is by making this a client component, then dealing with loading state, and CORS. |
Beta Was this translation helpful? Give feedback.
-
@timneutkens I wanted to revive this conversation as it isn't getting any attention. Is this feature something that will be supported? |
Beta Was this translation helpful? Give feedback.
-
This feature would be really needed in order to display context in the not found page based on route. Here's a use case we're dealing with: Sales page, route type Props = {
params: {
id?: string;
}
}
const NotFoundPage = async ({params}: Props) => {
const { id } = params;
const itemDetails = await fetch(`apiURL/items/${id}`)
// ...
} That is however not possible as // not-found.ts
const NotFoundPage = async () => {
return <ItemNotFound />
}
// ItemNotFound.tsx
"use client";
const ItemNotFound = () => {
const path = usePathname();
const itemId = useMemo(
() => path?.match(/(?<itemId>\d+)\/$/)?.groups?.itemId,
[path]
);
const [item, setItem] = useState<Item>();
useEffect(() => {
fetch(`apiURL/items/${itemId}`)
.then((response) => response.json())
.then((data) => {
setItem(data)
});
}, [itemId]);
} This is unoptimal due to multiple reasons:
Any indication that at least route params would be supported in the future? |
Beta Was this translation helpful? Give feedback.
-
One workaround could be to use
Doing this, we could access the
So calling that from This could be extended to include other stuff—search params could easily be pulled from |
Beta Was this translation helpful? Give feedback.
-
Any updated on this? I have a case where I have the route Currently I have to render a |
Beta Was this translation helpful? Give feedback.
-
I have similar requirements to what other people have been stating. It would be great to be able to bring contextual messages into the not found page by deriving them from params and props. What's great and simple about current Looking forward to seeing updates here, thanks! |
Beta Was this translation helpful? Give feedback.
-
Also looking for updates on this. With i18n handled via middleware, as currently recommended, I have everything located in But without having access to My current workaround is to add a client component to |
Beta Was this translation helpful? Give feedback.
-
I also have i18n use case for this. |
Beta Was this translation helpful? Give feedback.
-
I found an alternative solution for the ones who don't want to use the middleware solution. // [lang]/[...notFound]/page.tsx
export default async function NotFound({
params: { lang },
}: {
params: { lang: Locale };
}) {
// do some fetching logic using your params here
const {
page: { not_found },
} = await getDictionary(lang);
return (
<main>
<h1>{not_found.title}</h1>
</main>
);
} This example is specific for the internalization of the not-found.tsx page, but it should work for any similar issues. Edit: |
Beta Was this translation helpful? Give feedback.
-
Pretty underwhelming that this is not supported in stable app-router. :( |
Beta Was this translation helpful? Give feedback.
-
If you use // /src/app/[locale]/not-found.tsx
import { useLocale, useTranslations } from 'next-intl'
import Link from 'next/link'
import Header from '@/components/layout/Header'
import Footer from '@/components/layout/Footer'
export default function NotFound() {
const locale = useLocale() //en, de, ...
const t = useTranslations('NotFound')
return (
<div>
<Header locale={locale} />
<h1 className="text-lg">{t('title')}</h1>
<Link href="/">{t('cta-return-home')}</Link>
<Footer locale={locale} />
</div>
)
} |
Beta Was this translation helpful? Give feedback.
-
This is a pretty basic functionality that should be supported from the get-go. So in
And then in
So far seems to work with different paths, routes etc. |
Beta Was this translation helpful? Give feedback.
-
I ended up passing client component where I can read searchParams and display it in NonFound.
|
Beta Was this translation helpful? Give feedback.
-
One solution that I've found to be working for // context.ts
import { cache } from "react";
/**
* This is a workaround until Next.js adds support for true Server Context
* It works by using React `cache` to store the value for the lifetime of one rendering.
* Meaning it's available to all server components down the tree after it's set.
* Do not use this unless necessary.
*
* @warning This is a temporary workaround.
*/
export function createServerContext<T>(defaultValue: T): [() => T, (v: T) => void] {
const getRef = cache(() => ({ current: defaultValue }));
const getValue = (): T => getRef().current;
const setValue = (value: T) => {
getRef().current = value;
};
return [getValue, setValue];
}
export const [getNotFoundContext, setNotFoundContext] = createServerContext<string | null>(null) // layout.tsx
export default async function Layout({ params }: { params: Promise<{ param: string }> }) {
const { param } = await params;
setNotFoundContext(param);
return (
<div>{param}</div>
);
} // not-found.tsx
export default function NotFoundPage() {
const param = getNotFoundContext();
return (
<div>{param}</div>
);
} |
Beta Was this translation helpful? Give feedback.
-
Describe the feature you'd like to request
The special
not-found.js
page should have access to anyparams
,searchParams
, and/or specific context from thenotFound()
invocation fromnext/navigation
.This is important in order to give users more specific info on what was not found.
Describe the solution you'd like
Either:
params
andsearchParams
to thenot-found.js
file's propsany
prop passed fromnotFound()
innext/navigation
that thenot-found.js
file's default export can use to provide additional user informationNote: the same feature request will go for the special
error.js
file as well.Describe alternatives you've considered
I could do this by not using
not-found.js
file, but I still want to return the404
status code.Beta Was this translation helpful? Give feedback.
All reactions