Skip to content

Commit 8870111

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
1 parent 7a24609 commit 8870111

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ async function _renderUniversal(
118118
projectRoot,
119119
root,
120120
outputPath,
121-
baseHref,
121+
baseHref ?? '/',
122122
browserOptions.ngswConfigPath,
123123
);
124124
}

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

+5-7
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export type BrowserBuilderOutput = BuilderOutput & {
7979
outputs: {
8080
locale?: string;
8181
path: string;
82-
baseHref: string;
82+
baseHref?: string;
8383
}[];
8484
};
8585

@@ -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`

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

+1-2
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)