Import instead of code: panel forms (steps, choice lists, dates), layout, page route, GEO routes, links, section fields
- Forms: step and checkboxGroup field types (radio, date on), server validation for them, useBuilderForm (logic) + BuilderForm (unstyled default) + RenderForm (/rsc: rich text, bound action, public part of the form only — the Emails tab never reaches the browser) - Notifications: next / back / stepOf texts - createContentHelpers: getSiteShell, getFormTexts, getLinkOptions, resolveLink, resolveNav, resolvePageRedirect, one-line route handlers (llms, llms-full, md, mcp, health, indexnow, manifest) - /rsc: SiteProviders, createPageRoute, createNotFoundPage - resolveLink / resolveNav / homeSlugFrom, buildSectionHeaderField / buildSectionSettingsField - Dev sandbox frontend built on these imports; tests (unit + real Payload) Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
import type { ReactNode } from 'react'
|
||||
|
||||
import type { BlockComponentMap, BlockData } from '../blocks/types.js'
|
||||
import type { ResolvedRoute } from '../content/index.js'
|
||||
import type { ContentHelpers, PageJsonLdOptions } from './createContentHelpers.js'
|
||||
|
||||
import { RenderBlocks } from '../blocks/RenderBlocks.js'
|
||||
import { LocaleView } from '../pages/LocaleView.js'
|
||||
import { JsonLdGraph } from '../seo/JsonLdGraph.js'
|
||||
|
||||
type Helpers = Pick<
|
||||
ContentHelpers,
|
||||
'generatePageMetadata' | 'generateStaticParams' | 'getConfiguredLocales' | 'getNotFoundPage' | 'getPageJsonLd' | 'i18n' | 'resolvePageRedirect' | 'resolveRoute'
|
||||
>
|
||||
|
||||
type BlockPropsContext = { doc: Record<string, unknown>; locale: string }
|
||||
|
||||
export type PageRouteOptions = {
|
||||
/** Props for every block on top of `locale`, computed once per page — e.g. `{ formTexts, sendForm }` for form blocks. */
|
||||
blockProps?: (ctx: BlockPropsContext) => Promise<Record<string, unknown>> | Record<string, unknown>
|
||||
/** blockType → component (the project's registry). */
|
||||
blocks: BlockComponentMap
|
||||
/** Per-page JSON-LD (WebPage, breadcrumbs, FAQ…). Default true; options as getPageJsonLd. */
|
||||
jsonLd?: boolean | PageJsonLdOptions
|
||||
/** Field with the blocks. Default 'layout'. */
|
||||
layoutField?: string
|
||||
/** `notFound` from 'next/navigation' — injected, the plugin doesn't import next/* on the server. */
|
||||
notFound: () => never
|
||||
/** `permanentRedirect` from 'next/navigation' (308 for an old slug). */
|
||||
permanentRedirect: (url: string) => never
|
||||
/**
|
||||
* Render a route yourself — an archive listing, a blog entry template.
|
||||
* Return undefined to keep the default (the document's blocks).
|
||||
*/
|
||||
render?: (ctx: { blocks: ReactNode; locale: string; route: ResolvedRoute; segments: string[] }) => Promise<ReactNode | undefined> | ReactNode | undefined
|
||||
}
|
||||
|
||||
type PageArgs = { params: Promise<{ locale?: string; slug?: string[] }> }
|
||||
|
||||
/**
|
||||
* The catch-all page — route resolution, old-slug 308, 404, metadata, static
|
||||
* params, per-page JSON-LD and the blocks — from one call:
|
||||
*
|
||||
* // src/app/(frontend)/[locale]/[[...slug]]/page.tsx
|
||||
* import { notFound, permanentRedirect } from 'next/navigation'
|
||||
* import { createPageRoute } from '@intecion/ipal-kit/rsc'
|
||||
* import * as content from '@/lib/content'
|
||||
* import { blockRegistry } from '@/blocks/registry'
|
||||
*
|
||||
* export const revalidate = 3600 // must stay a literal in the file (Next reads it statically)
|
||||
* const route = createPageRoute(content, { blocks: blockRegistry, notFound, permanentRedirect })
|
||||
* export default route.Page
|
||||
* export const generateMetadata = route.generateMetadata
|
||||
* export const generateStaticParams = route.generateStaticParams
|
||||
*
|
||||
* Archive listings / entry templates: `render`. Anything else: write the page
|
||||
* yourself with the same helpers (PROGRAMISTA §2.8) — nothing here is required.
|
||||
*/
|
||||
export function createPageRoute(helpers: Helpers, options: PageRouteOptions) {
|
||||
const { blockProps, blocks, jsonLd = true, layoutField = 'layout', notFound, permanentRedirect, render } = options
|
||||
const localeOf = (locale: string | undefined) => {
|
||||
const l = locale ?? helpers.i18n?.defaultLocale
|
||||
if (!l) {throw new Error('[ipal] createPageRoute: no locale param and no i18n.defaultLocale in createContentHelpers.')}
|
||||
return l
|
||||
}
|
||||
|
||||
async function generateMetadata({ params }: PageArgs) {
|
||||
const { slug, locale } = await params
|
||||
return helpers.generatePageMetadata(localeOf(locale), slug)
|
||||
}
|
||||
|
||||
async function Page({ params }: PageArgs) {
|
||||
const { slug, locale: param } = await params
|
||||
const locale = localeOf(param)
|
||||
const segments = slug ?? []
|
||||
const route = await helpers.resolveRoute(locale, segments, 1)
|
||||
if (!route) {
|
||||
const target = await helpers.resolvePageRedirect(locale, segments)
|
||||
return target ? permanentRedirect(target) : notFound()
|
||||
}
|
||||
const doc = route.doc
|
||||
const extra = blockProps ? await blockProps({ doc, locale }) : {}
|
||||
const rendered = (
|
||||
<RenderBlocks
|
||||
blocks={doc[layoutField] as BlockData[] | undefined}
|
||||
components={blocks}
|
||||
enhanceProps={() => ({ locale, ...extra })}
|
||||
/>
|
||||
)
|
||||
const custom = render ? await render({ blocks: rendered, locale, route, segments }) : undefined
|
||||
const schemas = jsonLd ? await helpers.getPageJsonLd(locale, slug, jsonLd === true ? {} : jsonLd) : []
|
||||
return (
|
||||
<>
|
||||
{schemas.length ? <JsonLdGraph schemas={schemas} /> : null}
|
||||
{custom ?? rendered}
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
return { generateMetadata, generateStaticParams: helpers.generateStaticParams, Page }
|
||||
}
|
||||
|
||||
export type NotFoundPageOptions = {
|
||||
blockProps?: (ctx: BlockPropsContext) => Promise<Record<string, unknown>> | Record<string, unknown>
|
||||
blocks: BlockComponentMap
|
||||
/** Shown when no 404 page is assigned in Site Settings (or the DB is unreachable). Per locale or one node. */
|
||||
fallback?: ((locale: string) => ReactNode) | ReactNode
|
||||
layoutField?: string
|
||||
}
|
||||
|
||||
/**
|
||||
* app/(frontend)/[locale]/not-found.tsx — the 404 page from the panel, in the
|
||||
* visitor's language, WITHOUT headers() (the route stays static): one view per
|
||||
* locale rendered here, <LocaleView> picks the one from the URL.
|
||||
*
|
||||
* import { createNotFoundPage } from '@intecion/ipal-kit/rsc'
|
||||
* export default createNotFoundPage(content, { blocks: blockRegistry })
|
||||
*/
|
||||
export function createNotFoundPage(helpers: Helpers, options: NotFoundPageOptions) {
|
||||
const { blockProps, blocks, fallback = null, layoutField = 'layout' } = options
|
||||
return async function NotFoundPage() {
|
||||
const configured = await helpers.getConfiguredLocales()
|
||||
const locales = configured.length ? configured : [helpers.i18n?.defaultLocale ?? '']
|
||||
const views = Object.fromEntries(
|
||||
await Promise.all(
|
||||
locales.map(async (locale) => {
|
||||
const doc = await helpers.getNotFoundPage(locale)
|
||||
const layout = doc?.[layoutField] as BlockData[] | undefined
|
||||
const fb = typeof fallback === 'function' ? fallback(locale) : fallback
|
||||
if (!doc || !layout?.length) {return [locale, fb] as const}
|
||||
const extra = blockProps ? await blockProps({ doc, locale }) : {}
|
||||
return [locale, <RenderBlocks blocks={layout} components={blocks} enhanceProps={() => ({ locale, ...extra })} key={locale} />] as const
|
||||
}),
|
||||
),
|
||||
)
|
||||
return <LocaleView defaultLocale={helpers.i18n?.defaultLocale ?? locales[0]} views={views} />
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user