The titleSeparator select stored '|', '–', '-', '·', '/'. Select values become GraphQL enum names; '|', '–', '·' are invalid and '-' and '/' collide on '_', so every project's /api/graphql answered 500 since 1.0.1 (admin/REST unaffected). - Values are now pipe/endash/hyphen/middot/slash (same labels); TITLE_SEPARATORS and titleSeparatorChar map them to the character; titles accept old characters and custom project values literally - Field afterRead/beforeChange turn an old stored character into its name - MongoDB: rewritten at start (migrateTitleSeparator); Postgres: titleSeparatorSql() for the generated migration (verified up/down/up against Postgres 16) - Test builds the full plugin GraphQL schema and fails on invalid or duplicate enum names; unit tests for the mapping, hooks, SQL and Mongo rewrite - CHANGELOG Unreleased (Fixed + upgrade step 5), seo.md, ipal-upgrade, api.md Co-Authored-By: Claude Opus 5.5 <[email protected]>
123 lines
4.8 KiB
TypeScript
123 lines
4.8 KiB
TypeScript
import type { BasePayload } from 'payload'
|
|
|
|
import type { TitleOrder } from './composeTitle.js'
|
|
import type { VerificationMetadata } from './searchSettings.js'
|
|
|
|
import { verificationFrom } from './searchSettings.js'
|
|
import { titleSeparatorChar } from './titleSeparator.js'
|
|
|
|
type SettingsShape = {
|
|
[key: string]: unknown
|
|
homepage?: { id: number | string; slug?: unknown } | null | number | string
|
|
titleOrder?: null | TitleOrder
|
|
titleSeparator?: null | string
|
|
}
|
|
|
|
export type SiteMetaConfig = {
|
|
/** Site Settings → defaultShareImage (populated upload), used when a page has no OG image. */
|
|
defaultShareImage?: null | Record<string, unknown>
|
|
/**
|
|
* Home slug per locale, e.g. { pl: 'strona-glowna', de: 'startseite' }. A map
|
|
* (not a single string) so multilingual homepages collapse to root in EVERY
|
|
* language — otherwise hreflang points other languages at '/de/startseite'
|
|
* instead of '/de', breaking return tags (a real GSC error).
|
|
*/
|
|
homeSlug?: Record<string, string>
|
|
order?: TitleOrder
|
|
/** Padded separator, e.g. ' | ' — the panel stores the bare character. */
|
|
separator?: string
|
|
/** Global fallback meta description. */
|
|
siteDescription?: null | string
|
|
siteName: null | string
|
|
/** Site Settings → Search & AI → verification codes (Search Console, Bing, Yandex). */
|
|
verification?: VerificationMetadata
|
|
}
|
|
|
|
/**
|
|
* Reads the site-wide inputs to title and URL composition from SiteSettings.
|
|
*
|
|
* Split out of metadata assembly because these are one concern with one source:
|
|
* whatever an editor set in the panel. Metadata generation shouldn't also know
|
|
* that the separator arrives unpadded, or that the home slug hides inside a
|
|
* relationship — it should receive a resolved config.
|
|
*/
|
|
export async function readSiteMetaConfig({
|
|
locale,
|
|
pagesSlug = 'pages',
|
|
payload,
|
|
settings: preloaded,
|
|
settingsSlug = 'site-settings',
|
|
siteNameField = 'siteName',
|
|
}: {
|
|
locale: string
|
|
/** Collection holding the homepage. Default 'pages'. */
|
|
pagesSlug?: string
|
|
payload: BasePayload
|
|
/** SiteSettings already read for this locale (depth ≥ 1) — skips the read. */
|
|
settings?: null | Record<string, unknown>
|
|
settingsSlug?: string
|
|
siteNameField?: string
|
|
}): Promise<SiteMetaConfig> {
|
|
// depth 1 populates the homepage relationship, so its slug is available
|
|
// without a second query.
|
|
const settings = (preloaded ??
|
|
(await payload.findGlobal({
|
|
slug: settingsSlug as never,
|
|
depth: 1,
|
|
locale: locale as never,
|
|
}))) as SettingsShape
|
|
|
|
const homepage =
|
|
settings.homepage && typeof settings.homepage === 'object' ? settings.homepage : null
|
|
|
|
// Build the per-locale home slug map. The homepage's slug is localized, so a
|
|
// single-locale read only gives THIS language's slug. Re-read the homepage
|
|
// with locale:'all' to get every language's slug — needed so hreflang can
|
|
// collapse each language's home to its root.
|
|
let homeSlug: Record<string, string> | undefined
|
|
if (homepage?.id) {
|
|
try {
|
|
const homeDoc = (await payload.findByID({
|
|
id: homepage.id,
|
|
collection: pagesSlug as never,
|
|
depth: 0,
|
|
locale: 'all' as never,
|
|
})) as { slug?: unknown }
|
|
const slugField = homeDoc.slug
|
|
if (slugField && typeof slugField === 'object') {
|
|
const map: Record<string, string> = {}
|
|
for (const [loc, value] of Object.entries(slugField as Record<string, unknown>)) {
|
|
if (typeof value === 'string') {map[loc] = value}
|
|
}
|
|
if (Object.keys(map).length > 0) {homeSlug = map}
|
|
} else if (typeof slugField === 'string') {
|
|
// Non-localized slug (same in every language) — key it by current locale;
|
|
// buildLocalizedPath tolerates a partial map. Non-localized homepage
|
|
// slugs are rare, so this branch is a safety net, not the norm.
|
|
homeSlug = { [locale]: slugField }
|
|
}
|
|
} catch {
|
|
// Fall back to the single-locale slug below.
|
|
}
|
|
}
|
|
|
|
// Fallback: if the 'all' read didn't yield a map, use the current locale's slug
|
|
// for at least this locale (better than nothing).
|
|
if (!homeSlug && typeof homepage?.slug === 'string') {
|
|
homeSlug = { [locale]: homepage.slug }
|
|
}
|
|
|
|
const share = settings.defaultShareImage
|
|
const verification = verificationFrom(settings)
|
|
return {
|
|
...(verification ? { verification } : {}),
|
|
defaultShareImage: share && typeof share === 'object' ? (share as Record<string, unknown>) : null,
|
|
siteDescription: (settings.siteDescription as string | undefined) ?? null,
|
|
siteName: (settings[siteNameField] as string | undefined) ?? null,
|
|
// The panel stores '|'; titles need it padded.
|
|
homeSlug,
|
|
order: settings.titleOrder ?? undefined,
|
|
separator: titleSeparatorChar(settings.titleSeparator) ? ` ${titleSeparatorChar(settings.titleSeparator)} ` : undefined,
|
|
}
|
|
}
|