Skip to content

Commit 164452e

Browse files
author
Francesco Novy
committed
Move instance initializer cleanup to head-layout component
Previously, any eventual head data built by fastboot was cleaned up in an instance initializer. This was now moved into the head-layout component, where this happens on init. This fixes an issue where this cleanup happens multiple times when using lazy loading engines. Additionally, this moves the head-layout component into the addon namespace, which makes it possible to overwrite/extend from it.
1 parent bfb802e commit 164452e

3 files changed

Lines changed: 62 additions & 33 deletions

File tree

addon/components/head-layout.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import Component from '@ember/component';
2+
import { get, computed } from '@ember/object';
3+
import { getOwner } from '@ember/application';
4+
import layout from '../templates/components/head-layout';
5+
6+
export default Component.extend({
7+
tagName: '',
8+
layout,
9+
10+
/**
11+
* If true, this will tear down any existing head on init of this component.
12+
* This is useful if there is a head built with fastboot - it will then be torn down when this is initialized in the browser.
13+
* If you do not want this behavior, you can set this to false.
14+
* @public
15+
*/
16+
shouldTearDownOnInit: true,
17+
18+
headElement: computed(function() {
19+
let documentService = getOwner(this).lookup('service:-document');
20+
return documentService.head;
21+
}),
22+
23+
init() {
24+
this._super(...arguments);
25+
26+
if (get(this, 'shouldTearDownOnInit')) {
27+
this._tearDownHead();
28+
}
29+
},
30+
31+
/**
32+
* Tear down any previous head, if there was one.
33+
* @private
34+
*/
35+
_tearDownHead() {
36+
if (this._isFastboot()) {
37+
return;
38+
}
39+
40+
// clear fast booted head (if any)
41+
let startMeta = document.querySelector('meta[name="ember-cli-head-start"]');
42+
let endMeta = document.querySelector('meta[name="ember-cli-head-end"]');
43+
if (startMeta && endMeta) {
44+
let el = startMeta.nextSibling;
45+
while (el && el !== endMeta) {
46+
document.head.removeChild(el);
47+
el = startMeta.nextSibling;
48+
}
49+
document.head.removeChild(startMeta);
50+
document.head.removeChild(endMeta);
51+
}
52+
},
53+
54+
_isFastboot() {
55+
return typeof FastBoot !== 'undefined'
56+
}
57+
58+
});

app/components/head-layout.js

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1 @@
1-
import Ember from 'ember';
2-
import layout from 'ember-cli-head/templates/components/head-layout';
3-
4-
export default Ember.Component.extend({
5-
tagName: '',
6-
headElement: Ember.computed(function() {
7-
let documentService = Ember.getOwner(this).lookup('service:-document');
8-
return documentService.head;
9-
}),
10-
layout
11-
});
1+
export { default } from 'ember-cli-head/components/head-layout';
Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,8 @@
1-
import ENV from '../config/environment';
2-
3-
export function initialize() {
4-
if (ENV['ember-cli-head'] && ENV['ember-cli-head']['suppressBrowserRender']) { return true; }
5-
6-
// clear fast booted head (if any)
7-
let startMeta = document.querySelector('meta[name="ember-cli-head-start"]')
8-
let endMeta = document.querySelector('meta[name="ember-cli-head-end"]')
9-
if (startMeta && endMeta) {
10-
let el = startMeta.nextSibling
11-
while(el && el !== endMeta) {
12-
document.head.removeChild(el);
13-
el = startMeta.nextSibling;
14-
}
15-
document.head.removeChild(startMeta);
16-
document.head.removeChild(endMeta);
17-
}
18-
}
19-
201
export default {
212
name: 'head-browser',
223
initialize() {
23-
if (typeof FastBoot === 'undefined') {
24-
initialize(...arguments);
25-
}
4+
// do nothing!
5+
// this functionality has been moved into addon/components/head-layout.js
6+
// This is only here in order to not break existing addons relying on this, e.g. ember-page-title.
267
}
278
};

0 commit comments

Comments
 (0)