Added richtext support, and helpers for proxy, llms route
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
import type { BasePayload } from 'payload'
|
||||
|
||||
import type { I18nConfig } from '../i18n/index.js'
|
||||
|
||||
import { getDefaultLocale } from '../i18n/index.js'
|
||||
|
||||
type MediaLike =
|
||||
| { height?: null | number; mimeType?: null | string; url?: null | string; width?: null | number }
|
||||
| null
|
||||
| undefined
|
||||
|
||||
type ManifestIcon = { sizes?: string; src: string; type?: string }
|
||||
|
||||
type Manifest = {
|
||||
background_color?: string
|
||||
display: 'standalone'
|
||||
icons: ManifestIcon[]
|
||||
name?: string
|
||||
short_name?: string
|
||||
start_url: string
|
||||
theme_color?: string
|
||||
}
|
||||
|
||||
type SettingsShape = {
|
||||
backgroundColor?: null | string
|
||||
favicon?: MediaLike
|
||||
logo?: MediaLike
|
||||
siteName?: null | string
|
||||
themeColor?: null | string
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a Next Web App Manifest from SiteSettings — replaces the ~50 lines of
|
||||
* icon/mimeType parsing copied into every project's manifest.ts. Values come
|
||||
* from the panel; missing fields are omitted (no hardcoded client data).
|
||||
*
|
||||
* // app/manifest.ts
|
||||
* import { createManifestHandler } from '@intecion/ipal-kit'
|
||||
* import { getCachedPayload } from '@/lib/content'
|
||||
* import { i18nConfig } from '@/i18n.config'
|
||||
* export default createManifestHandler({ getCachedPayload, config: i18nConfig })
|
||||
*/
|
||||
export function createManifestHandler(opts: {
|
||||
config: I18nConfig
|
||||
getCachedPayload: () => Promise<BasePayload>
|
||||
settingsSlug?: string
|
||||
}): () => Promise<Manifest> {
|
||||
return async () => {
|
||||
const defaultLocale = getDefaultLocale(opts.config)
|
||||
const payload = await opts.getCachedPayload()
|
||||
const settings = (await payload
|
||||
.findGlobal({
|
||||
slug: (opts.settingsSlug ?? 'site-settings') as never,
|
||||
depth: 1,
|
||||
locale: defaultLocale as never,
|
||||
})
|
||||
.catch(() => null)) as null | SettingsShape
|
||||
|
||||
const start_url = `/${defaultLocale}`
|
||||
if (!settings) {
|
||||
return { display: 'standalone', icons: [], start_url }
|
||||
}
|
||||
|
||||
const iconSource = settings.favicon ?? settings.logo
|
||||
const icons: ManifestIcon[] = []
|
||||
if (iconSource?.url) {
|
||||
const type =
|
||||
iconSource.mimeType ?? (iconSource.url.endsWith('.svg') ? 'image/svg+xml' : undefined)
|
||||
const isSvg = type === 'image/svg+xml'
|
||||
const size =
|
||||
iconSource.width && iconSource.height
|
||||
? `${iconSource.width}x${iconSource.height}`
|
||||
: undefined
|
||||
icons.push({
|
||||
src: iconSource.url,
|
||||
...(isSvg ? { sizes: 'any' } : size ? { sizes: size } : {}),
|
||||
...(type ? { type } : {}),
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
...(settings.siteName ? { name: settings.siteName, short_name: settings.siteName } : {}),
|
||||
display: 'standalone',
|
||||
start_url,
|
||||
...(settings.backgroundColor ? { background_color: settings.backgroundColor } : {}),
|
||||
...(settings.themeColor ? { theme_color: settings.themeColor } : {}),
|
||||
icons,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ export { buildFaqJsonLd } from './buildFaqJsonLd.js'
|
||||
export { buildIconsMetadata } from './buildIconsMetadata.js'
|
||||
export { buildLlmsTxt } from './buildLlmsTxt.js'
|
||||
export { buildLocalBusinessJsonLd } from './buildLocalBusinessJsonLd.js'
|
||||
export { createManifestHandler } from './buildManifest.js'
|
||||
export { buildMetadata } from './buildMetadata.js'
|
||||
export type { PageMetadata } from './buildMetadata.js'
|
||||
export { buildOrganizationJsonLd } from './buildOrganizationJsonLd.js'
|
||||
@@ -26,6 +27,7 @@ export { injectAutoFillMeta } from './injectAutoFillMeta.js'
|
||||
export { injectSeoTabs } from './injectSeoTabs.js'
|
||||
export { readSiteMetaConfig } from './readSiteMetaConfig.js'
|
||||
export type { SiteMetaConfig } from './readSiteMetaConfig.js'
|
||||
export { createLlmsTxtRoute, createSitemapRouteHandler } from './routeHandlers.js'
|
||||
export { buildSeoPlugin } from './seoPluginConfig.js'
|
||||
export { slugsAcrossLocales } from './slugsAcrossLocales.js'
|
||||
export type { SeoMeta, SeoOption } from './types.js'
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
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'
|
||||
|
||||
/**
|
||||
* Ready-made Next route handler for /llms.txt. Replaces the ~60-line try/catch
|
||||
* every project was copying. buildLlmsTxt already handles collections without
|
||||
* drafts, so no workaround is needed here.
|
||||
*
|
||||
* // app/llms.txt/route.ts
|
||||
* import { createLlmsTxtRoute } from '@intecion/ipal-kit'
|
||||
* import { getCachedPayload } from '@/lib/content'
|
||||
* import { i18nConfig } from '@/i18n.config'
|
||||
* export const dynamic = 'force-dynamic'
|
||||
* export const GET = createLlmsTxtRoute({ getCachedPayload, config: i18nConfig,
|
||||
* baseUrl: process.env.NEXT_PUBLIC_SERVER_URL! })
|
||||
*/
|
||||
export function createLlmsTxtRoute(opts: {
|
||||
baseUrl: string
|
||||
config: I18nConfig
|
||||
getCachedPayload: () => Promise<BasePayload>
|
||||
pagesSlug?: string
|
||||
settingsSlug?: string
|
||||
}): () => Promise<Response> {
|
||||
return async () => {
|
||||
try {
|
||||
const payload = await opts.getCachedPayload()
|
||||
const body = await buildLlmsTxt({
|
||||
baseUrl: opts.baseUrl,
|
||||
config: opts.config,
|
||||
pagesSlug: opts.pagesSlug,
|
||||
payload,
|
||||
settingsSlug: opts.settingsSlug,
|
||||
})
|
||||
return new Response(body, {
|
||||
headers: { 'Content-Type': 'text/plain; charset=utf-8' },
|
||||
})
|
||||
} catch (err) {
|
||||
// DB unreachable at build (container) — return a minimal valid file so the
|
||||
// route doesn't 500; it fills in at runtime. No per-project try/catch.
|
||||
console.warn('[ipal] createLlmsTxtRoute: could not build llms.txt:', err)
|
||||
return new Response('# llms.txt\n', {
|
||||
headers: { 'Content-Type': 'text/plain; charset=utf-8' },
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* // app/sitemap.xml/route.ts
|
||||
* import { createSitemapRouteHandler } from '@intecion/ipal-kit'
|
||||
* import { sitemap } 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).
|
||||
*/
|
||||
export function createSitemapRouteHandler(
|
||||
sitemap: () => Promise<SitemapEntry[]>,
|
||||
opts: { cssUrl?: string } = {},
|
||||
): () => Promise<Response> {
|
||||
return async () => {
|
||||
try {
|
||||
const entries = await sitemap()
|
||||
const xml = buildSitemapXml(entries, { cssUrl: opts.cssUrl })
|
||||
return new Response(xml, {
|
||||
headers: { 'Content-Type': 'application/xml; charset=utf-8' },
|
||||
})
|
||||
} catch (err) {
|
||||
console.warn('[ipal] createSitemapRouteHandler: could not build sitemap:', err)
|
||||
// Valid empty urlset so the route doesn't 500 during a DB-less build.
|
||||
const empty =
|
||||
'<?xml version="1.0" encoding="UTF-8"?>\n' +
|
||||
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"></urlset>'
|
||||
return new Response(empty, {
|
||||
headers: { 'Content-Type': 'application/xml; charset=utf-8' },
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user