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]>
68 lines
2.9 KiB
TypeScript
68 lines
2.9 KiB
TypeScript
import { isValidElement, type ReactElement } from 'react'
|
|
import { describe, expect, test, vi } from 'vitest'
|
|
|
|
vi.mock('next/navigation.js', () => ({ usePathname: () => '/en/missing' }))
|
|
|
|
import { createNotFoundPage, renderNotFoundView } from '../src/modules/frontend/pageRoute.js'
|
|
import { localeFromPath } from '../src/modules/pages/localeFromPath.js'
|
|
import { LocaleView } from '../src/modules/pages/LocaleView.js'
|
|
import { NotFoundLoader } from '../src/modules/pages/NotFoundLoader.js'
|
|
|
|
const layout = [{ id: '1', blockType: 'hero' }]
|
|
const helpers = () =>
|
|
({
|
|
getConfiguredLocales: vi.fn(async () => ['pl', 'en']),
|
|
getNotFoundPage: vi.fn(async (locale: string) => ({ layout, locale })),
|
|
i18n: { defaultLocale: 'pl', locales: [{ code: 'pl' }, { code: 'en' }] },
|
|
}) as never as { getNotFoundPage: ReturnType<typeof vi.fn> } & Parameters<typeof renderNotFoundView>[0]
|
|
|
|
describe('localeFromPath', () => {
|
|
test('first segment when it is a locale, default otherwise', () => {
|
|
expect(localeFromPath('/en/missing', ['pl', 'en'], 'pl')).toBe('en')
|
|
expect(localeFromPath('/de/missing', ['pl', 'en'], 'pl')).toBe('pl')
|
|
expect(localeFromPath('', ['pl'], 'pl')).toBe('pl')
|
|
})
|
|
})
|
|
|
|
describe('createNotFoundPage', () => {
|
|
test('lazy (load): only the loader goes into the payload — no 404 page is read or rendered', async () => {
|
|
const h = helpers()
|
|
const load = vi.fn(async () => null)
|
|
const element = (await createNotFoundPage(h, { load })()) as ReactElement<{ load: unknown; locales: string[] }>
|
|
expect(element.type).toBe(NotFoundLoader)
|
|
expect(element.props.locales).toEqual(['pl', 'en'])
|
|
expect(element.props.load).toBe(load)
|
|
expect(h.getNotFoundPage).not.toHaveBeenCalled()
|
|
})
|
|
|
|
test('eager (blocks): one view per locale in LocaleView', async () => {
|
|
const h = helpers()
|
|
const element = (await createNotFoundPage(h, { blocks: {} })()) as ReactElement<{ views: Record<string, unknown> }>
|
|
expect(element.type).toBe(LocaleView)
|
|
expect(Object.keys(element.props.views)).toEqual(['pl', 'en'])
|
|
expect(h.getNotFoundPage).toHaveBeenCalledTimes(2)
|
|
})
|
|
})
|
|
|
|
describe('renderNotFoundView', () => {
|
|
test('renders the requested configured locale', async () => {
|
|
const h = helpers()
|
|
const node = await renderNotFoundView(h, { blocks: {} }, 'en')
|
|
expect(isValidElement(node)).toBe(true)
|
|
expect(h.getNotFoundPage).toHaveBeenCalledWith('en')
|
|
})
|
|
|
|
test('a locale from the browser outside the configured ones → default locale', async () => {
|
|
const h = helpers()
|
|
await renderNotFoundView(h, { blocks: {} }, '../admin')
|
|
await renderNotFoundView(h, { blocks: {} }, 42)
|
|
expect(h.getNotFoundPage.mock.calls).toEqual([['pl'], ['pl']])
|
|
})
|
|
|
|
test('no 404 page in the panel → fallback for that locale', async () => {
|
|
const h = helpers()
|
|
h.getNotFoundPage.mockResolvedValueOnce(null)
|
|
expect(await renderNotFoundView(h, { blocks: {}, fallback: (locale) => `missing:${locale}` }, 'en')).toBe('missing:en')
|
|
})
|
|
})
|