Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add external Javascript in gatsby?

I want to know how to add external javascript file to gatsby, where should i put these tag in which file ? And also how to import the file properly to the DOM.

Thanks

like image 565
Adarsh Avatar asked Mar 02 '23 20:03

Adarsh


1 Answers

You can add this part of code in you gatsby-ssr.js, this add code in your balise .

export const onRenderBody = ({ setHeadComponents }) => {
  setHeadComponents([
    <link
      rel="stylesheet"
      href="/style.css"
    />
  ])
}

You Have the same for the body

export const onRenderBody = ({ setHeadComponents, setPostBodyComponents }) => {
      setHeadComponents([
        <link
          rel="stylesheet"
          href="/style.css"
        />
      ]),
setPostBodyComponents([
       <script></script>,
      ]),
    }
like image 162
Dako Junior Avatar answered Mar 05 '23 18:03

Dako Junior