Skip to content

Commit 4b3a965

Browse files
alan-agius4clydin
authored andcommitted
fix(@angular-devkit/build-angular): baseHref with trailing slash causes server not to be accessible without trailing slash
This commit fixes an issue were when using a `baseHref` with trailing slash, vite dev-server would have been only accessible via a URL with a trailing slash. As vite would redirect to an error page similar to the below; ``` The server is configured with a public base URL of /myapp/ - did you mean to visit [/myapp/](http://localhost:4200/myapp/) instead? ``` Closes: #26618
1 parent c6d70a2 commit 4b3a965

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC 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+
import { executeDevServer } from '../../index';
10+
import { executeOnceAndFetch } from '../execute-fetch';
11+
import { describeServeBuilder } from '../jasmine-helpers';
12+
import { BASE_OPTIONS, DEV_SERVER_BUILDER_INFO } from '../setup';
13+
14+
describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupTarget) => {
15+
describe('Behavior: "buildTarget baseHref"', () => {
16+
beforeEach(async () => {
17+
setupTarget(harness, {
18+
baseHref: '/test/',
19+
});
20+
21+
// Application code is not needed for these tests
22+
await harness.writeFile('src/main.ts', 'console.log("foo");');
23+
});
24+
25+
it('uses the baseHref defined in the "buildTarget" options as the serve path', async () => {
26+
harness.useTarget('serve', {
27+
...BASE_OPTIONS,
28+
});
29+
30+
const { result, response } = await executeOnceAndFetch(harness, '/test/main.js');
31+
32+
expect(result?.success).toBeTrue();
33+
const baseUrl = new URL(`${result?.baseUrl}/`);
34+
expect(baseUrl.pathname).toBe('/test/');
35+
expect(await response?.text()).toContain('console.log');
36+
});
37+
38+
it('serves the application from baseHref location without trailing slash', async () => {
39+
harness.useTarget('serve', {
40+
...BASE_OPTIONS,
41+
});
42+
43+
const { result, response } = await executeOnceAndFetch(harness, '/test');
44+
45+
expect(result?.success).toBeTrue();
46+
expect(await response?.text()).toContain('<script src="main.js" type="module">');
47+
});
48+
});
49+
});

packages/angular_devkit/build_angular/src/builders/dev-server/vite-server.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,11 @@ export async function* serveWithVite(
8686
// Set all packages as external to support Vite's prebundle caching
8787
browserOptions.externalPackages = serverOptions.cacheOptions.enabled;
8888

89-
if (serverOptions.servePath === undefined && browserOptions.baseHref !== undefined) {
90-
serverOptions.servePath = browserOptions.baseHref;
89+
const baseHref = browserOptions.baseHref;
90+
if (serverOptions.servePath === undefined && baseHref !== undefined) {
91+
// Remove trailing slash
92+
serverOptions.servePath =
93+
baseHref[baseHref.length - 1] === '/' ? baseHref.slice(0, -1) : baseHref;
9194
}
9295

9396
// The development server currently only supports a single locale when localizing.
@@ -464,7 +467,7 @@ export async function setupServer(
464467
css: {
465468
devSourcemap: true,
466469
},
467-
// Vite will normalize the `base` option by adding a leading and trailing forward slash.
470+
// Vite will normalize the `base` option by adding a leading slash.
468471
base: serverOptions.servePath,
469472
resolve: {
470473
mainFields: ['es2020', 'browser', 'module', 'main'],

0 commit comments

Comments
 (0)