From 37d632106d088b069ac2d076d1039d4efbf9d9fb Mon Sep 17 00:00:00 2001 From: Emmanuel Gautier Date: Mon, 2 Oct 2023 22:40:27 +0200 Subject: [PATCH] fix(@galactiks/explorer): use default lang configured --- .../src/core/content/metadata/alternates.mts | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/packages/explorer/src/core/content/metadata/alternates.mts b/packages/explorer/src/core/content/metadata/alternates.mts index b925db5f..9b5af927 100644 --- a/packages/explorer/src/core/content/metadata/alternates.mts +++ b/packages/explorer/src/core/content/metadata/alternates.mts @@ -1,3 +1,4 @@ +import { getDefaultLanguage } from '@galactiks/config'; import type { Content, MetadataHeaders } from '../index.mjs'; import type { ContentlayerDocumentWithURL } from '../urls.mjs'; @@ -19,7 +20,7 @@ export const alternatesHeaderBuilder = ( return (document: Content): MetadataHeaders['alternates'] => { let translations: Array = []; - let defaultTranslation: ContentlayerDocumentWithURL | Content; + let defaultTranslation: ContentlayerDocumentWithURL | Content | undefined; if (document.translationOfWork) { const translationOfWorkId = document.translationOfWork['@id']; @@ -42,15 +43,19 @@ export const alternatesHeaderBuilder = ( document.identifier, document.url ); - defaultTranslation = document; } - translations = translations.concat(defaultTranslation); - - if (translations.length === 1) { + if (translations.length === 0) { translations = selectPagesByIdentifier(document.identifier); } + if (!defaultTranslation) { + const configDefaultLanguage = getDefaultLanguage(); + defaultTranslation = + translations.find((t) => t.inLanguage === configDefaultLanguage) || + document; + } + return translations .filter((_t) => _t.inLanguage) .map((_t) => ({ href: _t.url, hreflang: _t.inLanguage as string }))