Question presented to candidate:
"We ship one build to staging and production, and the API URL must come from a config.json fetched at startup. How would you make sure that config is loaded before any component or service uses it? Mention APP_INITIALIZER."
What a strong answer should cover:
- An app initializer is a function Angular runs during bootstrap; if it returns a Promise or Observable, bootstrap waits for it before creating the root component.
APP_INITIALIZERis a multi-provider token for such functions; it has been deprecated since v19 in favour ofprovideAppInitializer(fn), which takes the function directly and runs it in an injection context (soinject(HttpClient)works).- The pattern: a
ConfigServicewith aload()method called by the initializer, storing the result; everything else injectsConfigService(or a token derived from it). - Multiple initializers run concurrently, not in sequence; if one depends on another, chain them inside one initializer.
- A rejected Promise fails the bootstrap: the root component is never created, so handle errors (fallback config or a visible error).
- Trade-off: it delays first render; keep it small, and consider build-time environment files when config does not need to vary per deployment.
Clarifying questions expected:
- "Does the config differ per deployment of the same build, or per build?"
- "What should happen if config.json fails to load?"
Code / implementation expected: Yes β provideAppInitializer with a config service, and the legacy APP_INITIALIZER form for comparison.