Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

ember-cli-head

ronco37.8kMIT2.0.0

An addon to easily set document head content via a template.

ember-addon, meta, seo

readme

Build Status

ember-cli-head

This addon lets you populate <head> tag from your Ember code without any direct hacky DOM manipulation. It also provides ember-cli-fastboot compatibility for generating head tags in server-rendered apps.

The hope is that, in the future, Ember will provide a mechanism for populating <head> tag from your app. Until then, this addon provides that functionality.

  • Ember.js v3.16 or above
  • Ember CLI v2.13 or above
  • Node.js v12 or above

Installation

Install by running

ember install ember-cli-head

Then, add <HeadLayout /> to the top of your application template.

{{!-- app/templates/application.hbs --}}

<HeadLayout />

{{outlet}}

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 fulfill both requirements.

Usage

Head template

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

app/templates/head.hbs

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

Head data service

The addon provides model that is scoped to the head template. The model is actually an alias of the head-data service. You can set whatever data you want to be available to the head template on this service.

⚠️ Warning for Octane apps

Because model refers to the head-data service (and not what a route's model hook returns), it is important to use this.model (not @model) in the head template.

Example

Setting content data in route

// app/routes/application.js

import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';

export default class ApplicationRoute extends Route {
  @service headData;

  afterModel() {
    this.headData.title = 'Demo App';
  }
}

Declare title as a tracked property on the head-data service

// app/services/head-data.js

import Service from '@ember/service';
import { tracked } from '@glimmer/tracking';

export default class HeadDataService extends Service {
  @tracked title;
}

Using the service in head template

{{!-- app/templates/head.hbs --}}

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

Checking head tag

This will result in a document along the lines of:

<html data-ember-extension="1">
  <head>
    ...
    <meta name="ember-cli-head-start" content>
    <meta property="og:title" content="Demo App">
    <meta name="ember-cli-head-end" content>
  </head>
  <body class="ember-application">
    ...
  </body>
</html>

FastBoot-Only Use

The primary need for this addon is to support various bots and web crawlers. To that end, the head content is only truly needed in a server-rendered environment like FastBoot.

By default, the addon will keep the head content in sync with any route transitions and data changes that occur when your Ember app runs in the browser. This can be useful for development and debugging.

If you don't wish the head content to be "live" when the app runs in browser, you can restrict this addon to run only in FastBoot:

// config/environment.js

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

  return ENV;
};

If you use suppressBrowserRender, the content of <head> will be the static FastBoot-rendered content throughout your app's lifecycle.

Upgrade to 0.4.x

As previously mentioned, you need to add the <HeadLayout /> component once and only once in an application-wide template. This template is usually app/templates/application.hbs but may be different in your case.

Prior to 0.4, the component was appended to the document inside an instance initializer. This prevented the need for the <HeadLayout /> component as it was automatically injected and used inside that initializer. This approach needed to change so that we could render the component with the rest of the application rendering.

In short, if you are upgrading to 0.4.x, you simply add the <HeadLayout /> component to your application-wide template.

Contributing

See the Contributing guide for details.

License

This project is licensed under the MIT License.

changelog

Changelog

v2.0.0 (2021-05-26)

:boom: Breaking Change

:rocket: Enhancement

Committers: 5

v1.0.0 (2020-06-02)

:boom: Breaking Change

:rocket: Enhancement

  • #78 Update dependencies & devDependencies to latest. (@rwjblue)
  • #71 Migrate to {{#in-element}} (from emberjs/rfcs#287) (@gitKrystan)

:house: Internal

Committers: 5

v0.4.1 (2018-05-07)

Full Changelog

Closed issues:

  • Meta elements "ember-cli-head-start" and "ember-cli-head-end" missing "content" attribute #47
  • Does this repo really only work with Fastboot now? #46
  • When loading page you get no title while the application renders #43
  • Make FastBoot DOM smashing opt-in #41
  • 0.4.0 made all my head data stop working #40
  • rootURL favicon link difficulties (not using fastboot) #39

Merged pull requests:

  • Added empty content attribute to meta tags #50 (samcic)
  • Avoid issues with Chrome suid on CI. #49 (rwjblue)
  • Refactor instance-initializer #45 (mydea)

v0.4.0 (2017-09-21)

Full Changelog

Closed issues:

  • Acceptance testing #38

Merged pull requests:

v0.3.1 (2017-06-27)

Full Changelog

Closed issues:

  • head-browser instance-initializer not found from ember-page-title, booting the app fails (v0.3, fastboot rc3) #34
  • Fastboot deprecation #29

Merged pull requests:

  • Fix head-fastboot instance-initializer overriding head-browser #35 (simonihmig)
  • Make addon more idiomatic at the expense of making it 2.10+ & fastboot 1.0rc+ #32 (cibernox)

v0.2.2 (2017-06-01)

Full Changelog

Merged pull requests:

v0.2.1 (2017-06-01)

Full Changelog

Closed issues:

  • Incompatible with FastBoot #26
  • Cannot find fastboot-filter-initializers #22

Merged pull requests:

v0.2.0 (2017-04-26)

Full Changelog

v0.1.5 (2017-04-26)

Full Changelog

Closed issues:

  • Fastboot with Ember 2.11: EventDispatcher should never be instantiated in fastboot mode. #18

Merged pull requests:

v0.1.4 (2017-01-25)

Full Changelog

Merged pull requests:

  • Check ember-source version from NPM, if not found use ember from bower #19 (josemarluedke)

v0.1.3 (2016-11-10)

Full Changelog

Merged pull requests:

  • [fix] Correct logic for pushing app-lt-2-10 tree #14 (arjansingh)

v0.1.2 (2016-10-19)

Full Changelog

Merged pull requests:

  • Account for 2.9.0 not including Glimmer 2. #13 (rwjblue)

v0.1.1 (2016-09-08)

Full Changelog

Closed issues:

  • ember-alpha support #10

Merged pull requests:

  • Enable usage in Ember 2.9 beta and above. #11 (rwjblue)

v0.1.0 (2016-06-14)

Full Changelog

Closed issues:

  • Handle lack of app/templates/head.hbs more gracefully. #7

Merged pull requests:

  • include a default head template in app #9 (ronco)
  • remove unneeded slash on meta elements per HTML5 and ember-cli-template-lint #8 (openhouse)

v0.0.7 (2016-05-11)

Full Changelog

Merged pull requests:

  • Remove HTML Comments from head template. #6 (rwjblue)

v0.0.6 (2016-05-03)

Full Changelog

Closed issues:

  • head inside head #3

Merged pull requests:

  • Remove harding of isDevelopingAddon. #5 (rwjblue)

v0.0.5 (2016-03-17)

Full Changelog

Merged pull requests:

  • added option to suppress browser rendering for fastboot #4 (habdelra)

v0.0.4 (2016-03-01)

Full Changelog

Merged pull requests:

  • Clear fast boot rendered head on browser boot #2 (ronco)

v0.0.3 (2016-02-10)

Full Changelog

v0.0.2 (2016-02-10)

Full Changelog

v0.0.1 (2016-02-10)

Merged pull requests:

  • Fastboot compatibility #1 (ronco)