Skip to content

Commit 1de2674

Browse files
committed
Migrate away from ember-cli-head for <title> updates
1 parent d3a8a86 commit 1de2674

File tree

9 files changed

+5749
-5298
lines changed

9 files changed

+5749
-5298
lines changed

README.md

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ This addon provides a helper for changing the title of the page you're on.
88
ember install ember-page-title
99
```
1010

11-
Add `{{head-layout}}` to your application's `application.hbs` template.
12-
1311
<details>
1412
<summary>Fastboot vs Non-Fastboot Notes</summary>
1513

@@ -60,21 +58,15 @@ module.exports = function (environment) {
6058
};
6159
```
6260

63-
### Fastboot
64-
65-
When working with other addons that use `ember-cli-head`, you'll need to create a custom `head.hbs` file that exposes the `<title>` tag properly:
66-
67-
```hbs
68-
<title>{{model.title}}</title>
69-
```
70-
71-
This file is added automatically if you use `ember install`. This is for all the folks using ember-cli-head addons like ember-cli-meta-tags.
72-
7361
### Deprecations
7462

7563
- Since **v5.2.2**: The `{{title}}` helper has been deprecated, use `{{page-title}}` instead, it has the same API. The
7664
`{{title}}` helper was an AST transform and will be removed in the next major release.
7765

66+
### Upgrading notes for 5.x to 6.x
67+
68+
From 6.x onward, you don't need to have `{{head-layout}}` within your application's `application.hbs` template. If you have `ember-cli-head` as direct dependency on your project, but not used for anything else you can also remove it.
69+
7870
### Upgrading notes for 3.x to 4.x
7971

8072
From 4.x onward, you need to have `{{head-layout}}` within your application's `application.hbs` template. Without this, you will not see a page title appear.

addon/helpers/page-title.js

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
11
import { scheduleOnce } from '@ember/runloop';
22
import { inject as service } from '@ember/service';
33
import Helper from '@ember/component/helper';
4-
import { set } from '@ember/object';
4+
import { reads } from '@ember/object/computed';
55
import { guidFor } from '@ember/object/internals';
66
import { assign } from '@ember/polyfills';
77
import { getOwner } from '@ember/application';
88

9-
function updateTitle(tokens) {
10-
set(this, 'title', tokens.toString());
9+
function updateTitle(document, tokens, isFastBoot) {
10+
const toBeTitle = tokens.toString();
11+
12+
if (isFastBoot) {
13+
let childNodes = document.head.childNodes;
14+
for (let i = 0; i < childNodes.length; i++) {
15+
let node = childNodes.item(i);
16+
if (node.tagName.toLowerCase() === 'title') {
17+
document.head.removeChild(node);
18+
}
19+
}
20+
let titleEl = document.createElement('title');
21+
let titleContents = document.createTextNode(toBeTitle);
22+
titleEl.appendChild(titleContents);
23+
document.head.appendChild(titleEl);
24+
} else {
25+
document.title = toBeTitle;
26+
}
1127
}
1228

1329
/**
@@ -18,7 +34,9 @@ function updateTitle(tokens) {
1834
*/
1935
export default Helper.extend({
2036
pageTitleList: service(),
21-
headData: service(),
37+
document: service('-document'),
38+
fastboot: service(),
39+
isFastBoot: reads('fastboot.isFastBoot'),
2240

2341
init() {
2442
this._super();
@@ -40,7 +58,7 @@ export default Helper.extend({
4058
hash.id = guidFor(this);
4159
hash.title = params.join('');
4260
tokens.push(hash);
43-
scheduleOnce('afterRender', this.headData, updateTitle, tokens);
61+
scheduleOnce('afterRender', updateTitle, this.document, tokens, this.isFastBoot);
4462
return '';
4563
},
4664

@@ -52,16 +70,15 @@ export default Helper.extend({
5270
let router = getOwner(this).lookup('router:main');
5371
let routes = router._routerMicrolib || router.router;
5472
let { activeTransition } = routes || {};
55-
let headData = this.headData;
5673
if (activeTransition) {
57-
activeTransition.promise.finally(function () {
58-
if (headData.isDestroyed) {
74+
activeTransition.promise.finally(() => {
75+
if (this.isDestroyed) {
5976
return;
6077
}
61-
scheduleOnce('afterRender', headData, updateTitle, tokens);
78+
scheduleOnce('afterRender', updateTitle, this.document, tokens, this.isFastBoot);
6279
});
6380
} else {
64-
scheduleOnce('afterRender', headData, updateTitle, tokens);
81+
scheduleOnce('afterRender', updateTitle, this.document, tokens, this.isFastBoot);
6582
}
6683
},
6784
});

0 commit comments

Comments
 (0)