# Add a script tag to index.html only on production build

**URL:** <https://discuss.emberjs.com/t/add-a-script-tag-to-index-html-only-on-production-build/7438>\
**Category:** Ember CLI\
**Created:** [March 4, 2015, 8:56am UTC](https://discuss.emberjs.com/t/add-a-script-tag-to-index-html-only-on-production-build/7438 "2015-03-04T08:56:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tschoartschi](https://avatars.discourse-cdn.com/v4/letter/t/f0a364/32.png) [@tschoartschi](https://discuss.emberjs.com/u/tschoartschi)\
**Post date:** [March 4, 2015, 8:56am UTC](https://discuss.emberjs.com/t/add-a-script-tag-to-index-html-only-on-production-build/7438/1 "2015-03-04T08:56:58Z")

</div>

Hello!

How can I add a script-tag to index.html only when I build my app in production mode? The background to this is:

We want to include a third party library but only when our app is in production mode. The constraint is, that the script needs to be the first script embedded on the page. So I can’t just load it after Ember is ready.

Is there a good way using Broccoli or the Ember-Cli-asset-pipeline to achieve this? Here just some short snippets to get the idea what I need:

In production mode:

```
<html>
  <head>
   <title>...</title>
   <script src="http://....THIRD-PARTY" />
   ...
   ...

```

In development mode:

```
<html>
  <head>
   <title>...</title>
   // NO THIRD PARTY SCRIPT HERE
   ...
   ...

```

I’m sure there are some neat tricks to do this 😉Thanks

---

<div class="post-metadata">

**Author:** ![tschoartschi](https://avatars.discourse-cdn.com/v4/letter/t/f0a364/32.png) [@tschoartschi](https://discuss.emberjs.com/u/tschoartschi)\
**Post date:** [March 6, 2015, 6:58am UTC](https://discuss.emberjs.com/t/add-a-script-tag-to-index-html-only-on-production-build/7438/2 "2015-03-06T06:58:48Z")

</div>

I wanted to push this thread because I didn’t find a solution but it is very urgent to solve this issue. So if anyone has an idea, please let me know 😄

---

<div class="post-metadata">

**Author:** ![tschoartschi](https://avatars.discourse-cdn.com/v4/letter/t/f0a364/32.png) [@tschoartschi](https://discuss.emberjs.com/u/tschoartschi)\
**Post date:** [March 6, 2015, 1:35pm UTC](https://discuss.emberjs.com/t/add-a-script-tag-to-index-html-only-on-production-build/7438/3 "2015-03-06T13:35:13Z")

</div>

If someone has the same problem, here is how I solved it:

Into Brocfile.js

```
// At the beginning of the file
var replace = require('broccoli-string-replace');
...
...
...
// At the end:
var settings = app.options.getEnvJSON(app.env);
var tree = app.toTree();

tree = replace(tree, {
    files: 'index.html',
    patterns: [{
        match: /\{\{STR_TO_REPLACE\}\}/g,
        replacement: settings.APP.STR_TO_REPLACE
    }]
});

```

therefore I needed to install broccoli-string-replace via npm. My environment.js looks like follow:

```
APP: {
    //...
    //...
    STR_TO_REPLACE: ''
    //...
    //...
}

if (environment === 'development') {
    //...
    //...
}
//...
if (environment === 'production') {
   ENV.APP.STR_TO_REPLACE = ...;
}
//...

```

Works good for me. Don’t know if it is the best solution but I does what I need 😄

---

<div class="post-metadata">

**Author:** ![Panman8201](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/panman8201/32/17727_2.png) [@Panman8201](https://discuss.emberjs.com/u/Panman8201)\
**Post date:** [March 6, 2015, 3:57pm UTC](https://discuss.emberjs.com/t/add-a-script-tag-to-index-html-only-on-production-build/7438/4 "2015-03-06T15:57:49Z")

</div>

I think the “proper” ember-cli way of doing this is to access the [`content-for` hook](http://www.ember-cli.com/#content). However, you can only access that from an addon so your best option is to create an [in-repo addon](http://www.ember-cli.com/#detailed-list-of-blueprints-and-their-use) (scroll down to “In-Repo Addon”). I’m hoping they expose these hooks from within the app someday…

---

<div class="post-metadata">

**Author:** ![Dan\_Nelson](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/dan_nelson/32/11890_2.png) [@Dan\_Nelson](https://discuss.emberjs.com/u/Dan_Nelson)\
**Post date:** [September 7, 2023, 6:17pm UTC](https://discuss.emberjs.com/t/add-a-script-tag-to-index-html-only-on-production-build/7438/5 "2023-09-07T18:17:33Z")

</div>

Obviously a decade late but … I stumbled onto this while trying to remember how I’ve used `{{content-for ...}}` in the past and I was reminded of this terrific addon: [GitHub - gpoitch/ember-cli-inline-content: An ember-cli add-on to render inline scripts, styles, or any content directly into your index.html file](https://github.com/gpoitch/ember-cli-inline-content)

---

<div class="post-metadata">

**Author:** ![horatio](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/horatio/32/18506_2.png) [@horatio](https://discuss.emberjs.com/u/horatio)\
**Post date:** [March 13, 2025, 2:59pm UTC](https://discuss.emberjs.com/t/add-a-script-tag-to-index-html-only-on-production-build/7438/6 "2025-03-13T14:59:42Z")

</div>

Your post was the way out of the rabbit hole. That addon was the exact solution I needed to dynamically add a meta tag into the index.html based on whether or not an environment variable was turned on.
