Skip to content

Commit a05ee30

Browse files
alan-agius4dgp1130
authored andcommitted
fix(@angular-devkit/build-angular): baseHref with protocol and localize option
`posix.join` will dedupe double forward slashes resulting in incorrect protocol. Closes: #17029 (cherry picked from commit 4e65705)
1 parent 1f43b5c commit a05ee30

File tree

5 files changed

+60
-8
lines changed

5 files changed

+60
-8
lines changed

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import {
4949
normalizeAssetPatterns,
5050
normalizeOptimization,
5151
normalizeSourceMaps,
52+
urlJoin,
5253
} from '../utils';
5354
import { BundleActionExecutor } from '../utils/action-executor';
5455
import { findCachePath } from '../utils/cache-path';
@@ -695,11 +696,9 @@ export function buildWebpackBrowser(
695696
for (const [locale, outputPath] of outputPaths.entries()) {
696697
let localeBaseHref;
697698
if (i18n.locales[locale] && i18n.locales[locale].baseHref !== '') {
698-
localeBaseHref = path.posix.join(
699+
localeBaseHref = urlJoin(
699700
options.baseHref || '',
700-
i18n.locales[locale].baseHref === undefined
701-
? `/${locale}/`
702-
: i18n.locales[locale].baseHref,
701+
i18n.locales[locale].baseHref ?? `/${locale}/`,
703702
);
704703
}
705704

@@ -726,11 +725,9 @@ export function buildWebpackBrowser(
726725
for (const [locale, outputPath] of outputPaths.entries()) {
727726
let localeBaseHref;
728727
if (i18n.locales[locale] && i18n.locales[locale].baseHref !== '') {
729-
localeBaseHref = path.posix.join(
728+
localeBaseHref = urlJoin(
730729
options.baseHref || '',
731-
i18n.locales[locale].baseHref === undefined
732-
? `/${locale}/`
733-
: i18n.locales[locale].baseHref,
730+
i18n.locales[locale].baseHref ?? `/${locale}/`,
734731
);
735732
}
736733

packages/angular_devkit/build_angular/src/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ export * from './normalize-asset-patterns';
1515
export * from './normalize-source-maps';
1616
export * from './normalize-optimization';
1717
export * from './normalize-builder-schema';
18+
export * from './url';
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
10+
export function urlJoin(...parts: string[]): string {
11+
const [p, ...rest] = parts;
12+
13+
// Remove trailing slash from first part
14+
// Join all parts with `/`
15+
// Dedupe double slashes from path names
16+
return p.replace(/\/$/, '') + ('/' + rest.join('/')).replace(/\/\/+/g, '/');
17+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
import { urlJoin } from './url';
9+
10+
describe('urlJoin', () => {
11+
it('should work with absolute url with trailing slash', () => {
12+
expect(urlJoin('http://foo.com/', '/one/')).toBe('http://foo.com/one/');
13+
});
14+
15+
it('should work with absolute url without trailing slash', () => {
16+
expect(urlJoin('http://foo.com', '/one')).toBe('http://foo.com/one');
17+
});
18+
19+
it('should work with absolute url without slashes', () => {
20+
expect(urlJoin('http://foo.com', 'one', 'two')).toBe('http://foo.com/one/two');
21+
});
22+
23+
it('should work with relative url without slashes', () => {
24+
expect(urlJoin('one', 'two', 'three')).toBe('one/two/three');
25+
});
26+
27+
it('should keep trailing slash if empty path is provided', () => {
28+
expect(urlJoin('one/', '')).toBe('one/');
29+
});
30+
});

tests/legacy-cli/e2e/tests/i18n/ivy-localize-basehref.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,11 @@ export default async function() {
100100
server.close();
101101
}
102102
}
103+
104+
// Test absolute base href.
105+
await ng('build', '--base-href', 'http://www.domain.com/');
106+
for (const { lang, outputPath } of langTranslations) {
107+
// Verify the HTML base HREF attribute is present
108+
await expectFileToMatch(`${outputPath}/index.html`, `href="http://www.domain.com${baseHrefs[lang] || '/'}"`);
109+
}
103110
}

0 commit comments

Comments
 (0)