Added module Links
This commit is contained in:
@@ -5,14 +5,10 @@ import { cache } from 'react'
|
||||
|
||||
import type { ArchiveEntries, ContentOption, ResolvedRoute } from '../content/index.js'
|
||||
import type { I18nConfig } from '../i18n/index.js'
|
||||
import type { RobotsRules, SitemapEntry } from '../seo/index.js'
|
||||
import type { PageMetadata, RobotsOptions, RobotsRules, SitemapEntry } from '../seo/index.js'
|
||||
|
||||
import {
|
||||
getArchiveEntries,
|
||||
resolveNotFoundPage,
|
||||
resolveRoute as resolveRouteRaw,
|
||||
} from '../content/index.js'
|
||||
import { buildRobots, buildSitemapEntries } from '../seo/index.js'
|
||||
import { getArchiveEntries, resolveRoute as resolveRouteRaw } from '../content/index.js'
|
||||
import { buildRobots, buildSitemapEntries, createPageMetadata } from '../seo/index.js'
|
||||
|
||||
type CreateContentHelpersArgs = {
|
||||
/**
|
||||
@@ -35,6 +31,12 @@ type CreateContentHelpersArgs = {
|
||||
i18n?: I18nConfig
|
||||
/** Pages collection slug. Defaults to 'pages'. */
|
||||
pagesSlug?: string
|
||||
/**
|
||||
* Options for the ready-made `robots` handler, e.g.
|
||||
* `{ disallowParams: ['s', 'meter'], aiBots: 'block-training' }`, or
|
||||
* `{ adminRoute: false }` when the admin path is hidden (ipalKit adminRoute).
|
||||
*/
|
||||
robots?: RobotsOptions
|
||||
/** SiteSettings global slug. Defaults to 'site-settings'. */
|
||||
settingsSlug?: string
|
||||
}
|
||||
@@ -71,6 +73,7 @@ export function createContentHelpers({
|
||||
content,
|
||||
i18n,
|
||||
pagesSlug = 'pages',
|
||||
robots: robotsOptions,
|
||||
settingsSlug = 'site-settings',
|
||||
}: CreateContentHelpersArgs) {
|
||||
const origin = baseUrl ?? process.env.NEXT_PUBLIC_SERVER_URL ?? ''
|
||||
@@ -83,6 +86,10 @@ export function createContentHelpers({
|
||||
return c.localization ? c.localization.locales.map((l) => l.code) : []
|
||||
})
|
||||
|
||||
/**
|
||||
* SiteSettings for a locale — ONE query per locale per request, shared by the
|
||||
* layout, generatePageMetadata, resolveRoute and anything else that asks.
|
||||
*/
|
||||
const getSettings = cache(async (locale: string) => {
|
||||
const payload = await getCachedPayload()
|
||||
return payload.findGlobal({ slug: settingsSlug as never, depth: 2, locale: locale as never })
|
||||
@@ -96,10 +103,70 @@ export function createContentHelpers({
|
||||
page: number,
|
||||
): Promise<null | ResolvedRoute> => {
|
||||
const payload = await getCachedPayload()
|
||||
return resolveRouteRaw({ content, locale, page, pagesSlug, payload, segments, settingsSlug })
|
||||
const settings = (await getSettings(locale)) as Record<string, unknown>
|
||||
return resolveRouteRaw({
|
||||
content,
|
||||
locale,
|
||||
page,
|
||||
pagesSlug,
|
||||
payload,
|
||||
segments,
|
||||
settings,
|
||||
settingsSlug,
|
||||
})
|
||||
},
|
||||
)
|
||||
|
||||
/**
|
||||
* generateMetadata for the catch-all page, sharing the request cache with
|
||||
* the page component: the same settings read and the same resolveRoute call
|
||||
* (React cache spans generateMetadata and the page within one request), so
|
||||
* metadata costs no extra route/settings queries.
|
||||
*
|
||||
* export async function generateMetadata({ params }) {
|
||||
* const { locale, slug } = await params
|
||||
* return generatePageMetadata(locale, slug)
|
||||
* }
|
||||
*/
|
||||
let pageMetadata: ReturnType<typeof createPageMetadata> | undefined
|
||||
const generatePageMetadata = async (
|
||||
locale: string,
|
||||
segments: string[] | undefined,
|
||||
page = 1,
|
||||
): Promise<PageMetadata> => {
|
||||
if (!i18n) {
|
||||
throw new Error('[ipal] createContentHelpers: pass `i18n` to use generatePageMetadata.')
|
||||
}
|
||||
pageMetadata ??= createPageMetadata({
|
||||
baseUrl: origin || undefined,
|
||||
collection: pagesSlug,
|
||||
config: i18n,
|
||||
content,
|
||||
loaders: {
|
||||
getSettings: async (l) => (await getSettings(l)) as Record<string, unknown>,
|
||||
resolveRoute,
|
||||
},
|
||||
settingsSlug,
|
||||
})
|
||||
return pageMetadata({ slug: segments, locale, page, payload: await getCachedPayload() })
|
||||
}
|
||||
|
||||
/** ID of the page assigned as 404 — the relation isn't localized, so one read serves every locale. */
|
||||
const getNotFoundPageId = cache(async (): Promise<null | number | string> => {
|
||||
const payload = await getCachedPayload()
|
||||
const settings = (await payload.findGlobal({
|
||||
slug: settingsSlug as never,
|
||||
depth: 0,
|
||||
})) as { notFound?: unknown }
|
||||
const rel = settings.notFound
|
||||
if (typeof rel === 'string' || typeof rel === 'number') {return rel}
|
||||
if (rel && typeof rel === 'object') {
|
||||
const id = (rel as { id?: unknown }).id
|
||||
if (typeof id === 'string' || typeof id === 'number') {return id}
|
||||
}
|
||||
return null
|
||||
})
|
||||
|
||||
/**
|
||||
* The page assigned as 404 in System Pages (or null → built-in fallback).
|
||||
* For app/not-found.tsx; never throws. not-found.tsx receives no params and
|
||||
@@ -108,10 +175,18 @@ export function createContentHelpers({
|
||||
*/
|
||||
const getNotFoundPage = cache(async (locale: string) => {
|
||||
try {
|
||||
const id = await getNotFoundPageId()
|
||||
if (id === null) {return null}
|
||||
const payload = await getCachedPayload()
|
||||
return await resolveNotFoundPage({ locale, pagesSlug, payload, settingsSlug })
|
||||
const doc = await payload.findByID({
|
||||
id,
|
||||
collection: pagesSlug as never,
|
||||
depth: 2,
|
||||
locale: locale as never,
|
||||
})
|
||||
return (doc as null | Record<string, unknown>) ?? null
|
||||
} catch {
|
||||
return null // DB unreachable (container build) — fallback view
|
||||
return null // DB unreachable (container build) or page deleted — fallback view
|
||||
}
|
||||
})
|
||||
|
||||
@@ -186,7 +261,7 @@ export function createContentHelpers({
|
||||
* export { robots as default } from '@/lib/content'
|
||||
* ```
|
||||
*/
|
||||
const robots = (): RobotsRules => buildRobots({ baseUrl: origin })
|
||||
const robots = (): RobotsRules => buildRobots({ ...robotsOptions, baseUrl: origin })
|
||||
|
||||
/**
|
||||
* Next.js generateStaticParams for the [[...slug]] route (or
|
||||
@@ -310,6 +385,7 @@ export function createContentHelpers({
|
||||
}
|
||||
|
||||
return {
|
||||
generatePageMetadata,
|
||||
generateStaticParams,
|
||||
getCachedPayload,
|
||||
getConfiguredLocales,
|
||||
|
||||
Reference in New Issue
Block a user