Skip to content

Commit 853b0ff

Browse files
committed
Add a new service to abstract over fastboot.isFastBoot, in-line
clear-double-boot instance-initializer
1 parent ca084bb commit 853b0ff

5 files changed

Lines changed: 61 additions & 4 deletions

File tree

app/controllers/project-version.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export default class ProjectVersionController extends Controller {
1717
project;
1818

1919
@service
20-
fastboot;
20+
prerender;
2121

2222
@service router;
2323
@service('project') projectService;
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Overriding the ember-cli-fastboot instance-initializer so that
2+
// we can own this and migrate away.
3+
// When using `ember serve` when fastboot addon is installed the application
4+
// output will already be rendered to the DOM when the actual JavaScript
5+
// loads. Ember does not automatically clear its `rootElement` so this
6+
// leads to the "double" applications being visible at once (only the
7+
// "bottom" one is running via JS and is interactive).
8+
//
9+
// This removes any pre-rendered ember-view elements, so that the booting
10+
// application will replace the pre-rendered output
11+
export function clearHtml() {
12+
let current = document.getElementById('fastboot-body-start');
13+
let endMarker = document.getElementById('fastboot-body-end');
14+
15+
if (current && endMarker) {
16+
let shoeboxNodes = document.querySelectorAll('[type="fastboot/shoebox"]');
17+
let shoeboxNodesArray = []; // Note that IE11 doesn't support more concise options like Array.from, so we have to do something like this
18+
for (let i = 0; i < shoeboxNodes.length; i++) {
19+
shoeboxNodesArray.push(shoeboxNodes[i]);
20+
}
21+
let parent = current.parentElement;
22+
let nextNode;
23+
do {
24+
nextNode = current.nextSibling;
25+
parent.removeChild(current);
26+
current = nextNode;
27+
} while (
28+
nextNode &&
29+
nextNode !== endMarker &&
30+
shoeboxNodesArray.indexOf(nextNode) < 0
31+
);
32+
endMarker.parentElement.removeChild(endMarker);
33+
}
34+
}
35+
export default {
36+
name: 'clear-double-boot',
37+
38+
initialize(instance) {
39+
if (typeof FastBoot === 'undefined') {
40+
var originalDidCreateRootView = instance.didCreateRootView;
41+
42+
instance.didCreateRootView = function () {
43+
clearHtml();
44+
originalDidCreateRootView.apply(instance, arguments);
45+
};
46+
}
47+
},
48+
};

app/routes/application.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export default class ApplicationRoute extends Route {
1515
router;
1616

1717
@service
18-
fastboot;
18+
prerender;
1919

2020
@service
2121
metrics;
@@ -25,7 +25,7 @@ export default class ApplicationRoute extends Route {
2525

2626
constructor() {
2727
super(...arguments);
28-
if (!this.fastboot.isFastBoot) {
28+
if (!this.prerender.isPrerendering) {
2929
this.router.on('routeDidChange', this.trackPage);
3030

3131
/* Hax from https://github.com/DockYard/ember-router-scroll/issues/263

app/services/prerender.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import Service, { service } from '@ember/service';
2+
3+
export default class PrerenderService extends Service {
4+
@service fastboot;
5+
6+
get isPrerendering() {
7+
return this.fastboot.isFastBoot;
8+
}
9+
}

app/templates/project-version.gjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ import ScrollToTopButton from 'ember-api-docs/components/scroll-to-top-button';
4444
<section class="content-wrapper">
4545
{{outlet}}
4646

47-
{{#unless @controller.fastboot.isFastBoot}}
47+
{{#unless @controller.prerender.isPrerendering}}
4848
<ScrollToTopButton />
4949
{{/unless}}
5050
</section>

0 commit comments

Comments
 (0)