Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling platformBrowserDynamic a second time silently ignores extraProviders #25358

Open
Erikvv opened this issue Aug 7, 2018 · 7 comments
Open
Assignees
Labels
area: core Issues related to the framework runtime core: bootstrap core: di freq1: low hotlist: error messages P5 The team acknowledges the request but does not plan to address it, it remains open for discussion type: confusing
Milestone

Comments

@Erikvv
Copy link

Erikvv commented Aug 7, 2018

I'm submitting a...


[x] Bug report

Current behavior

platformBrowserDynamic().bootstrapModule(ModuleOne);

platformBrowserDynamic([
    {
        provide: 'myValue',
        useValue: 'something',
    }
]).bootstrapModule(ModuleTwo)

Both applications are bootstrapped but 'myValue' is not available to ModuleTwo.

You wouldn't write code like this in a single file but it may happen that independent code both calls platformBrowserDynamic().

Expected behavior

Either

  • Make the extraProviders available to the bootstrapped module
  • Or throw an error saying you cannot call this function twice
  • Or throw an error that calling it twice must have the same arguments.

What is the motivation / use case for changing the behavior?

The issue is hard to debug. Existing code can be broken by newly added independent code if that new code calls platformBrowserDynamic() earlier.

Environment

Angular version: 6.1.1
Browser: not browser-specific

@kara kara added the area: core Issues related to the framework runtime label Aug 7, 2018
@ngbot ngbot bot added this to the needsTriage milestone Aug 7, 2018
@Erikvv
Copy link
Author

Erikvv commented Aug 7, 2018

Source is at

return (extraProviders: StaticProvider[] = []) => {

I'm willing to contribute, I'm just not sure what the desired behavior is.

@mhevery
Copy link
Contributor

mhevery commented Aug 9, 2018

You can only have one platform injector. So in a way this works as intended. But an error would be useful. PR with error would be accepted.

@trotyl
Copy link
Contributor

trotyl commented Aug 10, 2018

@mhevery The real problem should be why is ALLOW_MULTIPLE_PLATFORMS be false for browser. I don't see any technical problem in allowing that.

Moreover, one can have different Realms in the same page, even global variables are not shared in between.

@mhevery
Copy link
Contributor

mhevery commented Sep 7, 2018

The idea was that the platform would hold singletons which have resources which are singletons on the browser such as Location URL. You can only have one of those.

@trotyl
Copy link
Contributor

trotyl commented Sep 8, 2018

@mhevery But user can still override PlatformLocation token with its own implementation, so it could totally be a normal NgModule provider in BrowserModule. For example, one may have an internal application in a <div> (without <iframe>) for non-SPA page.

The only valid platform internal provider I can see is PLATFORM_INITIALIZER, which cannot be made NgModule level for its execution timing.

@mhevery
Copy link
Contributor

mhevery commented Sep 10, 2018

As I said "idea was" but I think there are simpler ways of getting there.

Problem is that if you bootstrap two apps in a single page (rare but possible) than each app would bring its own Location you would have an issue. Hence Platfrom.

going forward we are thinking of ways to remove it....

@Erikvv
Copy link
Author

Erikvv commented Feb 13, 2019

I did find a hacky workaround for the problem. Just swap the injector:

const extraProviders = [
    {
        provide: 'myValue',
        useValue: 'something',
    }
]

const platform = platformBrowserDynamic()

;(platform as any)._injector = Injector.create({
    providers: extraProviders,
    parent: platform.injector,
})

platform.bootstrapModule(MyModule)

Unfortunately this breaks AOT compilation because of angular/angular-cli#8880

@ngbot ngbot bot modified the milestones: Backlog, needsTriage May 28, 2020
@kara kara added the triage #1 label May 28, 2020
@atscott atscott added the P5 The team acknowledges the request but does not plan to address it, it remains open for discussion label Oct 19, 2020
@ngbot ngbot bot modified the milestones: needsTriage, Backlog Oct 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: core Issues related to the framework runtime core: bootstrap core: di freq1: low hotlist: error messages P5 The team acknowledges the request but does not plan to address it, it remains open for discussion type: confusing
Projects
None yet
Development

No branches or pull requests

7 participants