Start elm render mutation observer sooner to make sure pre-rendering doesn't stall.

This commit is contained in:
Dillon Kearns 2020-01-31 08:46:18 -08:00
parent 08aa55a479
commit c09410a74c

View File

@ -2,6 +2,8 @@ const elmPagesVersion = require("./package.json").version;
let prefetchedPages; let prefetchedPages;
let initialLocationHash; let initialLocationHash;
let elmViewRendered = false;
let headTagsAdded = false;
module.exports = function pagesInit( module.exports = function pagesInit(
/** @type { mainElmModule: { init: any } } */ { mainElmModule } /** @type { mainElmModule: { init: any } } */ { mainElmModule }
@ -11,6 +13,13 @@ module.exports = function pagesInit(
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
document.addEventListener("DOMContentLoaded", _ => { document.addEventListener("DOMContentLoaded", _ => {
new MutationObserver(function() {
elmViewRendered = true;
if (headTagsAdded) {
document.dispatchEvent(new Event("prerender-trigger"));
}
}).observe(document.body, { attributes: true, childList: true, subtree: true});
loadContentAndInitializeApp(mainElmModule).then(resolve, reject); loadContentAndInitializeApp(mainElmModule).then(resolve, reject);
}); });
}) })
@ -45,11 +54,10 @@ function loadContentAndInitializeApp(/** @type { init: any } */ mainElmModule)
fromElm.head.forEach(headTag => { fromElm.head.forEach(headTag => {
appendTag(headTag); appendTag(headTag);
}); });
headTagsAdded = true;
new MutationObserver(function() { if (elmViewRendered) {
document.dispatchEvent(new Event("prerender-trigger")); document.dispatchEvent(new Event("prerender-trigger"));
}).observe(document.body, { attributes: true, childList: true, subtree: true}); }
} else { } else {
setupLinkPrefetching(); setupLinkPrefetching();
} }