Lazy 404: createNotFoundPage({ load }) + renderNotFoundView, NotFoundLoader
Next puts a layout's not-found tree into the RSC payload of every page under it. The eager
createNotFoundPage({ blocks }) rendered the panel's 404 page there in every locale, so with
404 = homepage each page shipped the homepage twice (homepage HTML 195 -> 146 KB gzip).
With { load } only <NotFoundLoader> is in the payload; the content comes from the project's
server action (renderNotFoundView, locale checked against configured locales) when a 404 shows.
The eager variant is unchanged. Docs: pages.md, i18n.md, AGENT.md, PROGRAMISTA, ipal-setup, CHANGELOG.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
8b35986635
commit
166bf51e53
@@ -5,6 +5,8 @@ import type { ReactNode } from 'react'
|
||||
// Bundlers in the project resolve the same file either way.
|
||||
import { usePathname } from 'next/navigation.js'
|
||||
|
||||
import { localeFromPath } from './localeFromPath.js'
|
||||
|
||||
type LocaleViewProps = {
|
||||
/** Used when the URL has no locale segment (single-locale site) or an unknown one. */
|
||||
defaultLocale: string
|
||||
@@ -25,8 +27,6 @@ type LocaleViewProps = {
|
||||
* it also runs during SSR, so the HTML already contains the right language.
|
||||
*/
|
||||
export function LocaleView({ defaultLocale, views }: LocaleViewProps) {
|
||||
const pathname = usePathname() ?? ''
|
||||
const segment = pathname.split('/').filter(Boolean)[0] ?? ''
|
||||
const locale = segment in views ? segment : defaultLocale
|
||||
const locale = localeFromPath(usePathname() ?? '', Object.keys(views), defaultLocale)
|
||||
return <>{views[locale] ?? views[defaultLocale] ?? null}</>
|
||||
}
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
'use client'
|
||||
import type { ReactNode } from 'react'
|
||||
|
||||
// '.js' is required: `next` has no package.json "exports" (see LocaleView.tsx).
|
||||
import { usePathname } from 'next/navigation.js'
|
||||
import { useEffect, useState } from 'react'
|
||||
|
||||
import { localeFromPath } from './localeFromPath.js'
|
||||
|
||||
type NotFoundLoaderProps = {
|
||||
defaultLocale: string
|
||||
/** The project's server action: `(locale) => renderNotFoundView(content, options, locale)`. */
|
||||
load: (locale: string) => Promise<ReactNode>
|
||||
locales: string[]
|
||||
}
|
||||
|
||||
/**
|
||||
* The 404 content, fetched only when the 404 is actually shown.
|
||||
*
|
||||
* Next puts the `not-found` tree of a layout into the RSC payload of EVERY page
|
||||
* under it (the router needs it for a client-side 404). A 404 page with real
|
||||
* content (often the homepage) rendered there in every locale multiplied the
|
||||
* size of each page. Here only this small component is in the payload; the
|
||||
* content comes from a server action on mount.
|
||||
*/
|
||||
export function NotFoundLoader({ defaultLocale, load, locales }: NotFoundLoaderProps) {
|
||||
const pathname = usePathname() ?? ''
|
||||
const locale = localeFromPath(pathname, locales, defaultLocale)
|
||||
const [view, setView] = useState<{ locale: string; node: ReactNode } | null>(null)
|
||||
|
||||
useEffect(() => {
|
||||
let active = true
|
||||
load(locale)
|
||||
.then((node) => {
|
||||
if (active) {setView({ locale, node })}
|
||||
})
|
||||
.catch((error: unknown) => {
|
||||
// the status is already 404 — without the content the page stays empty, the error goes to the console
|
||||
console.error('[ipal] NotFoundLoader: the 404 content could not be loaded', error)
|
||||
})
|
||||
return () => {
|
||||
active = false
|
||||
}
|
||||
}, [load, locale])
|
||||
|
||||
return <>{view?.locale === locale ? view.node : null}</>
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
/**
|
||||
* The locale from the first URL segment (`/en/...` → `en`), or `defaultLocale`
|
||||
* when the segment isn't one of `locales` (single-locale site, unknown prefix).
|
||||
*/
|
||||
export function localeFromPath(pathname: string, locales: readonly string[], defaultLocale: string): string {
|
||||
const segment = pathname.split('/').filter(Boolean)[0] ?? ''
|
||||
return locales.includes(segment) ? segment : defaultLocale
|
||||
}
|
||||
Reference in New Issue
Block a user