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 /** * 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 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 settingsSlug?: string siteNameField?: string }): Promise { // 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 | 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 = {} for (const [loc, value] of Object.entries(slugField as Record)) { 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) : 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, } }