Skip to content

Commit c76edb8

Browse files
alan-agius4clydin
authored andcommitted
fix(@angular-devkit/build-angular): don't override base-href in HTML when it's not set in builder
With this change we fix a regression were we set the base-href to `/` when the browser builder `baseHref` option is not set. Closes #23475 (cherry picked from commit 8870111)
1 parent aa8ed53 commit c76edb8

File tree

4 files changed

+28
-10
lines changed

4 files changed

+28
-10
lines changed

packages/angular_devkit/build_angular/src/builders/app-shell/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ async function _renderUniversal(
117117
projectRoot,
118118
root,
119119
outputPath,
120-
baseHref,
120+
baseHref ?? '/',
121121
browserOptions.ngswConfigPath,
122122
);
123123
}

packages/angular_devkit/build_angular/src/builders/browser/index.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export type BrowserBuilderOutput = BuilderOutput & {
7878
outputs: {
7979
locale?: string;
8080
path: string;
81-
baseHref: string;
81+
baseHref?: string;
8282
}[];
8383
};
8484

@@ -182,8 +182,6 @@ export function buildWebpackBrowser(
182182
({ config, projectRoot, projectSourceRoot, i18n, target, cacheOptions }) => {
183183
const normalizedOptimization = normalizeOptimization(options.optimization);
184184

185-
const defaultBaseHref = options.baseHref ?? '/';
186-
187185
return runWebpack(config, context, {
188186
webpackFactory: require('webpack') as typeof webpack,
189187
logging:
@@ -317,7 +315,7 @@ export function buildWebpackBrowser(
317315
for (const [locale, outputPath] of outputPaths.entries()) {
318316
try {
319317
const { content, warnings, errors } = await indexHtmlGenerator.process({
320-
baseHref: getLocaleBaseHref(i18n, locale) || defaultBaseHref,
318+
baseHref: getLocaleBaseHref(i18n, locale) ?? options.baseHref,
321319
// i18nLocale is used when Ivy is disabled
322320
lang: locale || undefined,
323321
outputPath,
@@ -361,7 +359,7 @@ export function buildWebpackBrowser(
361359
projectRoot,
362360
context.workspaceRoot,
363361
outputPath,
364-
getLocaleBaseHref(i18n, locale) ?? defaultBaseHref,
362+
getLocaleBaseHref(i18n, locale) ?? options.baseHref ?? '/',
365363
options.ngswConfigPath,
366364
);
367365
} catch (error) {
@@ -391,10 +389,10 @@ export function buildWebpackBrowser(
391389
[...outputPaths.entries()].map(([locale, path]) => ({
392390
locale,
393391
path,
394-
baseHref: getLocaleBaseHref(i18n, locale) ?? defaultBaseHref,
392+
baseHref: getLocaleBaseHref(i18n, locale) ?? options.baseHref,
395393
}))) || {
396394
path: baseOutputPath,
397-
baseHref: defaultBaseHref,
395+
baseHref: options.baseHref,
398396
},
399397
} as BrowserBuilderOutput),
400398
),

packages/angular_devkit/build_angular/src/builders/browser/specs/base-href_spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,27 @@ describe('Browser Builder base href', () => {
3939
await run.stop();
4040
});
4141

42+
it('should not override base href in HTML when option is not set', async () => {
43+
host.writeMultipleFiles({
44+
'src/index.html': `
45+
<html>
46+
<head><base href="."></head>
47+
<body></body>
48+
</html>
49+
`,
50+
});
51+
52+
const run = await architect.scheduleTarget(targetSpec);
53+
const output = (await run.result) as BrowserBuilderOutput;
54+
55+
expect(output.success).toBeTrue();
56+
const fileName = join(normalize(output.outputs[0].path), 'index.html');
57+
const content = virtualFs.fileBufferToString(await host.read(fileName).toPromise());
58+
expect(content).toContain(`<base href=".">`);
59+
60+
await run.stop();
61+
});
62+
4263
it('should insert base href in the the correct position', async () => {
4364
host.writeMultipleFiles({
4465
'src/index.html': tags.oneLine`

packages/angular_devkit/build_angular/src/testing/test-utils.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ export async function browserBuild(
8383
};
8484
}
8585

86-
const [{ path, baseHref }] = output.outputs;
87-
expect(baseHref).toBeTruthy();
86+
const [{ path }] = output.outputs;
8887
expect(path).toBeTruthy();
8988
const outputPath = normalize(path);
9089

0 commit comments

Comments
 (0)