added auto generating for sitemap.

This commit is contained in:
2026-07-31 23:12:43 +02:00
parent 24a1fe0d60
commit f6b4b36df8
10 changed files with 428 additions and 12 deletions
+59 -1
View File
@@ -4,10 +4,19 @@ import { getPayload } from 'payload'
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 { getArchiveEntries, resolveRoute as resolveRouteRaw } from '../content/index.js'
import { buildRobots, buildSitemapEntries } from '../seo/index.js'
type CreateContentHelpersArgs = {
/**
* Absolute site origin for sitemap/robots URLs. Falls back to
* NEXT_PUBLIC_SERVER_URL, then to a relative origin (which most crawlers
* reject, so set one in production).
*/
baseUrl?: string
/**
* The client's payload config promise (the default export of payload.config).
* Passed in because the plugin never imports the client's config directly.
@@ -15,6 +24,11 @@ type CreateContentHelpersArgs = {
config: Promise<SanitizedConfig> | SanitizedConfig
/** Archive-backed collections, same value as the plugin option. */
content?: ContentOption
/**
* i18n config. Required only if you want the ready-made `sitemap` / `robots`
* handlers — they need the locale list to emit hreflang.
*/
i18n?: I18nConfig
/** Pages collection slug. Defaults to 'pages'. */
pagesSlug?: string
/** SiteSettings global slug. Defaults to 'site-settings'. */
@@ -48,11 +62,14 @@ type CreateContentHelpersArgs = {
* not the second, and a page component composes them in two obvious lines.
*/
export function createContentHelpers({
baseUrl,
config,
content,
i18n,
pagesSlug = 'pages',
settingsSlug = 'site-settings',
}: CreateContentHelpersArgs) {
const origin = baseUrl ?? process.env.NEXT_PUBLIC_SERVER_URL ?? ''
const getCachedPayload = cache(async (): Promise<BasePayload> =>
getPayload({ config: await config }),
)
@@ -92,5 +109,46 @@ export function createContentHelpers({
},
)
return { getCachedPayload, getConfiguredLocales, getEntries, getSettings, resolveRoute }
/**
* Ready-made handler for Next's `app/sitemap.ts` — every page and entry with
* per-URL hreflang and lastmod. Re-export it directly:
*
* ```ts
* // app/sitemap.ts
* export { sitemap as default } from '@/lib/content'
* ```
*/
const sitemap = cache(async (): Promise<SitemapEntry[]> => {
if (!i18n) {
throw new Error('[ipal] createContentHelpers: pass `i18n` to use the sitemap handler.')
}
return buildSitemapEntries({
baseUrl: origin,
config: i18n,
content,
pagesSlug,
payload: await getCachedPayload(),
settingsSlug,
})
})
/**
* Ready-made handler for Next's `app/robots.ts`. Re-export directly:
*
* ```ts
* // app/robots.ts
* export { robots as default } from '@/lib/content'
* ```
*/
const robots = (): RobotsRules => buildRobots({ baseUrl: origin })
return {
getCachedPayload,
getConfiguredLocales,
getEntries,
getSettings,
resolveRoute,
robots,
sitemap,
}
}