Skip to content

Instantly share code, notes, and snippets.

@dsebastien
Last active March 28, 2021 18:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dsebastien/023f3b416553bbfd4e651290ffca2c43 to your computer and use it in GitHub Desktop.
Save dsebastien/023f3b416553bbfd4e651290ffca2c43 to your computer and use it in GitHub Desktop.
import { enableProdMode } from "@angular/core";
import { disableDebugTools } from "@angular/platform-browser";
import { environment as defaultEnvironment } from "@app/web-env/environment";
import { RUNTIME_CONFIG_KEY } from "@app/web-core";
import { runtimeConfigLoader$ } from "@app/web-env/runtime-config-loader.fn";
import { catchError, concatMap, delay, map, retryWhen, take } from "rxjs/operators";
import { from, of, throwError } from "rxjs";
import { platformBrowserDynamic } from "@angular/platform-browser-dynamic";
...
/**
* Load configuration file at runtime, before the Angular application starts.
* Tt tries to load an environment.json file from the assets which are served statically, and store the resulting runtime environment configuration
* WARNING: The AppModule is loaded lazily to ensure that it only gets loaded and evaluated ONCE the configuration has been loaded. This is necessary because of the way we configure the Auth0 module.
*/
runtimeConfigLoader$
.pipe(
/**
* Retry a few times. This call is super important
*/
retryWhen((errors) =>
errors.pipe(
delay(500),
take(20),
map(() => {
throwError("Failed too many times to retrieve the runtime configuration");
}),
),
),
take(1),
concatMap((loadedRuntimeEnvironmentConfiguration) => {
/**
* We take the default environment as starting point. Then, whatever is defined in the file overrides the defaults.
*/
if (loadedRuntimeEnvironmentConfiguration) {
const runtimeEnvironmentConfiguration = {
...defaultEnvironment,
...loadedRuntimeEnvironmentConfiguration,
};
console.log("Overriding application config with the one loaded for the environment");
sessionStorage.setItem(RUNTIME_CONFIG_KEY, JSON.stringify(runtimeEnvironmentConfiguration, null, 2));
}
console.log(`Starting app. Version: ${defaultEnvironment.version}`);
return from(import("./app/app.module")).pipe(
concatMap((mod) => {
platformBrowserDynamic().bootstrapModule(mod.AppModule);
return of(void 0);
}),
);
}),
catchError((err) => {
...
}),
)
.subscribe();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment