Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fastboot title updates with nested slow model() routes #196

Merged
merged 1 commit into from
Oct 17, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions addon/services/page-title-list.js
Original file line number Diff line number Diff line change
@@ -19,6 +19,8 @@ export default class PageTitleListService extends Service {
@service
router;

// in fastboot context "document" is instance of
// ember-fastboot/simple-dom document
@service('-document')
document;

@@ -204,11 +206,7 @@ export default class PageTitleListService extends Service {
const toBeTitle = this.toString();

if (isFastBoot) {
// in fastboot context "document" is instance of ember-fastboot/simple-dom document
let titleEl = this.document.createElement('title');
let titleContents = this.document.createTextNode(toBeTitle);
titleEl.appendChild(titleContents);
this.document.head.appendChild(titleEl);
this.updateFastbootTitle(toBeTitle);
} else {
this.document.title = toBeTitle;
}
@@ -243,4 +241,26 @@ export default class PageTitleListService extends Service {
return token.id === id;
})[0];
}

updateFastbootTitle(toBeTitle) {
if (!isFastBoot) {
return;
}
const headElement = this.document.head;
const headChildNodes = headElement.childNodes;

// Remove existing title elements from previous render cycle
for (let i = 0; i < headChildNodes.length; i++) {
let node = headChildNodes[i];
if (node.nodeName.toLowerCase() === 'title') {
headElement.removeChild(node);
}
}

// Add title element with latest value
let titleEl = this.document.createElement('title');
let titleContents = this.document.createTextNode(toBeTitle);
titleEl.appendChild(titleContents);
headElement.appendChild(titleEl);
}
}