-
Notifications
You must be signed in to change notification settings - Fork 483
Provide a token from the server side #1646
Comments
Just noticed that when I add universal via CLI I get this in my server.ts: |
Can you encapsulate this in a GitHub repository? |
Here's a plain Angular CLI + Universal repo with 2 tokens: I've added 5 seconds delay to transition from SSR to browser so you can see tokens values on the page. Just run |
I seem to have the same issue ): |
I am also looking for a way to solve this issue :( |
Any news on this? :( |
Any news on this? |
Ok I dug through this for what turned out to be far too long, and here's the answer, though I understand if it's frustrating. Angular DI is structured in a reverse-hierarchal fashion. This means that at the top-level, where you define the platform (platform-server, browser, etc), there is an initial set of providers, and you keep building on this set. When you render a module, you are, in the following order: creating a platform, providing additional providers, then instantiating and rendering your module. At the final stage in this process, you are registering the factory value for your injection token, which is why it "wins" the DI race. When you define a provider at the server.ts level, this is only one level above the platform, and so it almost always "loses". To fix this, you need to instead define any "competing" tokens at the ServerAppModule level. This sits one level above AppModule, and so its providers in this case will "win". In the case of values provided only at server runtime, please make sure that they are singleton, and therefore noncompeting. After all, the browser shouldn't have a fallback for tl;dr move any competing tokens into I'm going to leave this open as a docs ticket, so we can clarify this behavior for other users. |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
🐞 Bug report
What modules are related to this issue?
Is this a regression?
No
Description
I am trying to provide a token on the server side. This token has a factory for default value. I provide it in
server.ts
but app still gets default value from the factory.🔬 Minimal Reproduction
test.ts:
server.ts:
app.component.ts:
I get
default
printed to console.It works if I use just a string. Is there a way to get this to work with tokens? I'm creating a library and I want to provide SSR overrides for its tokens so people would be able to just add them in server.ts. They rely on
req
so I cannot just use them inAppServerModule
🌍 Your Environment
The text was updated successfully, but these errors were encountered: