Skip to content

Commit 82d6fbb

Browse files
alan-agius4alxhub
authored andcommitted
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
1 parent 630af63 commit 82d6fbb

File tree

10 files changed

+6
-37
lines changed

10 files changed

+6
-37
lines changed

packages/core/src/application_tokens.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,32 @@
99
import {InjectionToken} from './di/injection_token';
1010

1111
/**
12-
* A [DI token](guide/glossary#di-token "DI token definition") representing a unique string ID, used
12+
* A [DI token](guide/glossary#di-token "DI token definition") representing a string ID, used
1313
* primarily for prefixing application attributes and CSS styles when
1414
* {@link ViewEncapsulation#Emulated ViewEncapsulation.Emulated} is being used.
1515
*
16-
* BY default, the value is randomly generated and assigned to the application by Angular.
17-
* To provide a custom ID value, use a DI provider <!-- TODO: provider --> to configure
18-
* the root {@link Injector} that uses this token.
1916
* The token is needed in cases when multiple applications are bootstrapped on a page
2017
* (for example, using `bootstrapApplication` calls). In this case, ensure that those applications
2118
* have different `APP_ID` value setup. For example:
2219
*
2320
* ```
2421
* bootstrapApplication(ComponentA, {
2522
* providers: [
26-
* {provide: APP_ID, useValue: 'app-a'},
23+
* { provide: APP_ID, useValue: 'app-a' },
2724
* // ... other providers ...
2825
* ]
2926
* });
3027
*
3128
* bootstrapApplication(ComponentB, {
3229
* providers: [
33-
* {provide: APP_ID, useValue: 'app-b'},
30+
* { provide: APP_ID, useValue: 'app-b' },
3431
* // ... other providers ...
3532
* ]
3633
* });
3734
* ```
3835
*
39-
* By default, when there is only 1 application bootstrapped, you don't need to provide the `APP_ID`
40-
* token (the `ng` will be used as an app ID).
36+
* By default, when there is only one application bootstrapped, you don't need to provide the
37+
* `APP_ID` token (the `ng` will be used as an app ID).
4138
*
4239
* @publicApi
4340
*/

packages/core/src/core_private_export.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
*/
88

99
export {ALLOW_MULTIPLE_PLATFORMS as ɵALLOW_MULTIPLE_PLATFORMS, internalCreateApplication as ɵinternalCreateApplication, provideNgZoneChangeDetection as ɵprovideNgZoneChangeDetection} from './application_ref';
10-
export {APP_ID_RANDOM_PROVIDER as ɵAPP_ID_RANDOM_PROVIDER} from './application_tokens';
1110
export {defaultIterableDiffers as ɵdefaultIterableDiffers, defaultKeyValueDiffers as ɵdefaultKeyValueDiffers} from './change_detection/change_detection';
1211
export {Console as ɵConsole} from './console';
1312
export {convertToBitFlags as ɵconvertToBitFlags, setCurrentInjector as ɵsetCurrentInjector} from './di/injector_compatibility';

packages/core/test/bundling/animations/bundle.golden_symbols.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -659,9 +659,6 @@
659659
{
660660
"name": "_query"
661661
},
662-
{
663-
"name": "_randomChar"
664-
},
665662
{
666663
"name": "_retrieveHydrationInfoImpl"
667664
},

packages/core/test/bundling/cyclic_import/bundle.golden_symbols.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -485,9 +485,6 @@
485485
{
486486
"name": "_processI18nInsertBefore"
487487
},
488-
{
489-
"name": "_randomChar"
490-
},
491488
{
492489
"name": "_retrieveHydrationInfoImpl"
493490
},

packages/core/test/bundling/forms_reactive/bundle.golden_symbols.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -668,9 +668,6 @@
668668
{
669669
"name": "_processI18nInsertBefore"
670670
},
671-
{
672-
"name": "_randomChar"
673-
},
674671
{
675672
"name": "_retrieveHydrationInfoImpl"
676673
},

packages/core/test/bundling/forms_template_driven/bundle.golden_symbols.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -656,9 +656,6 @@
656656
{
657657
"name": "_processI18nInsertBefore"
658658
},
659-
{
660-
"name": "_randomChar"
661-
},
662659
{
663660
"name": "_retrieveHydrationInfoImpl"
664661
},

packages/core/test/bundling/router/bundle.golden_symbols.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -860,9 +860,6 @@
860860
{
861861
"name": "_processI18nInsertBefore"
862862
},
863-
{
864-
"name": "_randomChar"
865-
},
866863
{
867864
"name": "_retrieveHydrationInfoImpl"
868865
},

packages/core/test/bundling/standalone_bootstrap/bundle.golden_symbols.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -446,9 +446,6 @@
446446
{
447447
"name": "_processI18nInsertBefore"
448448
},
449-
{
450-
"name": "_randomChar"
451-
},
452449
{
453450
"name": "_retrieveHydrationInfoImpl"
454451
},

packages/core/test/bundling/todo/bundle.golden_symbols.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -578,9 +578,6 @@
578578
{
579579
"name": "_processI18nInsertBefore"
580580
},
581-
{
582-
"name": "_randomChar"
583-
},
584581
{
585582
"name": "_retrieveHydrationInfoImpl"
586583
},

packages/platform-server/src/utils.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
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';
9+
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';
1010
import {BrowserModule} from '@angular/platform-browser';
1111
import {first} from 'rxjs/operators';
1212

@@ -49,12 +49,6 @@ function _render<T>(
4949
bootstrapPromise: Promise<NgModuleRef<T>|ApplicationRef>): Promise<string> {
5050
return bootstrapPromise.then((moduleOrApplicationRef) => {
5151
const environmentInjector = moduleOrApplicationRef.injector;
52-
const transitionId = environmentInjector.get(APP_ID, null);
53-
if (!transitionId) {
54-
throw new Error(
55-
`renderModule[Factory]() requires the use of BrowserModule.withServerTransition() to ensure
56-
the server-rendered app can be properly bootstrapped into a client app.`);
57-
}
5852
const applicationRef: ApplicationRef = moduleOrApplicationRef instanceof ApplicationRef ?
5953
moduleOrApplicationRef :
6054
environmentInjector.get(ApplicationRef);

0 commit comments

Comments
 (0)