Consider the following scenario (Angular v7):
- Load configuration parameters (API server URL and Auth server URL) from an external endpoint (JSON), before the AppModule is loaded
- Pass configuration to AppModule (OAuth2 module)
- Compile the app with AOT
Point 2 is key here, and looks like this:
@NgModule({
  imports: [
    ...
    OAuthModule.forRoot({
      resourceServer: {
        allowedUrls: [API_SERVER_URL], // <== we need to set the value that we loaded from the external endpoint (JSON) here
        sendAccessToken: true
      }
    }),
    ...
  ],
  declarations: [AppComponent],
  bootstrap: [AppComponent]
})
export class AppModule { }
What I've tried to far:
- A solution with APP_INITIALIZER. This doesn't work, as the OAuthModule.forRoot()is triggered before theAPP_INITIALIZERcan download the external configuration JSON.
- Load the config with an async function in the main.tsinto the Angular environment variables, then bootstrap the AppModule. Also doesn't work due to theimport { AppModule } from './app/app.module';statement inmain.ts, which causes the AppModule to load and fireOAuthModule.forRoot()before the external config is loaded (this comment confirms this behavior).
- Load the AppModule dynamically in main.ts, so without theimportstatement on top. This is the StackBlitz example given in that comment. It works, but 1) breaks lazy loadingWARNING in Lazy routes discovery is not enabled.and 2) doesn't work with AOT compiling. It does come very close to what I need.
Curious to hear if someone is aware of another method to get external configuration loaded before the AppModule loads.
StackBlitz for option 3 (Load the AppModule dynamically): https://stackblitz.com/edit/angular-n8hdty
 
     
     
     
    