I have an app, and a library, two separate repos. My app consumes my library, and my library contains a service which performs a http request.
My app deploys to several environments and I use Angular's environment.ts file to define where the external services live. All apps are served via a gateway and live on the same window.location.origin.
ivy is disabled and aot is enabled.
My Library exposes a forRoot as follows:
declarations: [
ToggleDirective
],
providers: [
ToggleService,
ToggleServiceConfig
],
exports: [
ToggleDirective
]
})
export class ToggleModule {
static forRoot(config: ToggleServiceConfig): ModuleWithProviders<ToggleModule> {
return {
ngModule: ToggleModule,
providers: [
{provide: ToggleServiceConfig, useValue: config }
]
};
}
}
My App's environment.ts looks like this:
export const environment = {
...
togglesUrl: window.location.origin + '/release-toggling',
...
};
In my App's app.module, I configure it like this:
...
const releaseToggleServiceConfig: ToggleServiceConfig = {
togglesUrl: environment.togglesUrl
};
...
@NgModule({
declarations: [AppComponent],
imports: [
CoreModule,
SharedModule,
...
ToggleModule.forRoot(releaseToggleServiceConfig)
],
...
})
export class AppModule {}
if I hard code the window.location.origin (locally, to 'localhost:4200') then it works fine. Obviously, the window doesn't exist at this stage yet. It may be something to do with aot but disabling aot is not an option here unfortunately.
What's the best pattern/practice here to pass the window.location.origin properly. technically, it's not needed until the app does the call to my external service.
I'm interested in your solutions or suggestions. This may be something simple to solve, that many have faced before.