mirror of
https://github.com/angular/angular
synced 2026-05-24 09:28:37 +00:00
refactor(core): add ENABLE_ROOT_COMPONENT_BOOTSTRAP token (#59205)
Introduced the `ENABLE_ROOT_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 `false`, 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 `true`, the default behavior of bootstrapping the root component(s) during initialization is maintained. Context: https://github.com/angular/angular-cli/issues/29085 PR Close #59205
This commit is contained in:
parent
4d74f86139
commit
ba9bbd07bf
13 changed files with 71 additions and 2 deletions
|
|
@ -105,6 +105,7 @@ export {
|
|||
} from './metadata/resource_loading';
|
||||
export {PendingTasksInternal as ɵPendingTasksInternal} from './pending_tasks';
|
||||
export {ALLOW_MULTIPLE_PLATFORMS as ɵALLOW_MULTIPLE_PLATFORMS} from './platform/platform';
|
||||
export {ENABLE_ROOT_COMPONENT_BOOTSTRAP as ɵENABLE_ROOT_COMPONENT_BOOTSTRAP} from './platform/bootstrap';
|
||||
export {ReflectionCapabilities as ɵReflectionCapabilities} from './reflection/reflection_capabilities';
|
||||
export {AnimationRendererType as ɵAnimationRendererType} from './render/api';
|
||||
export {
|
||||
|
|
|
|||
|
|
@ -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';
|
||||
|
|
@ -22,10 +22,34 @@ 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 to control root component bootstrap behavior.
|
||||
*
|
||||
* This token is primarily used in Angular's server-side rendering (SSR) scenarios,
|
||||
* particularly by the `@angular/ssr` package, to manage whether the root component
|
||||
* should be bootstrapped during the application initialization process.
|
||||
*
|
||||
* ## Purpose:
|
||||
* During SSR route extraction, setting this token to `false` prevents Angular from
|
||||
* bootstrapping the root component. This avoids unnecessary component rendering,
|
||||
* enabling route extraction without requiring additional APIs or triggering
|
||||
* component logic.
|
||||
*
|
||||
* ## Behavior:
|
||||
* - **`false`**: Prevents the root component from being bootstrapped.
|
||||
* - **`true`** (default): Proceeds with the normal root component bootstrap process.
|
||||
*
|
||||
* This mechanism ensures SSR can efficiently separate route extraction logic
|
||||
* from component rendering.
|
||||
*/
|
||||
export const ENABLE_ROOT_COMPONENT_BOOTSTRAP = new InjectionToken<boolean>(
|
||||
ngDevMode ? 'ENABLE_ROOT_COMPONENT_BOOTSTRAP' : '',
|
||||
);
|
||||
|
||||
export interface BootstrapConfig {
|
||||
platformInjector: Injector;
|
||||
}
|
||||
|
|
@ -125,6 +149,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 enableRootComponentBoostrap = envInjector.get(ENABLE_ROOT_COMPONENT_BOOTSTRAP, true);
|
||||
if (!enableRootComponentBoostrap) {
|
||||
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();
|
||||
|
|
|
|||
|
|
@ -191,6 +191,9 @@
|
|||
{
|
||||
"name": "EMPTY_SUBSCRIPTION"
|
||||
},
|
||||
{
|
||||
"name": "ENABLE_ROOT_COMPONENT_BOOTSTRAP"
|
||||
},
|
||||
{
|
||||
"name": "ENTER_TOKEN_REGEX"
|
||||
},
|
||||
|
|
|
|||
|
|
@ -218,6 +218,9 @@
|
|||
{
|
||||
"name": "EMPTY_SUBSCRIPTION"
|
||||
},
|
||||
{
|
||||
"name": "ENABLE_ROOT_COMPONENT_BOOTSTRAP"
|
||||
},
|
||||
{
|
||||
"name": "ENTER_TOKEN_REGEX"
|
||||
},
|
||||
|
|
|
|||
|
|
@ -134,6 +134,9 @@
|
|||
{
|
||||
"name": "EMPTY_SUBSCRIPTION"
|
||||
},
|
||||
{
|
||||
"name": "ENABLE_ROOT_COMPONENT_BOOTSTRAP"
|
||||
},
|
||||
{
|
||||
"name": "ENVIRONMENT_INITIALIZER"
|
||||
},
|
||||
|
|
|
|||
|
|
@ -182,6 +182,9 @@
|
|||
{
|
||||
"name": "EMPTY_SUBSCRIPTION"
|
||||
},
|
||||
{
|
||||
"name": "ENABLE_ROOT_COMPONENT_BOOTSTRAP"
|
||||
},
|
||||
{
|
||||
"name": "ENVIRONMENT_INITIALIZER"
|
||||
},
|
||||
|
|
|
|||
|
|
@ -188,6 +188,9 @@
|
|||
{
|
||||
"name": "EMPTY_SUBSCRIPTION"
|
||||
},
|
||||
{
|
||||
"name": "ENABLE_ROOT_COMPONENT_BOOTSTRAP"
|
||||
},
|
||||
{
|
||||
"name": "ENVIRONMENT_INITIALIZER"
|
||||
},
|
||||
|
|
|
|||
|
|
@ -191,6 +191,9 @@
|
|||
{
|
||||
"name": "EMPTY_SUBSCRIPTION"
|
||||
},
|
||||
{
|
||||
"name": "ENABLE_ROOT_COMPONENT_BOOTSTRAP"
|
||||
},
|
||||
{
|
||||
"name": "ENVIRONMENT_INITIALIZER"
|
||||
},
|
||||
|
|
|
|||
|
|
@ -86,6 +86,9 @@
|
|||
{
|
||||
"name": "EMPTY_SUBSCRIPTION"
|
||||
},
|
||||
{
|
||||
"name": "ENABLE_ROOT_COMPONENT_BOOTSTRAP"
|
||||
},
|
||||
{
|
||||
"name": "ENVIRONMENT_INITIALIZER"
|
||||
},
|
||||
|
|
|
|||
|
|
@ -122,6 +122,9 @@
|
|||
{
|
||||
"name": "EMPTY_SUBSCRIPTION"
|
||||
},
|
||||
{
|
||||
"name": "ENABLE_ROOT_COMPONENT_BOOTSTRAP"
|
||||
},
|
||||
{
|
||||
"name": "ENVIRONMENT_INITIALIZER"
|
||||
},
|
||||
|
|
|
|||
|
|
@ -197,6 +197,9 @@
|
|||
{
|
||||
"name": "EMPTY_SUBSCRIPTION"
|
||||
},
|
||||
{
|
||||
"name": "ENABLE_ROOT_COMPONENT_BOOTSTRAP"
|
||||
},
|
||||
{
|
||||
"name": "ENVIRONMENT_INITIALIZER"
|
||||
},
|
||||
|
|
|
|||
|
|
@ -113,6 +113,9 @@
|
|||
{
|
||||
"name": "EMPTY_SUBSCRIPTION"
|
||||
},
|
||||
{
|
||||
"name": "ENABLE_ROOT_COMPONENT_BOOTSTRAP"
|
||||
},
|
||||
{
|
||||
"name": "ENVIRONMENT_INITIALIZER"
|
||||
},
|
||||
|
|
|
|||
|
|
@ -137,6 +137,9 @@
|
|||
{
|
||||
"name": "EMPTY_SUBSCRIPTION"
|
||||
},
|
||||
{
|
||||
"name": "ENABLE_ROOT_COMPONENT_BOOTSTRAP"
|
||||
},
|
||||
{
|
||||
"name": "ENVIRONMENT_INITIALIZER"
|
||||
},
|
||||
|
|
|
|||
Loading…
Reference in a new issue