·

Mathjax loads slow. How to load local JS file with Nuxt?

Published at 2024-06-17 21:35:18Viewed 576 times
Professional article
Please reprint with source link

In How to render math in Vue or Nuxt?, we explain the way to use Mathjax to render beautiful math formula in Nuxt.js. However, using CDN to load Mathjax is easy, but it degrades the performance of our pages. In other words, Mathjax may load slow if it is loaded by CDN.

One of the solutions is to load Mathjax locally. You can use npm to install Mathjax to your project.

npm install mathjax@4.0.0-beta.6

However, it is difficult to integrate with Mathjax in Nuxt. Not only because there is little information about this, but following the Mathjax documentation may not work at all for Nuxt.js.

Therefore, there is only one way that is easy enough: load Mathjax's JS file locally. In other words, you continue to use CDN to load Mathjax, but load local Mathjax's JS file. Just follow the steps:

  1. Download Mathjax's source files from MathJax v4.0.0-beta.6.
  2. Put all the Mathjax files in the directory public/mathjax. Note that if you are using VSCode to write your Nuxt project, don't forget to see VSCode error: To enable project-wide JavaScript/TypeScript language features, exclude large folders... to avoid an important error.
  3. In your nuxt.config.ts
head: {
    link: [{
        rel: 'icon',
        type: 'image/x-icon',
        href: '/favicon.png'
    }],
    script: [
        {
          type: "text/javascript",
          id: "MathJax-script",
          async: true,
          src: "/mathjax/tex-chtml.js",
          tagPosition: "bodyClose",
        },
        {
          innerHTML:
            "MathJax = {tex: {inlineMath: [['$', '$'],['$$', '$$']]}};",
        },

    ]
}


0 人喜欢

Comments

There is no comment, let's add the first one.

弦圈热门内容

Vertically aligning CSS :before and :after content

I am trying to centre the link with the image, but can't seem to move the content vertically in any way.<h4>More Information</h4> <a href="#" class="pdf">File Name</a>The icon is 22 x 22px.pdf { font-size: 12px; } .pdf:before { padding:0 5px 0 0; content: url(../img/icon/pdf_small.png); } .pdf:after { content: " ( .pdf )"; font-size: 10px; } .pdf:hover:after { color: #000; }

Get connected with us on social networks! Twitter

©2024 Guangzhou Sinephony Technology Co., Ltd All Rights Reserved