Pre-boot JavaScript


#1

I’m looking for a pattern for code that should run before Ember boots. I’m thinking of things like initializing tracking libraries and checking browser compatibility.

ember-cli-google-analytics injects an inline <script> into <head>. I don’t love that because it violates Content-Security-Policy (unless you generate a cryptographic hash of the inline script contents and add that to the CSP definition).

Another option would be putting the code in app/initializers/. I don’t love that (at least for the browser-compatibility detection) because the browser may never get to initializers.

I’m considering adding a pre-boot/ directory that gets appended to app.js without being wrapped in require.define blocks so it executes immediately.

Does anyone have a pattern they really like?


#2

How about adding your code to vendor/whatever-correct-name.js? It gets prepended to the vendor.js file, hence run before your app.

And if you want to pass some of the gathered config to the app or control when/if your app should start, here’s an addon that could be useful: https://github.com/xcambar/ember-cli-embedded#start-your-app

Hope this helps,


#3

Forget about the plugin. By the time your code in you vendor file runs, you can’t get the reference to the (unstarted) app. That won’t work as easily… :thinking: