Sitemap.css is now builded in plugin
This commit is contained in:
@@ -1,10 +1,9 @@
|
||||
import type { BasePayload } from 'payload'
|
||||
|
||||
import type { I18nConfig } from '../i18n/index.js'
|
||||
import type { SitemapEntry } from './buildSitemapEntries.js'
|
||||
|
||||
import { buildLlmsTxt } from './buildLlmsTxt.js'
|
||||
import { buildSitemapXml } from './buildSitemapXml.js'
|
||||
import type { SitemapEntry } from './buildSitemapEntries.js'
|
||||
import { buildSitemapCss, SITEMAP_STYLE_PARAM, type SitemapStyleOptions } from './sitemapStyle.js'
|
||||
|
||||
/**
|
||||
* Ready-made Next route handler for /llms.txt. Replaces the ~60-line try/catch
|
||||
@@ -26,13 +25,13 @@ import { buildSitemapXml } from './buildSitemapXml.js'
|
||||
* baseUrl: process.env.NEXT_PUBLIC_SERVER_URL!, full: true })
|
||||
*/
|
||||
export function createLlmsTxtRoute(opts: {
|
||||
baseUrl: string
|
||||
config: I18nConfig
|
||||
/** Inline every page's full Markdown (llms-full.txt). Default false. */
|
||||
full?: boolean
|
||||
getCachedPayload: () => Promise<BasePayload>
|
||||
config: I18nConfig
|
||||
baseUrl: string
|
||||
pagesSlug?: string
|
||||
settingsSlug?: string
|
||||
/** Inline every page's full Markdown (llms-full.txt). Default false. */
|
||||
full?: boolean
|
||||
/** Field holding a richer AI summary (buildSummaryField) to prefer as description. */
|
||||
summaryField?: string
|
||||
}): () => Promise<Response> {
|
||||
@@ -40,12 +39,12 @@ export function createLlmsTxtRoute(opts: {
|
||||
try {
|
||||
const payload = await opts.getCachedPayload()
|
||||
const body = await buildLlmsTxt({
|
||||
baseUrl: opts.baseUrl,
|
||||
config: opts.config,
|
||||
full: opts.full,
|
||||
pagesSlug: opts.pagesSlug,
|
||||
payload,
|
||||
config: opts.config,
|
||||
baseUrl: opts.baseUrl,
|
||||
pagesSlug: opts.pagesSlug,
|
||||
settingsSlug: opts.settingsSlug,
|
||||
full: opts.full,
|
||||
summaryField: opts.summaryField,
|
||||
})
|
||||
return new Response(body, {
|
||||
@@ -62,28 +61,58 @@ export function createLlmsTxtRoute(opts: {
|
||||
}
|
||||
}
|
||||
|
||||
export type SitemapRouteOptions = {
|
||||
/**
|
||||
* Built-in browser look (default ON): the XML points at `<this route>?style`
|
||||
* and the same handler serves the CSS — nothing to copy into /public.
|
||||
* `false` = raw XML. Object = header/credit/accent (SitemapStyleOptions);
|
||||
* `title` may be async (sitemapRoute passes the Site Settings name).
|
||||
*/
|
||||
style?: false | (Omit<SitemapStyleOptions, 'title'> & { title?: string | (() => Promise<string | undefined>) })
|
||||
/** Legacy: your own stylesheet URL (e.g. '/sitemap.css'). Overrides `style`. */
|
||||
cssUrl?: string
|
||||
}
|
||||
|
||||
/**
|
||||
* Ready-made Next route handler for /sitemap.xml with the CSS stylesheet.
|
||||
* Replaces the hand-written route in every project. Pass the project's `sitemap`
|
||||
* helper (from createContentHelpers) and optionally a cssUrl.
|
||||
* Ready-made Next route handler for /sitemap.xml, styled for humans out of the box.
|
||||
* Usually you don't call this — `createContentHelpers` returns `sitemapRoute`:
|
||||
*
|
||||
* // app/sitemap.xml/route.ts
|
||||
* import { createSitemapRouteHandler } from '@intecion/ipal-kit'
|
||||
* import { sitemap } from '@/lib/content'
|
||||
* // app/sitemap.xml/route.ts — the whole file
|
||||
* export { sitemapRoute as GET } from '@/lib/content'
|
||||
* export const dynamic = 'force-dynamic'
|
||||
* export const GET = createSitemapRouteHandler(sitemap, { cssUrl: '/sitemap.css' })
|
||||
*
|
||||
* Remember: if you use this, DON'T also keep app/sitemap.ts (two sitemaps
|
||||
* confuse crawlers).
|
||||
* `GET /sitemap.xml` → XML (crawlers), `GET /sitemap.xml?style` → its CSS.
|
||||
* Don't keep app/sitemap.ts next to it — two sitemaps confuse crawlers.
|
||||
*/
|
||||
export function createSitemapRouteHandler(
|
||||
sitemap: () => Promise<SitemapEntry[]>,
|
||||
opts: { cssUrl?: string } = {},
|
||||
): () => Promise<Response> {
|
||||
return async () => {
|
||||
opts: SitemapRouteOptions = {},
|
||||
): (req?: Request) => Promise<Response> {
|
||||
const style = opts.cssUrl ? false : opts.style === false ? false : (opts.style ?? {})
|
||||
|
||||
return async (req?: Request) => {
|
||||
const url = req ? new URL(req.url) : null
|
||||
|
||||
if (style && url?.searchParams.has(SITEMAP_STYLE_PARAM)) {
|
||||
let title: string | undefined
|
||||
try {
|
||||
title = typeof style.title === 'function' ? await style.title() : style.title
|
||||
} catch {
|
||||
title = undefined // DB down → generic heading, never a broken stylesheet
|
||||
}
|
||||
return new Response(buildSitemapCss({ ...style, title }), {
|
||||
headers: {
|
||||
'Content-Type': 'text/css; charset=utf-8',
|
||||
'Cache-Control': 'public, max-age=3600',
|
||||
'X-Robots-Tag': 'noindex',
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
const cssUrl = opts.cssUrl ?? (style ? `${url?.pathname ?? '/sitemap.xml'}?${SITEMAP_STYLE_PARAM}` : undefined)
|
||||
try {
|
||||
const entries = await sitemap()
|
||||
const xml = buildSitemapXml(entries, { cssUrl: opts.cssUrl })
|
||||
const xml = buildSitemapXml(entries, { cssUrl })
|
||||
return new Response(xml, {
|
||||
headers: { 'Content-Type': 'application/xml; charset=utf-8' },
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user