diff --git a/assets/js/graph.js b/assets/js/graph.js index a1cbba9ab..79b276e3a 100644 --- a/assets/js/graph.js +++ b/assets/js/graph.js @@ -1,8 +1,5 @@ async function drawGraph(url, baseUrl, pathColors, depth, enableDrag, enableLegend, enableZoom) { const container = document.getElementById("graph-container"); - // We should clear the graph in case there is anything within it - if (!container) return; - container.textContent = ""; const { index, links, content } = await fetchData; const curPage = url.replace(baseUrl, ""); diff --git a/layouts/partials/head.html b/layouts/partials/head.html index ec37f8816..72a44736c 100644 --- a/layouts/partials/head.html +++ b/layouts/partials/head.html @@ -51,8 +51,13 @@ window.navigate = navigate; router(".singlePage"); const callback = () => { - // RAF waits for Million to complete diffing, then draw - requestAnimationFrame(() => { + const draw = () => { + const container = document.getElementById("graph-container"); + // retry if the graph is not ready + if (!container) return requestAnimationFrame(draw); + // clear the graph in case there is anything within it + container.textContent = ""; + drawGraph( {{strings.TrimRight "/" .Page.Permalink}}, {{strings.TrimRight "/" .Site.BaseURL}}, @@ -61,8 +66,9 @@ {{$.Site.Data.graphConfig.enableDrag}}, {{$.Site.Data.graphConfig.enableLegend}}, {{$.Site.Data.graphConfig.enableZoom}} - )} - ); + ); + }; + requestAnimationFrame(draw); }; // We need on initial load, then subsequent redirs window.addEventListener("million:navigate", callback);