Katex Fails After Enabling Rocket Loader in Cloudflare Environment

Words: 211

After deploying the blog online, I found that the Katex formulas would not display properly, while the same Html file displayed correctly in the local environment.

The core parsing code is as follows:

javascript
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
window.addEventListener("DOMContentLoaded", () => {
    renderMathInElement(document.body, {
        delimiters: [
            { left: "$$", right: "$$", display: true },
            { left: "$", right: "$", display: false },
            { left: "\\(", right: "\\)", display: false },
            { left: "\\[", right: "\\]", display: true }
        ],
        ignoredClasses: ["gist"]
    });
})

By adding logging, I found that the DOMContentLoaded event was not firing.

But I couldn’t figure out the reason for a long time, until I noticed that in the online version the <script> tag had become something like <script type="d6a94c0af8a975daac5bee56-text/javascript">.

Eventually I recalled that Cloudflare had Rocket Loader enabled.

Rocket Loader is a Cloudflare feature that loads JavaScript asynchronously to improve page load speed.

However, Rocket Loader may change the loading order and thereby cause some JavaScript code to fail to execute properly.

I disabled Rocket Loader for now, and the problem was solved.

You can also disable Rocket Loader by adding data-cfasync="false" to the tag, but since that would require modifying the Theme, I won’t make that change for now.

This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...