Skip to content
Excelsior Technologies
Our Blogsangular
4 min read

Exploring Angular’s App Initializer: Use Cases, Mechanics and Limitations

A guide to Angular's APP_INITIALIZER token for running tasks before an app starts. We cover its mechanics, common use cases like config loading, and the performance costs of delaying the bootstrap process.

Written by
Excelsior Technologies
angulartypescriptweb developmentperformance

Exploring Angular’s App Initializer: Practical Use Cases, Mechanics, and Limitations

Angular's APP_INITIALIZER is a dependency injection token that lets you provide one or more functions that must run before the application bootstrap process completes. This means you can block the application from rendering until certain asynchronous tasks, like fetching configuration data, have finished. It is a specific tool with a significant trade-off: every task you run here directly increases the time to first paint, leaving your user with a blank screen for longer.

This article details the mechanics of APP_INITIALIZER, outlines the scenarios where its use is justified and explains the performance costs you must consider.

How APP_INITIALIZER works

The token works by registering one or more provider functions in your AppModule or another root module. These functions must return either a Promise that resolves or an Observable that completes. Angular collects all functions provided under this token and executes them. The application's bootstrap process is paused until every promise resolves and every observable completes.

If any function's promise rejects or an observable errors, the entire application bootstrap fails. This makes robust error handling within your initializer functions a critical requirement, not an optional extra.

The provider registration in your module looks like this:

``typescript { provide: APP_INITIALIZER, useFactory: (myService: MyService) => () => myService.load(), deps: [MyService], multi: true, } ``

The multi: true property is essential. It allows multiple providers to contribute to the same APP_INITIALIZER token, letting you register several independent startup tasks across your application's modules.

Justified use cases for delaying bootstrap

Using APP_INITIALIZER is a decision to prioritise a flicker-free initial render over the fastest possible time-to-interactive. Here are scenarios where that trade-off often makes sense.

Preloading essential configuration

If your application cannot function without certain runtime settings, such as API endpoints or external service keys, fetching them here is appropriate. This ensures the configuration is available to all services from the moment they are instantiated, preventing runtime errors caused by missing values.

Verifying user authentication

For applications where no content is visible to unauthenticated users, APP_INITIALIZER can be used to check for a valid session token. If the token is valid, the app proceeds. If not, you can redirect to a login page before any application components are rendered, securing the entire application from the start.

Loading initial localisation data

To avoid a 'flash of untranslated content' (FOUT), you can use the initializer to fetch the default or user-preferred language file. This ensures the first view the user sees is fully translated. The cost is a slower initial load, but it provides a better experience for global applications.

Fetching critical user-specific settings

Some user preferences, such as a high-contrast theme for accessibility or a pre-selected 'dark mode', must be applied before any UI is painted to prevent a jarring visual shift. Loading these settings during initialization allows the application to render correctly from the very first frame.

The performance trade-off and what to avoid

The primary limitation of APP_INITIALIZER is performance. Any task, however small, contributes to the blocking period before your app becomes visible. It should not be used as a general-purpose pre-loading mechanism.

Avoid running tasks that are not strictly necessary for the initial application view. For example, fetching data for a secondary page, loading non-critical user preferences or initialising a service that is only used later are all poor candidates for APP_INITIALIZER. These tasks can almost always be deferred until after the app has loaded, or handled by a route resolver for a specific view.

A slow initializer can be caused by a slow network request, a large data payload or a backend service with high latency. Monitor the duration of your initialization tasks. If they consistently take more than a few hundred milliseconds, you should look for an alternative approach.

Handling errors during initialisation

An unhandled error in an APP_INITIALIZER function will stop your application from loading entirely. You must implement fallback strategies. For example, if a request for remote configuration fails, your initializer function should catch the error and return a default, local configuration object instead of letting the promise reject.

In the case of a failed authentication check, you might redirect the user to a login page. The key is to ensure your useFactory function always returns a resolved promise or a completed observable, even when things go wrong, so that the user sees a functional state rather than a blank page.

Hire dedicated developers page on excelsiortechnologies.com

We have built this

How we work at Excelsior

Our dedicated developers work your hours, follow your process and report to you. You see matching profiles within 24 to 48 hours, and you interview them before anyone joins your team.

Hire dedicated developers →

Want this for your business?

Ask ET on our website, or book a free 30-minute call with a project lead, not a salesperson.

Ask AI about Excelsior

Don’t take our word for it.Ask an AI.

Ask ET, our own assistant, or the one you already use. The question is about this page; you can read it, copy it or change it there.

The question we’ll ask
“What does Excelsior Technologies, the software company in Ahmedabad and Toronto, do, what have they built, and would they be a good fit for my project?”

Opens in a new tab with this question. Nothing about you is sent.