Skip to content

Commit

Permalink
fix(@angular/ssr): disable component bootstrapping during route extra…
Browse files Browse the repository at this point in the history
…ction

This commit disables component bootstrapping during route extraction to prevent invoking the AppComponent and its lifecycle hooks.

Closes #29085

(cherry picked from commit 10a5b8b)
  • Loading branch information
alan-agius4 committed Dec 11, 2024
1 parent 5ea9ce3 commit 4203efb
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 11 deletions.
23 changes: 22 additions & 1 deletion packages/angular/ssr/src/routes/ng-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,16 @@
*/

import { APP_BASE_HREF, PlatformLocation } from '@angular/common';
import { ApplicationRef, Compiler, Injector, runInInjectionContext, ɵConsole } from '@angular/core';
import {
APP_INITIALIZER,
ApplicationRef,
Compiler,
ComponentRef,
Injector,
inject,
runInInjectionContext,
ɵConsole,
} from '@angular/core';
import { INITIAL_CONFIG, platformServer } from '@angular/platform-server';
import { Route, Router, ɵloadChildren as loadChildrenHelper } from '@angular/router';
import { ServerAssets } from '../assets';
Expand Down Expand Up @@ -417,6 +426,18 @@ export async function getRoutesFromAngularRouterConfig(
// would also necessitate various patches of `@angular/bazel` to support ESM.
useFactory: () => new Console(),
},
{
// We cannot replace `ApplicationRef` with a different provider here due to the dependency injection (DI) hierarchy.
// This code is running at the platform level, where `ApplicationRef` is provided in the root injector.
// As a result, any attempt to replace it will cause the root provider to override the platform provider.
// TODO(alanagius): investigate exporting the app config directly which would help with: https://github.com/angular/angular/issues/59144
provide: APP_INITIALIZER,
multi: true,
useFactory: () => () => {
const appRef = inject(ApplicationRef);
appRef.bootstrap = () => undefined as unknown as ComponentRef<unknown>;
},
},
]);

try {
Expand Down
29 changes: 29 additions & 0 deletions packages/angular/ssr/test/routes/ng-routes_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,4 +466,33 @@ describe('extractRoutesAndCreateRouteTree', () => {
{ route: '/example/home', renderMode: RenderMode.Server },
]);
});

it('should not bootstrap the root component', async () => {
@Component({
standalone: true,
selector: 'app-root',
template: '',
})
class RootComponent {
constructor() {
throw new Error('RootComponent should not be bootstrapped.');
}
}

setAngularAppTestingManifest(
[
{ path: '', component: DummyComponent },
{ path: 'home', component: DummyComponent },
],
[{ path: '**', renderMode: RenderMode.Server }],
undefined,
undefined,
undefined,
RootComponent,
);

const { routeTree, errors } = await extractRoutesAndCreateRouteTree({ url });
expect(errors).toHaveSize(0);
expect(routeTree.toObject()).toHaveSize(2);
});
});
22 changes: 12 additions & 10 deletions packages/angular/ssr/test/testing-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,22 @@
* found in the LICENSE file at https://angular.dev/license
*/

import { Component, provideExperimentalZonelessChangeDetection } from '@angular/core';
import { Component, Type, provideExperimentalZonelessChangeDetection } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import { provideServerRendering } from '@angular/platform-server';
import { RouterOutlet, Routes, provideRouter } from '@angular/router';
import { destroyAngularServerApp } from '../src/app';
import { ServerAsset, setAngularAppManifest } from '../src/manifest';
import { ServerRoute, provideServerRoutesConfig } from '../src/routes/route-config';

@Component({
standalone: true,
selector: 'app-root',
template: '<router-outlet />',
imports: [RouterOutlet],
})
class AppComponent {}

/**
* Configures the Angular application for testing by setting up the Angular app manifest,
* configuring server-side rendering, and bootstrapping the application with the provided routes.
Expand All @@ -26,24 +34,18 @@ import { ServerRoute, provideServerRoutesConfig } from '../src/routes/route-conf
* @param additionalServerAssets - A record of additional server assets to include,
* where the keys are asset paths and the values are asset details.
* @param locale - An optional locale to configure for the application during testing.
* @param rootComponent - The root Angular component to bootstrap the application.
*/
export function setAngularAppTestingManifest(
routes: Routes,
serverRoutes: ServerRoute[],
baseHref = '/',
additionalServerAssets: Record<string, ServerAsset> = {},
locale?: string,
rootComponent: Type<unknown> = AppComponent,
): void {
destroyAngularServerApp();

@Component({
standalone: true,
selector: 'app-root',
template: '<router-outlet />',
imports: [RouterOutlet],
})
class AppComponent {}

setAngularAppManifest({
inlineCriticalCss: false,
baseHref,
Expand Down Expand Up @@ -81,7 +83,7 @@ export function setAngularAppTestingManifest(
},
},
bootstrap: async () => () => {
return bootstrapApplication(AppComponent, {
return bootstrapApplication(rootComponent, {
providers: [
provideServerRendering(),
provideExperimentalZonelessChangeDetection(),
Expand Down

0 comments on commit 4203efb

Please sign in to comment.