Fixed support for multi-langual slugs

This commit is contained in:
2026-09-09 09:56:47 +02:00
parent db2ac80a1e
commit 2ecfc2e12d
41 changed files with 646 additions and 212 deletions
+59 -18
View File
@@ -1,21 +1,25 @@
import type { BasePayload } from 'payload'
import type { TitleOrder } from './composeTitle.js'
type SettingsShape = {
homepage?: { id: string | number; slug?: unknown } | string | number | null
titleSeparator?: string | null
titleOrder?: TitleOrder | null
[key: string]: unknown
homepage?: { id: number | string; slug?: unknown } | null | number | string
titleOrder?: null | TitleOrder
titleSeparator?: null | string
}
export type SiteMetaConfig = {
/** Slug of the page assigned as Homepage; collapses to the locale root. */
homeSlug?: string
order?: TitleOrder
siteName: string | null
/** Padded separator, e.g. ' | ' — the panel stores the bare character. */
separator?: string
siteName: null | string
order?: TitleOrder
/**
* 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>
}
/**
@@ -27,13 +31,13 @@ export type SiteMetaConfig = {
* relationship — it should receive a resolved config.
*/
export async function readSiteMetaConfig({
locale,
payload,
settingsSlug = 'site-settings',
siteNameField = 'siteName',
}: {
locale: string
payload,
locale,
settingsSlug = 'site-settings',
siteNameField = 'siteName',
}: {
payload: BasePayload
locale: string
settingsSlug?: string
siteNameField?: string
}): Promise<SiteMetaConfig> {
@@ -41,18 +45,55 @@ export async function readSiteMetaConfig({
// without a second query.
const settings = (await payload.findGlobal({
slug: settingsSlug as never,
depth: 1,
locale: locale as never,
depth: 1,
})) 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({
collection: 'pages' as never,
id: homepage.id,
locale: 'all' as never,
depth: 0,
})) 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 }
}
return {
siteName: (settings[siteNameField] as string | undefined) ?? null,
// The panel stores '|'; titles need it padded.
homeSlug: typeof homepage?.slug === 'string' ? homepage.slug : undefined,
order: settings.titleOrder ?? undefined,
separator: settings.titleSeparator ? ` ${settings.titleSeparator} ` : undefined,
order: settings.titleOrder ?? undefined,
homeSlug,
}
}
}