I'm using Datawrapper to display graphs as iframes on my website.
I just recently migrated from Remark to MDX. With the new setup, my Datawrapper graphs aren't loading before the page that contains the graphs is allowed to load in the "traditional" sense (i.e., hitting refresh or following the page that contains the graphs directly).
So, if you follow this link, the graphs display fine: kolstadmagnus.no/ekspert-mener-a-ha-svaret-bak-krfs-stortingssmell.
But if you go to the home page (kolstadmagnus.no) and then hit the link (second-top post), the graphs won't display.
Some context
Datawrapper iframes use this script so that the iframes will be responsive:
!function(){"use strict";window.addEventListener("message",(function(e){if(void 0!==e.data["datawrapper-height"]){var t=document.querySelectorAll("iframe");for(var a in e.data["datawrapper-height"])for(var r=0;r<t.length;r++){if(t[r].contentWindow===e.source)t[r].style.height=e.data["datawrapper-height"][a]+"px"}}}))}();
Since Gatsby works the way it does (not loading every page "traditionally"—sorry, I don't know what the feature is called), I have had to put this script in the html.js file, like this:
<body {...props.bodyAttributes}>
{props.preBodyComponents}
<div
key={`body`}
id="___gatsby"
dangerouslySetInnerHTML={{ __html: props.body }}
/>
{props.postBodyComponents}
<script
dangerouslySetInnerHTML={{
__html: `
!function(){"use strict";window.addEventListener("message",(function(e){if(void 0!==e.data["datawrapper-height"]){var t=document.querySelectorAll("iframe");for(var a in e.data["datawrapper-height"])for(var r=0;r<t.length;r++){if(t[r].contentWindow===e.source)t[r].style.height=e.data["datawrapper-height"][a]+"px"}}}))}(); `
}}
/>
</body>;
This causes the script to be executed for the entire website instead of just for that single page, thereby solving the issue where hitting refresh would be required after entering the pages with the iframes.
With MDX though, problems are back. But now the iframes don't display at all.
How do I solve this?