Skip to content

Commit dff70a5

Browse files
committed
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 angular#23475
1 parent 54551bb commit dff70a5

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

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

+4-6
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,6 @@ export function buildWebpackBrowser(
183183
({ config, projectRoot, projectSourceRoot, i18n, target, cacheOptions }) => {
184184
const normalizedOptimization = normalizeOptimization(options.optimization);
185185

186-
const defaultBaseHref = options.baseHref ?? '/';
187-
188186
return runWebpack(config, context, {
189187
webpackFactory: require('webpack') as typeof webpack,
190188
logging:
@@ -319,7 +317,7 @@ export function buildWebpackBrowser(
319317
for (const [locale, outputPath] of outputPaths.entries()) {
320318
try {
321319
const { content, warnings, errors } = await indexHtmlGenerator.process({
322-
baseHref: getLocaleBaseHref(i18n, locale) || defaultBaseHref,
320+
baseHref: getLocaleBaseHref(i18n, locale) ?? options.baseHref,
323321
// i18nLocale is used when Ivy is disabled
324322
lang: locale || undefined,
325323
outputPath,
@@ -363,7 +361,7 @@ export function buildWebpackBrowser(
363361
projectRoot,
364362
context.workspaceRoot,
365363
outputPath,
366-
getLocaleBaseHref(i18n, locale) ?? defaultBaseHref,
364+
getLocaleBaseHref(i18n, locale) ?? options.baseHref ?? '/',
367365
options.ngswConfigPath,
368366
);
369367
} catch (error) {
@@ -393,10 +391,10 @@ export function buildWebpackBrowser(
393391
[...outputPaths.entries()].map(([locale, path]) => ({
394392
locale,
395393
path,
396-
baseHref: getLocaleBaseHref(i18n, locale) ?? defaultBaseHref,
394+
baseHref: getLocaleBaseHref(i18n, locale) ?? options.baseHref,
397395
}))) || {
398396
path: baseOutputPath,
399-
baseHref: defaultBaseHref,
397+
baseHref: options.baseHref,
400398
},
401399
} as BrowserBuilderOutput),
402400
),

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

+21
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`

0 commit comments

Comments
 (0)