Skip to content

Commit c26ea16

Browse files
alan-agius4dgp1130
authored andcommitted
fix(@angular/ssr): properly handle baseHref with protocol
Enhances handling of `baseHref` when it includes a full URL with a protocol. Closes #29590 (cherry picked from commit 833dc98)
1 parent 8890a5f commit c26ea16

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

packages/angular/build/src/utils/server-rendering/prerender.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -228,12 +228,14 @@ async function renderPages(
228228
try {
229229
const renderingPromises: Promise<void>[] = [];
230230
const appShellRouteWithLeadingSlash = appShellRoute && addLeadingSlash(appShellRoute);
231-
const baseHrefWithLeadingSlash = addLeadingSlash(baseHref);
231+
const baseHrefPathnameWithLeadingSlash = new URL(baseHref, 'http://localhost').pathname;
232232

233233
for (const { route, redirectTo } of serializableRouteTreeNode) {
234234
// Remove the base href from the file output path.
235-
const routeWithoutBaseHref = addTrailingSlash(route).startsWith(baseHrefWithLeadingSlash)
236-
? addLeadingSlash(route.slice(baseHrefWithLeadingSlash.length))
235+
const routeWithoutBaseHref = addTrailingSlash(route).startsWith(
236+
baseHrefPathnameWithLeadingSlash,
237+
)
238+
? addLeadingSlash(route.slice(baseHrefPathnameWithLeadingSlash.length))
237239
: route;
238240

239241
const outPath = posix.join(removeLeadingSlash(routeWithoutBaseHref), 'index.html');

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

+2-5
Original file line numberDiff line numberDiff line change
@@ -569,13 +569,10 @@ export async function getRoutesFromAngularRouterConfig(
569569

570570
const errors: string[] = [];
571571

572-
let baseHref =
572+
const rawBaseHref =
573573
injector.get(APP_BASE_HREF, null, { optional: true }) ??
574574
injector.get(PlatformLocation).getBaseHrefFromDOM();
575-
576-
if (baseHref.startsWith('./')) {
577-
baseHref = baseHref.slice(2);
578-
}
575+
const { pathname: baseHref } = new URL(rawBaseHref, 'http://localhost');
579576

580577
const compiler = injector.get(Compiler);
581578
const serverRoutesConfig = injector.get(SERVER_ROUTES_CONFIG, null, { optional: true });

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

+19
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,25 @@ describe('extractRoutesAndCreateRouteTree', () => {
510510
]);
511511
});
512512

513+
it('handles a baseHref starting with a protocol', async () => {
514+
setAngularAppTestingManifest(
515+
[{ path: 'home', component: DummyComponent }],
516+
[{ path: '**', renderMode: RenderMode.Server }],
517+
/** baseHref*/ 'http://foo.com/example/',
518+
);
519+
520+
const { routeTree, errors } = await extractRoutesAndCreateRouteTree({
521+
url,
522+
invokeGetPrerenderParams: true,
523+
includePrerenderFallbackRoutes: true,
524+
});
525+
526+
expect(errors).toHaveSize(0);
527+
expect(routeTree.toObject()).toEqual([
528+
{ route: '/example/home', renderMode: RenderMode.Server },
529+
]);
530+
});
531+
513532
it('should not bootstrap the root component', async () => {
514533
@Component({
515534
standalone: true,

0 commit comments

Comments
 (0)