Difference between initializers and instance-initializers

Applies to Ember in general but mostly pertains to an ember-cli application. I curious about the background to these ways to initialize parts of an Ember application.

What is the exact difference between initializers found in app/initializer versus app/instance-initializers? Are instance-initializers a subset of an initializer?

Roughly, initializers are used to wire up the parts of the application. Think “finalizing the application mold”. That mostly means setting up dependencies and loading code. They are run once. And they cannot access any application objects, as none exist at this point.

Instance initializers are used to setup an instance of the application. Think “doing stuff on application start”. For instance, I use one to push pre-loaded data embedded in the page into the store. Instance initializers are run for every copy of your application and can use its objects. That usually means once, but not always, like when doing integration tests, each test will create an instance and run instance initializers on it.

This also lays ground for doing server-side rendering using node.js. As each request will need to create an application instance, factoring out static initialization tasks will speed up request handling. I don’t know what the status of that feature is. Look for “FastBoot” in docs/forum/github.

6 Likes

Thanks spectras. Your explanation definitely helps clarify each piece’s role in the ember app.