fix(@angular/build): replace deprecation of i18n.baseHref with a warning

In certain scenarios, users build applications with the same `baseHref` when using i18n, primarily for deploying localized applications across multiple domains. To address this, we are removing the deprecation of `i18n.baseHref` and will revisit potential options as part of https://github.com/angular/angular-cli/issues/29111

Instead of deprecating `i18n.baseHref`, we now issue a warning when it is used with SSR, as this may lead to undefined behavior.

Closes #29396
This commit is contained in:
Alan Agius 2025-01-21 13:53:26 +00:00 committed by Alan Agius
parent e7413d3ae1
commit 8535c11182
3 changed files with 19 additions and 15 deletions

View File

@ -174,7 +174,7 @@ export async function normalizeOptions(
const i18nOptions: I18nOptions & { const i18nOptions: I18nOptions & {
duplicateTranslationBehavior?: I18NTranslation; duplicateTranslationBehavior?: I18NTranslation;
missingTranslationBehavior?: I18NTranslation; missingTranslationBehavior?: I18NTranslation;
} = createI18nOptions(projectMetadata, options.localize, context.logger); } = createI18nOptions(projectMetadata, options.localize, context.logger, !!options.ssr);
i18nOptions.duplicateTranslationBehavior = options.i18nDuplicateTranslation; i18nOptions.duplicateTranslationBehavior = options.i18nDuplicateTranslation;
i18nOptions.missingTranslationBehavior = options.i18nMissingTranslation; i18nOptions.missingTranslationBehavior = options.i18nMissingTranslation;
if (options.forceI18nFlatOutput) { if (options.forceI18nFlatOutput) {

View File

@ -80,6 +80,7 @@ export function createI18nOptions(
logger?: { logger?: {
warn(message: string): void; warn(message: string): void;
}, },
ssrEnabled?: boolean,
): I18nOptions { ): I18nOptions {
const { i18n: metadata = {} } = projectMetadata; const { i18n: metadata = {} } = projectMetadata;
@ -110,12 +111,14 @@ export function createI18nOptions(
if (metadata.sourceLocale.baseHref !== undefined) { if (metadata.sourceLocale.baseHref !== undefined) {
ensureString(metadata.sourceLocale.baseHref, 'i18n.sourceLocale.baseHref'); ensureString(metadata.sourceLocale.baseHref, 'i18n.sourceLocale.baseHref');
logger?.warn( if (ssrEnabled) {
`The 'baseHref' field under 'i18n.sourceLocale' is deprecated and will be removed in future versions. ` + logger?.warn(
`Please use 'subPath' instead.\nNote: 'subPath' defines the URL segment for the locale, acting ` + `'baseHref' in 'i18n.sourceLocale' may lead to undefined behavior when used with SSR. ` +
`as both the HTML base HREF and the directory name for output.\nBy default, ` + `Consider using 'subPath' instead.\n\n` +
`if not specified, 'subPath' uses the locale code.`, `Note: 'subPath' specifies the URL segment for the locale, serving as both the HTML base HREF ` +
); `and the output directory name.\nBy default, if not explicitly set, 'subPath' defaults to the locale code.`,
);
}
rawSourceLocaleBaseHref = metadata.sourceLocale.baseHref; rawSourceLocaleBaseHref = metadata.sourceLocale.baseHref;
} }
@ -156,12 +159,15 @@ export function createI18nOptions(
if ('baseHref' in options) { if ('baseHref' in options) {
ensureString(options.baseHref, `i18n.locales.${locale}.baseHref`); ensureString(options.baseHref, `i18n.locales.${locale}.baseHref`);
logger?.warn(
`The 'baseHref' field under 'i18n.locales.${locale}' is deprecated and will be removed in future versions. ` + if (ssrEnabled) {
`Please use 'subPath' instead.\nNote: 'subPath' defines the URL segment for the locale, acting ` + logger?.warn(
`as both the HTML base HREF and the directory name for output.\nBy default, ` + `'baseHref' in 'i18n.locales.${locale}' may lead to undefined behavior when used with SSR. ` +
`if not specified, 'subPath' uses the locale code.`, `Consider using 'subPath' instead.\n\n` +
); `Note: 'subPath' specifies the URL segment for the locale, serving as both the HTML base HREF ` +
`and the output directory name.\nBy default, if not explicitly set, 'subPath' defaults to the locale code.`,
);
}
baseHref = options.baseHref; baseHref = options.baseHref;
} }

View File

@ -284,7 +284,6 @@
}, },
"baseHref": { "baseHref": {
"type": "string", "type": "string",
"deprecated": true,
"description": "Specifies the HTML base HREF for the locale. Defaults to the locale code if not provided." "description": "Specifies the HTML base HREF for the locale. Defaults to the locale code if not provided."
}, },
"subPath": { "subPath": {
@ -356,7 +355,6 @@
}, },
"baseHref": { "baseHref": {
"type": "string", "type": "string",
"deprecated": true,
"description": "Specifies the HTML base HREF for the locale. Defaults to the locale code if not provided." "description": "Specifies the HTML base HREF for the locale. Defaults to the locale code if not provided."
}, },
"subPath": { "subPath": {