4

I'm applying SSR to a pre-existing Angular project. In the previous Client Side Rendering CSR implementation, I used the global variable window to handle ENV VARS for one build - multiple deployments CI/CD purposes following this article.

I tried using a custom service to pass the env vars to app.modules.ts using the APP_INITIALIZER with no success because the usage of envirement with forRoot() will be called way before my service. Also, webpack can't help in this situation because of one build - multiple deployments requirment.

As the example bellow I tried to inject the EnvironmentService in a module but it seems that data isn't fetched yet from the file as metioned in the article above.

import { EnvironmentService } from './environments/EnvironmentService';

let envService: EnvironmentService;

@NgModule({
    declarations: [...],
    imports: [ServerLogModule.forRoot(envService.ENV_VARS)],
    providers: [...]
})
export class AppModule {}
2
  • 1
    The question is not clear enough, is calling ServerLogModule.forRoot() mandatory? I mean is it possible to change the code in the service, or is the service being imported from a third party library? Can you add the code of you ServerLogModule? Commented Nov 21, 2021 at 19:56
  • I use to have our own modules using forRoot and I solve them there. The problem now is with the many third-party modules such as ServerLogModule in the example above Commented Nov 21, 2021 at 20:11

1 Answer 1

3
+100

• Passing an environment variable to forRoot() method from another service in app.modules.ts's providers won't be possible since you won't be able to retrieve any data before the method is called.

In order for this to work, it's better to use webpack to create at build time an environment.ts file with the correct data, and use it in app.modules.ts this may help

• The article mentioned in your question -as you said- would work very well for a Client Side Rendering situation, but using the same approach for SSR may require lots of changes for some/many architectural choices you may have already made.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.