Files
ipal-kit/dist/modules/seo/readSiteMetaConfig.js

70 lines
2.9 KiB
JavaScript

/**
* 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, payload, settingsSlug = 'site-settings', siteNameField = 'siteName' }) {
// depth 1 populates the homepage relationship, so its slug is available
// without a second query.
const settings = await payload.findGlobal({
slug: settingsSlug,
depth: 1,
locale: locale
});
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;
if (homepage?.id) {
try {
const homeDoc = await payload.findByID({
id: homepage.id,
collection: 'pages',
depth: 0,
locale: 'all'
});
const slugField = homeDoc.slug;
if (slugField && typeof slugField === 'object') {
const map = {};
for (const [loc, value] of Object.entries(slugField)){
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 {
siteDescription: settings.siteDescription ?? null,
siteName: settings[siteNameField] ?? null,
// The panel stores '|'; titles need it padded.
homeSlug,
order: settings.titleOrder ?? undefined,
separator: settings.titleSeparator ? ` ${settings.titleSeparator} ` : undefined
};
}
//# sourceMappingURL=readSiteMetaConfig.js.map