From 82d6fbb109491607bd2e4feaa35c3dace79e4576 Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Tue, 14 Mar 2023 10:15:17 +0000 Subject: [PATCH] refactor(core): generate a static application ID (#49422) Prior to this change, a random application ID was generated each time which forced users using server rendering to provide an application ID themselves. This was needed to handle rare cases when multiple Angular applications are rendered on the same page. With this change the application ID is no longer generated randomly and instead it is hard coded. BREAKING CHANGE: The `APP_ID` token value is no longer randomly generated. If you are bootstrapping multiple application on the same page you will need to set to provide the `APP_ID` yourself. ```ts bootstrapApplication(ComponentA, { providers: [ { provide: APP_ID, useValue: 'app-a' }, // ... other providers ... ] }); ``` PR Close #49422 --- packages/core/src/application_tokens.ts | 13 +++++-------- packages/core/src/core_private_export.ts | 1 - .../bundling/animations/bundle.golden_symbols.json | 3 --- .../cyclic_import/bundle.golden_symbols.json | 3 --- .../forms_reactive/bundle.golden_symbols.json | 3 --- .../bundle.golden_symbols.json | 3 --- .../test/bundling/router/bundle.golden_symbols.json | 3 --- .../standalone_bootstrap/bundle.golden_symbols.json | 3 --- .../test/bundling/todo/bundle.golden_symbols.json | 3 --- packages/platform-server/src/utils.ts | 8 +------- 10 files changed, 6 insertions(+), 37 deletions(-) diff --git a/packages/core/src/application_tokens.ts b/packages/core/src/application_tokens.ts index f5ea477069f..3ca99210cb5 100644 --- a/packages/core/src/application_tokens.ts +++ b/packages/core/src/application_tokens.ts @@ -9,13 +9,10 @@ import {InjectionToken} from './di/injection_token'; /** - * A [DI token](guide/glossary#di-token "DI token definition") representing a unique string ID, used + * A [DI token](guide/glossary#di-token "DI token definition") representing a string ID, used * primarily for prefixing application attributes and CSS styles when * {@link ViewEncapsulation#Emulated ViewEncapsulation.Emulated} is being used. * - * BY default, the value is randomly generated and assigned to the application by Angular. - * To provide a custom ID value, use a DI provider to configure - * the root {@link Injector} that uses this token. * The token is needed in cases when multiple applications are bootstrapped on a page * (for example, using `bootstrapApplication` calls). In this case, ensure that those applications * have different `APP_ID` value setup. For example: @@ -23,21 +20,21 @@ import {InjectionToken} from './di/injection_token'; * ``` * bootstrapApplication(ComponentA, { * providers: [ - * {provide: APP_ID, useValue: 'app-a'}, + * { provide: APP_ID, useValue: 'app-a' }, * // ... other providers ... * ] * }); * * bootstrapApplication(ComponentB, { * providers: [ - * {provide: APP_ID, useValue: 'app-b'}, + * { provide: APP_ID, useValue: 'app-b' }, * // ... other providers ... * ] * }); * ``` * - * By default, when there is only 1 application bootstrapped, you don't need to provide the `APP_ID` - * token (the `ng` will be used as an app ID). + * By default, when there is only one application bootstrapped, you don't need to provide the + * `APP_ID` token (the `ng` will be used as an app ID). * * @publicApi */ diff --git a/packages/core/src/core_private_export.ts b/packages/core/src/core_private_export.ts index 7b1f317b140..9fd2a146ff3 100644 --- a/packages/core/src/core_private_export.ts +++ b/packages/core/src/core_private_export.ts @@ -7,7 +7,6 @@ */ export {ALLOW_MULTIPLE_PLATFORMS as ɵALLOW_MULTIPLE_PLATFORMS, internalCreateApplication as ɵinternalCreateApplication, provideNgZoneChangeDetection as ɵprovideNgZoneChangeDetection} from './application_ref'; -export {APP_ID_RANDOM_PROVIDER as ɵAPP_ID_RANDOM_PROVIDER} from './application_tokens'; export {defaultIterableDiffers as ɵdefaultIterableDiffers, defaultKeyValueDiffers as ɵdefaultKeyValueDiffers} from './change_detection/change_detection'; export {Console as ɵConsole} from './console'; export {convertToBitFlags as ɵconvertToBitFlags, setCurrentInjector as ɵsetCurrentInjector} from './di/injector_compatibility'; diff --git a/packages/core/test/bundling/animations/bundle.golden_symbols.json b/packages/core/test/bundling/animations/bundle.golden_symbols.json index e85b9eec638..be9ad9e7725 100644 --- a/packages/core/test/bundling/animations/bundle.golden_symbols.json +++ b/packages/core/test/bundling/animations/bundle.golden_symbols.json @@ -659,9 +659,6 @@ { "name": "_query" }, - { - "name": "_randomChar" - }, { "name": "_retrieveHydrationInfoImpl" }, diff --git a/packages/core/test/bundling/cyclic_import/bundle.golden_symbols.json b/packages/core/test/bundling/cyclic_import/bundle.golden_symbols.json index 8a363c460e8..a963669630f 100644 --- a/packages/core/test/bundling/cyclic_import/bundle.golden_symbols.json +++ b/packages/core/test/bundling/cyclic_import/bundle.golden_symbols.json @@ -485,9 +485,6 @@ { "name": "_processI18nInsertBefore" }, - { - "name": "_randomChar" - }, { "name": "_retrieveHydrationInfoImpl" }, diff --git a/packages/core/test/bundling/forms_reactive/bundle.golden_symbols.json b/packages/core/test/bundling/forms_reactive/bundle.golden_symbols.json index 49712354a6a..c6581836f5c 100644 --- a/packages/core/test/bundling/forms_reactive/bundle.golden_symbols.json +++ b/packages/core/test/bundling/forms_reactive/bundle.golden_symbols.json @@ -668,9 +668,6 @@ { "name": "_processI18nInsertBefore" }, - { - "name": "_randomChar" - }, { "name": "_retrieveHydrationInfoImpl" }, diff --git a/packages/core/test/bundling/forms_template_driven/bundle.golden_symbols.json b/packages/core/test/bundling/forms_template_driven/bundle.golden_symbols.json index d44a1152d49..272b472ca8c 100644 --- a/packages/core/test/bundling/forms_template_driven/bundle.golden_symbols.json +++ b/packages/core/test/bundling/forms_template_driven/bundle.golden_symbols.json @@ -656,9 +656,6 @@ { "name": "_processI18nInsertBefore" }, - { - "name": "_randomChar" - }, { "name": "_retrieveHydrationInfoImpl" }, diff --git a/packages/core/test/bundling/router/bundle.golden_symbols.json b/packages/core/test/bundling/router/bundle.golden_symbols.json index f4f28639397..1b3ffbafe00 100644 --- a/packages/core/test/bundling/router/bundle.golden_symbols.json +++ b/packages/core/test/bundling/router/bundle.golden_symbols.json @@ -860,9 +860,6 @@ { "name": "_processI18nInsertBefore" }, - { - "name": "_randomChar" - }, { "name": "_retrieveHydrationInfoImpl" }, diff --git a/packages/core/test/bundling/standalone_bootstrap/bundle.golden_symbols.json b/packages/core/test/bundling/standalone_bootstrap/bundle.golden_symbols.json index 2512937c5d9..f0a76be275a 100644 --- a/packages/core/test/bundling/standalone_bootstrap/bundle.golden_symbols.json +++ b/packages/core/test/bundling/standalone_bootstrap/bundle.golden_symbols.json @@ -446,9 +446,6 @@ { "name": "_processI18nInsertBefore" }, - { - "name": "_randomChar" - }, { "name": "_retrieveHydrationInfoImpl" }, diff --git a/packages/core/test/bundling/todo/bundle.golden_symbols.json b/packages/core/test/bundling/todo/bundle.golden_symbols.json index f87f419fe28..410f0dfe369 100644 --- a/packages/core/test/bundling/todo/bundle.golden_symbols.json +++ b/packages/core/test/bundling/todo/bundle.golden_symbols.json @@ -578,9 +578,6 @@ { "name": "_processI18nInsertBefore" }, - { - "name": "_randomChar" - }, { "name": "_retrieveHydrationInfoImpl" }, diff --git a/packages/platform-server/src/utils.ts b/packages/platform-server/src/utils.ts index 0e45f266030..fd17864e489 100644 --- a/packages/platform-server/src/utils.ts +++ b/packages/platform-server/src/utils.ts @@ -6,7 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import {APP_ID, ApplicationRef, EnvironmentProviders, importProvidersFrom, InjectionToken, NgModuleRef, PlatformRef, Provider, Renderer2, StaticProvider, Type, ɵannotateForHydration as annotateForHydration, ɵgetComponentDef as getComponentDef, ɵinternalCreateApplication as internalCreateApplication, ɵIS_HYDRATION_FEATURE_ENABLED as IS_HYDRATION_FEATURE_ENABLED, ɵisPromise} from '@angular/core'; +import {ApplicationRef, EnvironmentProviders, importProvidersFrom, InjectionToken, NgModuleRef, PlatformRef, Provider, Renderer2, StaticProvider, Type, ɵannotateForHydration as annotateForHydration, ɵgetComponentDef as getComponentDef, ɵinternalCreateApplication as internalCreateApplication, ɵIS_HYDRATION_FEATURE_ENABLED as IS_HYDRATION_FEATURE_ENABLED, ɵisPromise} from '@angular/core'; import {BrowserModule} from '@angular/platform-browser'; import {first} from 'rxjs/operators'; @@ -49,12 +49,6 @@ function _render( bootstrapPromise: Promise|ApplicationRef>): Promise { return bootstrapPromise.then((moduleOrApplicationRef) => { const environmentInjector = moduleOrApplicationRef.injector; - const transitionId = environmentInjector.get(APP_ID, null); - if (!transitionId) { - throw new Error( - `renderModule[Factory]() requires the use of BrowserModule.withServerTransition() to ensure -the server-rendered app can be properly bootstrapped into a client app.`); - } const applicationRef: ApplicationRef = moduleOrApplicationRef instanceof ApplicationRef ? moduleOrApplicationRef : environmentInjector.get(ApplicationRef);