Replies: 3 comments 4 replies
-
To use a custom 404 page in Next.js, you can create a file named 404.tsx in the pages directory. However, in your case, you want to use the not-found.tsx file in the [lang] directory as the 404 page, which is not recognized by Next.js. To make Next.js recognize the not-found.tsx file as the 404 page, you can create a custom _error.tsx file in the pages directory, and then render the not-found.tsx component inside it when the statusCode is 404. Here's how you can do it:
In this example, we define a functional component named ErrorPage that takes the statusCode and message props. Inside the component, we check if the statusCode is 404, and if so, we render the NotFound component. Otherwise, we render a generic error message that includes the statusCode and message. We also define a getInitialProps function that gets the statusCode and message from the server response or error object, and returns them as props. With this code in place, Next.js should recognize the not-found.tsx component as the 404 page for your app. |
Beta Was this translation helpful? Give feedback.
-
Related issue I found to this: #48763 |
Beta Was this translation helpful? Give feedback.
-
It took me a while to find a solution, but within the App Router, you can use the following to make the 404 translatable:
Notice the /layout.tsx import './globals.css' // import your global css here
type Lang = 'en' | 'fr' | 'nl' // all the possible languages, better to define it in a separate file and import it here
const defaultLang: Lang = 'en' // the default language, better to define it in a separate file and import it here
export default function RootLayout({
params: {lang = defaultLang},
children
}: {
params: {lang?: Lang} // all the possible languages
children: React.ReactNode
}) {
return (
<html lang={lang}>
<body>
{children}
</body>
</html>
)
} /not-found.tsx type Lang = 'en' | 'fr' | 'nl' // all the possible languages, better to define it in a separate file and import it here
const defaultLang: Lang = 'en' // the default language, better to define it in a separate file and import it here
export default async function NotFound({params}: {params?: {lang?: Lang}}) {
const currentLang: Lang = params?.lang || defaultLang
return <div>
{currentLang === 'en' && "We have an error"}
{currentLang === 'fr' && "Nous avons une erreur"}
{currentLang === 'nl' && "Er is een fout opgetreden"}
</div>
} /app/[lang]/layout.tsx type Lang = 'en' | 'fr' | 'nl' // all the possible languages, better to define it in a separate file and import it here
const defaultLang: Lang = 'en' // the default language, better to define it in a separate file and import it here
export default async function RootLayout({
params: {lang= defaultLang},
children
}: {
params: {lang?: Lang}
children: React.ReactNode
}) {
return (
<>
<header><nav>
{`My nav, depending on the lang param: ${lang}`}
</nav></header>
<main>This [lang]/layout.tsx will be loaded inside the root layout.tsx, which will wrap the html and body tags around it.</main>
<footer>
{`My footer, depending on the lang param: ${lang}`}
</footer>
</>
)
} /app/[lang]/[...not-found]/page.tsx import {default as DefaultNotFound} from '@/app/not-found'
type Lang = 'en' | 'fr' | 'nl' // all the possible languages, better to define it in a separate file and import it here
export default async function NotFound({
params: {lang}
}: {
params: {lang: Lang, , 'not-found': string[]}
searchParams?: {[key: string]: string | string[]}
}) {
return await DefaultNotFound({params: {lang}})
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
My Environment:
I have the following folder structure:
and I want that my not-found.tsx is used as 404 page. This not-found.tsx page as well as the layout with header and footer is depending on the lang param therefore I cannot put them in the root of the app folder. But NextJS is not recognizing this nested not-found.tsx page.
Does anybody has an idea how I can use my not-found page as 404 page?
Additional information
No response
Example
No response
Beta Was this translation helpful? Give feedback.
All reactions