Skip to content

Commit

Permalink
fix(@angular/build): replace deprecation of i18n.baseHref with a wa…
Browse files Browse the repository at this point in the history
…rning

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 #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

(cherry picked from commit 8535c11)
  • Loading branch information
alan-agius4 committed Jan 21, 2025
1 parent 5165265 commit d50788c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
2 changes: 1 addition & 1 deletion packages/angular/build/src/builders/application/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ export async function normalizeOptions(
const i18nOptions: I18nOptions & {
duplicateTranslationBehavior?: I18NTranslation;
missingTranslationBehavior?: I18NTranslation;
} = createI18nOptions(projectMetadata, options.localize, context.logger);
} = createI18nOptions(projectMetadata, options.localize, context.logger, !!options.ssr);
i18nOptions.duplicateTranslationBehavior = options.i18nDuplicateTranslation;
i18nOptions.missingTranslationBehavior = options.i18nMissingTranslation;
if (options.forceI18nFlatOutput) {
Expand Down
30 changes: 18 additions & 12 deletions packages/angular/build/src/utils/i18n-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export function createI18nOptions(
logger?: {
warn(message: string): void;
},
ssrEnabled?: boolean,
): I18nOptions {
const { i18n: metadata = {} } = projectMetadata;

Expand Down Expand Up @@ -110,12 +111,14 @@ export function createI18nOptions(

if (metadata.sourceLocale.baseHref !== undefined) {
ensureString(metadata.sourceLocale.baseHref, 'i18n.sourceLocale.baseHref');
logger?.warn(
`The 'baseHref' field under 'i18n.sourceLocale' is deprecated and will be removed in future versions. ` +
`Please use 'subPath' instead.\nNote: 'subPath' defines the URL segment for the locale, acting ` +
`as both the HTML base HREF and the directory name for output.\nBy default, ` +
`if not specified, 'subPath' uses the locale code.`,
);
if (ssrEnabled) {
logger?.warn(
`'baseHref' in 'i18n.sourceLocale' may lead to undefined behavior when used with SSR. ` +
`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.`,
);
}

rawSourceLocaleBaseHref = metadata.sourceLocale.baseHref;
}
Expand Down Expand Up @@ -156,12 +159,15 @@ export function createI18nOptions(

if ('baseHref' in options) {
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. ` +
`Please use 'subPath' instead.\nNote: 'subPath' defines the URL segment for the locale, acting ` +
`as both the HTML base HREF and the directory name for output.\nBy default, ` +
`if not specified, 'subPath' uses the locale code.`,
);

if (ssrEnabled) {
logger?.warn(
`'baseHref' in 'i18n.locales.${locale}' may lead to undefined behavior when used with SSR. ` +
`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;
}

Expand Down
2 changes: 0 additions & 2 deletions packages/angular/cli/lib/config/workspace-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,6 @@
},
"baseHref": {
"type": "string",
"deprecated": true,
"description": "Specifies the HTML base HREF for the locale. Defaults to the locale code if not provided."
},
"subPath": {
Expand Down Expand Up @@ -356,7 +355,6 @@
},
"baseHref": {
"type": "string",
"deprecated": true,
"description": "Specifies the HTML base HREF for the locale. Defaults to the locale code if not provided."
},
"subPath": {
Expand Down

0 comments on commit d50788c

Please sign in to comment.