Skip to content

Commit b4f584c

Browse files
JeanMecheAndrewKushnir
authored andcommitted
fix(core): return StaticProvider for providePlatformInitializer
Returning `EnvironmentProviders` was never correct. fixes angular#64277
1 parent 11e271c commit b4f584c

File tree

3 files changed

+42
-30
lines changed

3 files changed

+42
-30
lines changed

goldens/public-api/core/index.api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1485,7 +1485,7 @@ export function provideEnvironmentInitializer(initializerFn: () => void): Enviro
14851485
export function provideNgReflectAttributes(): EnvironmentProviders;
14861486

14871487
// @public
1488-
export function providePlatformInitializer(initializerFn: () => void): EnvironmentProviders;
1488+
export function providePlatformInitializer(initializerFn: () => void): StaticProvider;
14891489

14901490
// @public
14911491
export type Provider = TypeProvider | ValueProvider | ClassProvider | ConstructorProvider | ExistingProvider | FactoryProvider | any[];

packages/core/src/platform/platform.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -187,20 +187,23 @@ export function createOrReusePlatformInjector(providers: StaticProvider[] = []):
187187
*
188188
* Note that the provided initializer is run in the injection context.
189189
*
190-
* Previously, this was achieved using the `PLATFORM_INITIALIZER` token which is now deprecated.
190+
* @usageNotes
191+
* The platform initializer should be provided during platform creation:
191192
*
192-
* @see {@link PLATFORM_INITIALIZER}
193+
* ```ts
194+
* const platformRef = platformBrowser([ providePlatformInitializer(() => ...) ]);
195+
*
196+
* bootstrapApplication(App, appConfig, { platformRef })
197+
* ```
193198
*
194199
* @publicApi
195200
*/
196-
export function providePlatformInitializer(initializerFn: () => void): EnvironmentProviders {
197-
return makeEnvironmentProviders([
198-
{
199-
provide: PLATFORM_INITIALIZER,
200-
useValue: initializerFn,
201-
multi: true,
202-
},
203-
]);
201+
export function providePlatformInitializer(initializerFn: () => void): StaticProvider {
202+
return {
203+
provide: PLATFORM_INITIALIZER,
204+
useValue: initializerFn,
205+
multi: true,
206+
};
204207
}
205208

206209
function runPlatformInitializers(injector: Injector): void {

packages/platform-browser/test/browser/bootstrap_spec.ts

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ import {ɵLog as Log, inject, TestBed} from '@angular/core/testing';
5151
import {BrowserModule} from '../../index';
5252
import {provideAnimations, provideNoopAnimations} from '../../animations';
5353
import {expect} from '@angular/private/testing/matchers';
54-
import {isNode} from '@angular/private/testing';
54+
import {isNode, withBody} from '@angular/private/testing';
5555

5656
import {bootstrapApplication, platformBrowser} from '../../src/browser';
5757

@@ -920,28 +920,37 @@ describe('providePlatformInitializer', () => {
920920

921921
createPlatformInjector([
922922
{provide: TEST_TOKEN, useValue: 'test'},
923-
providePlatformInitializer(() => {
924-
injectedValue = _inject(TEST_TOKEN);
925-
}),
923+
providePlatformInitializer(() => (injectedValue = _inject(TEST_TOKEN))),
926924
]);
927925

928926
expect(injectedValue).toBe('test');
929927
});
930928

931-
function createPlatformInjector(providers: Array<EnvironmentProviders | Provider>) {
932-
/* TODO: should we change `createOrReusePlatformInjector` type to allow `EnvironmentProviders`?
933-
*/
934-
return createOrReusePlatformInjector(providers as any);
929+
function createPlatformInjector(providers: Array<StaticProvider>) {
930+
return createOrReusePlatformInjector(providers);
935931
}
936-
});
937932

938-
/**
939-
* Typing tests.
940-
*/
941-
@Component({
942-
template: '',
943-
// @ts-expect-error: `providePlatformInitializer()` should not work with Component.providers, as
944-
// it wouldn't be executed anyway.
945-
providers: [providePlatformInitializer(() => {})],
946-
})
947-
class Test {}
933+
it('should bootstrap with platform initializers', async () => {
934+
return withBody('<app></app>', async () => {
935+
@Component({
936+
selector: 'app',
937+
template: '',
938+
})
939+
class App {}
940+
941+
let platformInitializerCalls = 0;
942+
943+
const platformRef = platformBrowser([
944+
providePlatformInitializer(() => {
945+
platformInitializerCalls++;
946+
}),
947+
]);
948+
949+
expect(platformInitializerCalls).toBe(0);
950+
await bootstrapApplication(App, undefined, {platformRef});
951+
expect(platformInitializerCalls).toBe(1);
952+
await bootstrapApplication(App, undefined, {platformRef});
953+
expect(platformInitializerCalls).toBe(1);
954+
});
955+
});
956+
});

0 commit comments

Comments
 (0)