Skip to content

Commit

Permalink
fix(@angular-devkit/build-angular): baseHref with protocol and locali…
Browse files Browse the repository at this point in the history
…ze option

`posix.join` will dedupe double forward slashes resulting in incorrect protocol.

Closes: #17029
(cherry picked from commit 4e65705)
  • Loading branch information
alan-agius4 authored and dgp1130 committed Feb 24, 2020
1 parent 1f43b5c commit a05ee30
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 8 deletions.
13 changes: 5 additions & 8 deletions packages/angular_devkit/build_angular/src/browser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import {
normalizeAssetPatterns,
normalizeOptimization,
normalizeSourceMaps,
urlJoin,
} from '../utils';
import { BundleActionExecutor } from '../utils/action-executor';
import { findCachePath } from '../utils/cache-path';
Expand Down Expand Up @@ -695,11 +696,9 @@ export function buildWebpackBrowser(
for (const [locale, outputPath] of outputPaths.entries()) {
let localeBaseHref;
if (i18n.locales[locale] && i18n.locales[locale].baseHref !== '') {
localeBaseHref = path.posix.join(
localeBaseHref = urlJoin(
options.baseHref || '',
i18n.locales[locale].baseHref === undefined
? `/${locale}/`
: i18n.locales[locale].baseHref,
i18n.locales[locale].baseHref ?? `/${locale}/`,
);
}

Expand All @@ -726,11 +725,9 @@ export function buildWebpackBrowser(
for (const [locale, outputPath] of outputPaths.entries()) {
let localeBaseHref;
if (i18n.locales[locale] && i18n.locales[locale].baseHref !== '') {
localeBaseHref = path.posix.join(
localeBaseHref = urlJoin(
options.baseHref || '',
i18n.locales[locale].baseHref === undefined
? `/${locale}/`
: i18n.locales[locale].baseHref,
i18n.locales[locale].baseHref ?? `/${locale}/`,
);
}

Expand Down
1 change: 1 addition & 0 deletions packages/angular_devkit/build_angular/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ export * from './normalize-asset-patterns';
export * from './normalize-source-maps';
export * from './normalize-optimization';
export * from './normalize-builder-schema';
export * from './url';
17 changes: 17 additions & 0 deletions packages/angular_devkit/build_angular/src/utils/url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/


export function urlJoin(...parts: string[]): string {
const [p, ...rest] = parts;

// Remove trailing slash from first part
// Join all parts with `/`
// Dedupe double slashes from path names
return p.replace(/\/$/, '') + ('/' + rest.join('/')).replace(/\/\/+/g, '/');
}
30 changes: 30 additions & 0 deletions packages/angular_devkit/build_angular/src/utils/url_spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { urlJoin } from './url';

describe('urlJoin', () => {
it('should work with absolute url with trailing slash', () => {
expect(urlJoin('http://foo.com/', '/one/')).toBe('http://foo.com/one/');
});

it('should work with absolute url without trailing slash', () => {
expect(urlJoin('http://foo.com', '/one')).toBe('http://foo.com/one');
});

it('should work with absolute url without slashes', () => {
expect(urlJoin('http://foo.com', 'one', 'two')).toBe('http://foo.com/one/two');
});

it('should work with relative url without slashes', () => {
expect(urlJoin('one', 'two', 'three')).toBe('one/two/three');
});

it('should keep trailing slash if empty path is provided', () => {
expect(urlJoin('one/', '')).toBe('one/');
});
});
7 changes: 7 additions & 0 deletions tests/legacy-cli/e2e/tests/i18n/ivy-localize-basehref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,11 @@ export default async function() {
server.close();
}
}

// Test absolute base href.
await ng('build', '--base-href', 'http://www.domain.com/');
for (const { lang, outputPath } of langTranslations) {
// Verify the HTML base HREF attribute is present
await expectFileToMatch(`${outputPath}/index.html`, `href="http://www.domain.com${baseHrefs[lang] || '/'}"`);
}
}

0 comments on commit a05ee30

Please sign in to comment.