Skip to content

Commit 8bd2b26

Browse files
committed
fix(@angular/ssr): handle baseHref that start with ./
Updated function to support handling `baseHref` starting with './' path correctly. (cherry picked from commit 8c534da)
1 parent 74461da commit 8bd2b26

File tree

4 files changed

+30
-7
lines changed

4 files changed

+30
-7
lines changed

packages/angular/ssr/src/routes/ng-routes.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -421,12 +421,15 @@ export async function getRoutesFromAngularRouterConfig(
421421
const routesResults: RouteTreeNodeMetadata[] = [];
422422
const errors: string[] = [];
423423

424-
const baseHref =
424+
let baseHref =
425425
injector.get(APP_BASE_HREF, null, { optional: true }) ??
426426
injector.get(PlatformLocation).getBaseHrefFromDOM();
427427

428-
const compiler = injector.get(Compiler);
428+
if (baseHref.startsWith('./')) {
429+
baseHref = baseHref.slice(2);
430+
}
429431

432+
const compiler = injector.get(Compiler);
430433
const serverRoutesConfig = injector.get(SERVER_ROUTES_CONFIG, null, { optional: true });
431434
let serverConfigRouteTree: RouteTree<ServerConfigRouteTreeAdditionalMetadata> | undefined;
432435

packages/angular/ssr/test/app-engine_spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ describe('AngularAppEngine', () => {
4242
setAngularAppTestingManifest(
4343
[{ path: 'home', component: HomeComponent }],
4444
[{ path: '**', renderMode: RenderMode.Server }],
45-
locale,
45+
'/' + locale,
4646
);
4747

4848
return {

packages/angular/ssr/test/routes/ng-routes_spec.ts

+20
Original file line numberDiff line numberDiff line change
@@ -355,4 +355,24 @@ describe('extractRoutesAndCreateRouteTree', () => {
355355
{ route: '/', renderMode: RenderMode.Server, status: 201 },
356356
]);
357357
});
358+
359+
it(`handles a baseHref starting with a "./" path`, async () => {
360+
setAngularAppTestingManifest(
361+
[{ path: 'home', component: DummyComponent }],
362+
[{ path: '**', renderMode: RenderMode.Server }],
363+
/** baseHref*/ './example',
364+
);
365+
366+
const { routeTree, errors } = await extractRoutesAndCreateRouteTree(
367+
url,
368+
/** manifest */ undefined,
369+
/** invokeGetPrerenderParams */ true,
370+
/** includePrerenderFallbackRoutes */ true,
371+
);
372+
373+
expect(errors).toHaveSize(0);
374+
expect(routeTree.toObject()).toEqual([
375+
{ route: '/example/home', renderMode: RenderMode.Server },
376+
]);
377+
});
358378
});

packages/angular/ssr/test/testing-utils.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ import { ServerRoute, provideServerRoutesConfig } from '../src/routes/route-conf
2121
*
2222
* @param routes - An array of route definitions to be used by the Angular Router.
2323
* @param serverRoutes - An array of ServerRoute definitions to be used for server-side rendering.
24-
* @param [baseHref=''] - An optional base href to be used in the HTML template.
24+
* @param [baseHref='/'] - An optional base href to be used in the HTML template.
2525
*/
2626
export function setAngularAppTestingManifest(
2727
routes: Routes,
2828
serverRoutes: ServerRoute[],
29-
baseHref = '',
29+
baseHref = '/',
3030
additionalServerAssets: Record<string, ServerAsset> = {},
3131
): void {
3232
setAngularAppManifest({
@@ -40,7 +40,7 @@ export function setAngularAppTestingManifest(
4040
text: async () => `<html>
4141
<head>
4242
<title>SSR page</title>
43-
<base href="/${baseHref}" />
43+
<base href="${baseHref}" />
4444
</head>
4545
<body>
4646
<app-root></app-root>
@@ -55,7 +55,7 @@ export function setAngularAppTestingManifest(
5555
`<html>
5656
<head>
5757
<title>CSR page</title>
58-
<base href="/${baseHref}" />
58+
<base href="${baseHref}" />
5959
</head>
6060
<body>
6161
<app-root></app-root>

0 commit comments

Comments
 (0)