Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

417
Views
Handling environment variables in SSR with Angular Universal

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 {}
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

• 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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!