|
| 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 | +}; |
0 commit comments