Ember Cli Addon for Adding Content to HTML Head
Clone or download
Latest commit 9468c0c May 7, 2018
Permalink
Failed to load latest commit information.
addon Added empty content attribute to meta tags Mar 7, 2018
app Move instance initializer cleanup to head-layout component Jan 3, 2018
blueprints/ember-cli-head Add readme entry around upgrade to 0.4.x Aug 31, 2017
config Remove unecessary files & update travis/testing config Jan 3, 2018
fastboot-tests Fix fastboot tests Aug 7, 2017
tests Remove unecessary files & update travis/testing config Jan 3, 2018
vendor Initial Commit from Ember CLI v1.13.13 Dec 13, 2015
.dir-locals.el Make it ember Dec 13, 2015
.editorconfig Upgraded to ember-cli 2.12.2 / Babel6 / yarn Apr 26, 2017
.ember-cli Initial Commit from Ember CLI v1.13.13 Dec 13, 2015
.eslintrc.js Upgraded to ember-cli 2.12.2 / Babel6 / yarn Apr 26, 2017
.gitignore Upgraded to ember-cli 2.12.2 / Babel6 / yarn Apr 26, 2017
.npmignore Upgraded to ember-cli 2.12.2 / Babel6 / yarn Apr 26, 2017
.travis.yml Remove unecessary files & update travis/testing config Jan 3, 2018
.watchmanconfig Initial Commit from Ember CLI v1.13.13 Dec 13, 2015
.yarnrc Update dependencies Jan 3, 2018
CHANGELOG.md Add v0.4.1 to CHANGELOG. May 7, 2018
LICENSE.md Initial Commit from Ember CLI v1.13.13 Dec 13, 2015
README.md Add readme entry around upgrade to 0.4.x Aug 31, 2017
ember-cli-build.js Upgraded to ember-cli 2.12.2 / Babel6 / yarn Apr 26, 2017
index.js Make addon more idiomatic at the expense of making it 2.10+ & fastboo… Jun 5, 2017
package.json 0.4.1 May 7, 2018
testem.js Avoid issues with Chrome suid on CI. Mar 7, 2018
yarn.lock Update dependencies Jan 3, 2018

README.md

Ember-cli-head Build Status

This addon adds easy population of head tags from your Ember code without any direct hacky dom manipulation. This addon also provides ember-cli-fastboot compatability for generating head tags in server rendered apps.

The hope is that Ember itself will provide a mechanism for populating head tags from your app at some time in the future. Until then this addon provides that functionality.

Installation

Install by running

ember install ember-cli-head

And add {{head-layout}} to the top of your application template.

Version

Take into account that version >= 0.3 of this addon require Ember 2.10+ and fastboot >=1.0.rc1 Please use 0.2.X if you don't fulfull both requirements.

Usage

Template

By installing this addon you will find a new template added to your app:

app/templates/head.hbs

The contents of this template will be inserted into the <head> element of the page.

Service

There will be a model in the rendering scope of this template. This model is actually an alias for the head-data service. You can set whatever data you want to be available in the template directly on that service.

Example

Setting content data in route

// app/routes/application.js

import Ember from 'ember';

const { set } = Ember;

export default Ember.Route.extend({
  // inject the head data service
  headData: Ember.inject.service(),
  afterModel() {
    set(this, 'headData.title', 'Demo App');
  }
});

Using the service as model in head.hbs

<meta property="og:title" content={{model.title}} />

Resulting head

This will result in a document along the lines of:

<html data-ember-extension="1">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>My Ember App</title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <base href="/">

    <link rel="stylesheet" href="assets/vendor.css">
    <link rel="stylesheet" href="assets/my-app.css">

    <meta property="og:title" content="Demo App">
  </head>
  <body class="ember-application">


    <script src="assets/vendor.js"></script>
    <script src="assets/my-app.js"></script>
    <div id="ember383" class="ember-view"><h2 id="title">Welcome to Ember</h2>

    </div>
  </body>
</html>

Fastboot Only

The primary need for this library is to support various bots and web crawlers. To that end the head content is only truly needed in a server rendered (ie FastBoot) environment. However by default the library will keep the head content in sync with any transitions/data changes that occur in your Ember App while running in the browser. This can be useful for development and/or debugging.

If you do not wish to have the head content "live" while running in browser you can restrict this library to work only in FastBoot by adding the following to your config/environment.js:

module.exports = function(environment) {
  var ENV = {
    'ember-cli-head': {
        suppressBrowserRender: true
    }
  };
}

Upgrade to 0.4.x

As mentioned above you need to add the {{head-layout}} component once and only once in an application wide template. This template is usually app/templates/application.hbs, but could be different in your case. Previously, in ember-cli-head 0.3.x and below the component was appended to the document inside an instance initializer. This prevented the need for the {{head-layout}} component as it was automatically injected and used inside that initializer. Unfortunately, this approach needed to change so that we could render the component with the rest of the application rendering.

If you care to read more about the details of render please see the PR that introduced these changes https://github.com/ronco/ember-cli-head/pull/37

But for now, if you are upgrading to 0.4.x, you simply need to add {{head-layout}} component to your application wide template.

If you make use of this mode the content of <head> will be the static FastBoot rendered content through the life of your App.