Skip to content

Commit

Permalink
refactor(core): add DISABLE_COMPONENT_BOOTSTRAP token
Browse files Browse the repository at this point in the history
Introduced the `DISABLE_COMPONENT_BOOTSTRAP` token to control the bootstrapping of components during application initialization. This token is utilized by the Angular CLI in the `@angular/ssr` package, particularly during server-side rendering (SSR) when extracting routes.

When set to `true`, this token prevents the root component from being bootstrapped during SSR's route extraction phase, which is crucial for efficiently extracting routes without triggering component initialization. This mechanism separates the concerns of route extraction and component bootstrapping during SSR rendering, optimizing performance.

If not provided or set to `false`, the default behavior of bootstrapping the root component(s) during initialization is maintained.

Context: angular/angular-cli#29085
  • Loading branch information
alan-agius4 committed Dec 10, 2024
1 parent 46f00f9 commit 4f2c59c
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/core/src/core_private_export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export {
PendingTasksInternal as ɵPendingTasks, // TODO(atscott): remove once there is a release with PendingTasksInternal so adev can be updated
PendingTasksInternal as ɵPendingTasksInternal,
} from './pending_tasks';
export {DISABLE_COMPONENT_BOOTSTRAP as ɵDISABLE_COMPONENT_BOOTSTRAP} from './platform/bootstrap';
export {ALLOW_MULTIPLE_PLATFORMS as ɵALLOW_MULTIPLE_PLATFORMS} from './platform/platform';
export {ReflectionCapabilities as ɵReflectionCapabilities} from './reflection/reflection_capabilities';
export {AnimationRendererType as ɵAnimationRendererType} from './render/api';
Expand Down
34 changes: 32 additions & 2 deletions packages/core/src/platform/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import {Subscription} from 'rxjs';

import {PROVIDED_NG_ZONE} from '../change_detection/scheduling/ng_zone_scheduling';
import {EnvironmentInjector, R3Injector} from '../di/r3_injector';
import {R3Injector} from '../di/r3_injector';
import {ErrorHandler} from '../error_handler';
import {RuntimeError, RuntimeErrorCode} from '../errors';
import {DEFAULT_LOCALE_ID} from '../i18n/localization';
Expand All @@ -22,10 +22,29 @@ import {NgZone} from '../zone/ng_zone';
import {ApplicationInitStatus} from '../application/application_init';
import {_callAndReportToErrorHandler, ApplicationRef, remove} from '../application/application_ref';
import {PROVIDED_ZONELESS} from '../change_detection/scheduling/zoneless_scheduling';
import {Injector} from '../di';
import {InjectionToken, Injector} from '../di';
import {InternalNgModuleRef, NgModuleRef} from '../linker/ng_module_factory';
import {stringify} from '../util/stringify';

/**
* InjectionToken used to disable the bootstrap of components.
*
* This token is used by the Angular CLI in the `@angular/ssr` package to control
* whether components should be bootstrapped during the application initialization process.
* Specifically, it is used during route extraction when server-side rendering is enabled.
*
* By setting this token to `true`, Angular prevents the root component from being bootstrapped
* and avoids invoking it during the route extraction phase, which is necessary for SSR
* to extract routes without requiring new APIs to be exposed. This mechanism helps separate
* the logic for route extraction and component bootstrapping during SSR rendering.
*
* When not provided or set to `false`, Angular will continue with the default behavior
* of bootstrapping the root component(s) during initialization.
*/
export const DISABLE_COMPONENT_BOOTSTRAP = new InjectionToken<boolean>(
ngDevMode ? 'DISABLE_ROOT_COMPONENT_BOOTSTRAP' : '',
);

export interface BootstrapConfig {
platformInjector: Injector;
}
Expand Down Expand Up @@ -125,6 +144,17 @@ export function bootstrap<M>(
// If the `LOCALE_ID` provider is defined at bootstrap then we set the value for ivy
const localeId = envInjector.get(LOCALE_ID, DEFAULT_LOCALE_ID);
setLocaleId(localeId || DEFAULT_LOCALE_ID);

const disableComponentBootstrap = envInjector.get(DISABLE_COMPONENT_BOOTSTRAP, false);
if (disableComponentBootstrap) {
if (isApplicationBootstrapConfig(config)) {
return envInjector.get(ApplicationRef);
}

config.allPlatformModules.push(config.moduleRef);
return config.moduleRef;
}

if (typeof ngDevMode === 'undefined' || ngDevMode) {
const imagePerformanceService = envInjector.get(ImagePerformanceWarning);
imagePerformanceService.start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@
{
"name": "DIMENSIONAL_PROP_SET"
},
{
"name": "DISABLE_COMPONENT_BOOTSTRAP"
},
{
"name": "DOCUMENT"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@
{
"name": "DIMENSIONAL_PROP_SET"
},
{
"name": "DISABLE_COMPONENT_BOOTSTRAP"
},
{
"name": "DOCUMENT"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@
{
"name": "DEFAULT_APP_ID"
},
{
"name": "DISABLE_COMPONENT_BOOTSTRAP"
},
{
"name": "DOCUMENT"
},
Expand Down
3 changes: 3 additions & 0 deletions packages/core/test/bundling/defer/bundle.golden_symbols.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@
{
"name": "DEHYDRATED_VIEWS"
},
{
"name": "DISABLE_COMPONENT_BOOTSTRAP"
},
{
"name": "DI_DECORATOR_FLAG"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@
{
"name": "DEFAULT_VALUE_ACCESSOR"
},
{
"name": "DISABLE_COMPONENT_BOOTSTRAP"
},
{
"name": "DOCUMENT"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@
{
"name": "DEFAULT_VALUE_ACCESSOR"
},
{
"name": "DISABLE_COMPONENT_BOOTSTRAP"
},
{
"name": "DOCUMENT"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@
{
"name": "ConsumerObserver"
},
{
"name": "DISABLE_COMPONENT_BOOTSTRAP"
},
{
"name": "DOCUMENT2"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@
{
"name": "DEFAULT_APP_ID"
},
{
"name": "DISABLE_COMPONENT_BOOTSTRAP"
},
{
"name": "DOCUMENT"
},
Expand Down
3 changes: 3 additions & 0 deletions packages/core/test/bundling/router/bundle.golden_symbols.json
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@
{
"name": "DEFAULT_SERIALIZER"
},
{
"name": "DISABLE_COMPONENT_BOOTSTRAP"
},
{
"name": "DOCUMENT"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@
{
"name": "DEFAULT_APP_ID"
},
{
"name": "DISABLE_COMPONENT_BOOTSTRAP"
},
{
"name": "DOCUMENT"
},
Expand Down
3 changes: 3 additions & 0 deletions packages/core/test/bundling/todo/bundle.golden_symbols.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@
{
"name": "DEFAULT_APP_ID"
},
{
"name": "DISABLE_COMPONENT_BOOTSTRAP"
},
{
"name": "DOCUMENT"
},
Expand Down

0 comments on commit 4f2c59c

Please sign in to comment.