• Hi,

    We’re running into an issue unloading KaTeX scripts from pages where we are not using the shortcode. We’ve tried the “Load KaTeX assets conditionally”, but then it does not load the assets when we need them.

    Assets loaded conditionally enabled: https://share.getcloudapp.com/Z4uGgJn9

    Assets loaded conditionally disabled:
    https://share.getcloudapp.com/04uvB0vm

    We’ve also tried using Perfmatters to unload KaTeX from the pages where it’s not in use, but there’s still one script loading from somewhere on every page:

    function _katexRender(rootElement) { const eles = rootElement.querySelectorAll(“.katex-eq:not(.katex-rendered)”); for(let idx=0; idx < eles.length; idx++) { const ele = eles[idx]; ele.classList.add(“katex-rendered”); try { katex.render( ele.textContent, ele, { displayMode: ele.getAttribute(“data-katex-display”) === ‘true’, throwOnError: false } ); } catch(n) { ele.style.color=”red”; ele.textContent = n.message; } } } function katexRender() { _katexRender(document); } document.addEventListener(“DOMContentLoaded”, function() { katexRender(); // Perform a KaTeX rendering step when the DOM is mutated. const katexObserver = new MutationObserver(function(mutations) { [].forEach.call(mutations, function(mutation) { if (mutation.target && mutation.target instanceof Element) { _katexRender(mutation.target); } }); }); const katexObservationConfig = { subtree: true, childList: true, attributes: true, characterData: true }; katexObserver.observe(document.body, katexObservationConfig); });

    Any tips for either correcting the load assets conditionally option or removing the script above from the pages where the KaTeX shortcode is not in use? Thank you!

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • Plugin Author Thomas Churchman

    (@beskhue)

    Hi there,

    Looking at the page you linked, it appears you are using a plugin to fetch content from the server dynamically (the quiz plugin?). The text with KaTeX content is fetched when the user finishes the quiz, but this only fetches the text, not this plugin’s KaTeX scripts.

    When text content is fetched dynamically, this plugin cannot automatically know in advance whether KaTeX will or won’t be used on a specific page.

    With such a setup there unfortunately is nothing this plugin can do to help, and you would need to always load the KaTeX resources. Perhaps the plugin doing the dynamic loading could be changed to perform some magic in automatically detecting and injecting missing scripts. That probably isn’t easy to do, though.

    I would assume that if Perfmatters removes the KaTeX resources from pages it doesn’t detect KaTeX usage on, that KaTeX would also stop rendering on the quiz page.

Viewing 1 replies (of 1 total)
  • The topic ‘Unloading KaTeX scripts from pages where not in use’ is closed to new replies.