Files

51 lines
2.0 KiB
JavaScript

import { buildLocalizedPath, getLocaleCodes } from '../i18n/index.js';
/**
* Builds a map of locale → URL for hreflang alternate links, suitable for
* Next.js Metadata `alternates.languages`.
*
* Bridges SEO and i18n: for each configured locale that the document has a
* slug in, it produces the locale-aware path (via buildLocalizedPath),
* optionally prefixed with an absolute origin.
*
* @example
* buildHreflangAlternates({
* slugs: { pl: 'o-nas', en: 'about' },
* config,
* baseUrl: 'https://example.com',
* })
* // → { pl: 'https://example.com/pl/o-nas', en: 'https://example.com/en/about' }
*/ export function buildHreflangAlternates({ slugs, config, baseUrl, homeSlug = 'home', prefix }) {
const origin = baseUrl?.replace(/\/$/, '') ?? '';
const alternates = {};
// Single-locale sites have no language alternatives — hreflang describes
// relationships BETWEEN language versions, and there's only one. Emitting
// hreflang (or x-default) here would be wrong, so return empty: the page keeps
// its canonical, but no alternate-language links.
if (config.locales.length === 1) {
return alternates;
}
for (const locale of getLocaleCodes(config)){
const path = buildLocalizedPath({
slugs,
locale,
config,
homeSlug,
prefix
});
if (path) {
alternates[locale] = `${origin}${path}`;
}
}
// x-default: the version Google serves when the user's language/region doesn't
// match any hreflang — and, crucially here, the fallback when the root ('/')
// redirect is ambiguous (Googlebot with no/foreign Accept-Language). Point it
// at the default locale (the primary market) so search shows that version by
// default instead of guessing. Only set when the default locale has a URL.
const defaultLocalePath = alternates[config.defaultLocale];
if (defaultLocalePath) {
alternates['x-default'] = defaultLocalePath;
}
return alternates;
}
//# sourceMappingURL=hreflang.js.map