Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,14 @@ async function renderPages(
try {
const renderingPromises: Promise<void>[] = [];
const appShellRouteWithLeadingSlash = appShellRoute && addLeadingSlash(appShellRoute);
const baseHrefWithLeadingSlash = addLeadingSlash(baseHref);
const baseHrefPathnameWithLeadingSlash = new URL(baseHref, 'http://localhost').pathname;

for (const { route, redirectTo } of serializableRouteTreeNode) {
// Remove the base href from the file output path.
const routeWithoutBaseHref = addTrailingSlash(route).startsWith(baseHrefWithLeadingSlash)
? addLeadingSlash(route.slice(baseHrefWithLeadingSlash.length))
const routeWithoutBaseHref = addTrailingSlash(route).startsWith(
baseHrefPathnameWithLeadingSlash,
)
? addLeadingSlash(route.slice(baseHrefPathnameWithLeadingSlash.length))
: route;

const outPath = posix.join(removeLeadingSlash(routeWithoutBaseHref), 'index.html');
Expand Down
7 changes: 2 additions & 5 deletions packages/angular/ssr/src/routes/ng-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -615,13 +615,10 @@ export async function getRoutesFromAngularRouterConfig(

const errors: string[] = [];

let baseHref =
const rawBaseHref =
injector.get(APP_BASE_HREF, null, { optional: true }) ??
injector.get(PlatformLocation).getBaseHrefFromDOM();

if (baseHref.startsWith('./')) {
baseHref = baseHref.slice(2);
}
const { pathname: baseHref } = new URL(rawBaseHref, 'http://localhost');

const compiler = injector.get(Compiler);
const serverRoutesConfig = injector.get(SERVER_ROUTES_CONFIG, null, { optional: true });
Expand Down
19 changes: 19 additions & 0 deletions packages/angular/ssr/test/routes/ng-routes_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,25 @@ describe('extractRoutesAndCreateRouteTree', () => {
]);
});

it('handles a baseHref starting with a protocol', async () => {
setAngularAppTestingManifest(
[{ path: 'home', component: DummyComponent }],
[{ path: '**', renderMode: RenderMode.Server }],
/** baseHref*/ 'http://foo.com/example/',
);

const { routeTree, errors } = await extractRoutesAndCreateRouteTree({
url,
invokeGetPrerenderParams: true,
includePrerenderFallbackRoutes: true,
});

expect(errors).toHaveSize(0);
expect(routeTree.toObject()).toEqual([
{ route: '/example/home', renderMode: RenderMode.Server },
]);
});

it('should not bootstrap the root component', async () => {
@Component({
standalone: true,
Expand Down