diff --git a/frontend/src/main.js b/frontend/src/main.js index a42ffb8..d70b3c4 100644 --- a/frontend/src/main.js +++ b/frontend/src/main.js @@ -12,41 +12,63 @@ import { timeline } from './timeline'; timeline.start(); +let loaded = false; +let loadedListener = null; +document.addEventListener("DOMContentLoaded", () => { + loaded = true; + if (loadedListener) { + loadedListener(); + loadedListener = null; + } +}); + function handleGatewaySettlement() { timeline.addCheckpoint("Got gateway settlement"); - const loadingElement = document.getElementById("pre--loading-screen"); - if (loadingElement) { - loadingElement.parentElement.removeChild(loadingElement); + const loadApplication = () => { + timeline.addCheckpoint("Application load started"); + + const loadingElement = document.getElementById("pre--loading-screen"); + if (loadingElement) { + loadingElement.parentElement.removeChild(loadingElement); + } + + const app = new Main({ + target: document.body + }); + window.__waffle.app = app; + + timeline.addCheckpoint("Created application"); + + pluginStore.consumePluginLoaders(); + const scripts = getItem("app:javascript"); + scripts.forEach((script) => { + const func = new Function(script); + func(); + }); + const styles = getItem("app:stylesheets"); + styles.forEach((styleText) => { + const style = document.createElement("style"); + style.textContent = styleText; + document.head.appendChild(style); + }); + + timeline.addCheckpoint("Loaded custom user content"); + + gateway.unsubscribe(GatewayEventType.Ready, handleGatewaySettlement); + gateway.unsubscribe(GatewayEventType.Close, handleGatewaySettlement); + + timeline.addCheckpoint("All initialization finished"); + timeline.dump(); + }; + + if (!loaded) { + timeline.addCheckpoint("Waiting for DOM content before loading application"); + + loadedListener = loadApplication; + } else { + loadApplication(); } - - const app = new Main({ - target: document.body - }); - window.__waffle.app = app; - - timeline.addCheckpoint("Created application"); - - pluginStore.consumePluginLoaders(); - const scripts = getItem("app:javascript"); - scripts.forEach((script) => { - const func = new Function(script); - func(); - }); - const styles = getItem("app:stylesheets"); - styles.forEach((styleText) => { - const style = document.createElement("style"); - style.textContent = styleText; - document.head.appendChild(style); - }); - - timeline.addCheckpoint("Loaded custom user content"); - - gateway.unsubscribe(GatewayEventType.Ready, handleGatewaySettlement); - gateway.unsubscribe(GatewayEventType.Close, handleGatewaySettlement); - - timeline.addCheckpoint("All initialization finished"); - timeline.dump(); } function main() {