0

How do I add JS to the bottom of GatsbyJS? I'm new to React and Gatsby and need help with clear instructions. I don't have an index.html file with </body> visible anywhere in my /src folder. The script I would like to add is jQuery and a few other jQuery libraries that should only load after the content has been loaded.

  • Have you generated your website? You can run `gatsby build` to create the website with a root html. But it’s not recommended ti use jQuery with React! – Kokodoko May 02 '19 at 09:20
  • Have a look at https://stackoverflow.com/questions/54468240/how-to-include-jquery-in-a-gatsby-js-project – ksav May 02 '19 at 09:28
  • @Kokodoko when built, a Gatsby site is a set of pure static files and, so on, React shouldn't be included in the production folder. jQuery can then be used as a front-end framework, don't you think ? – imrok May 28 '21 at 06:06

1 Answers1

-1

Maybe, You have to use Jquery in your case, but best practices is avoid using JQuery because JQuery so slow.

With your question, I think the first, you should do with Gatsby tutorial to understand How Gatsby run/build and work.

After that, you can use either solution below to add custom script to your page: 1. Require directly your script file. 2. Use Helmet plugins 3. Use dangerouslySetInnerHTML

Some link will help you:

https://github.com/gatsbyjs/gatsby/issues/3083 add raw HTML with <script> inside Gatsby React page

Regards.

thaopv
  • 29
  • 3
  • I don't think using jQuery could be considered bad practice. It's not always the best choice for sure, but one could find meaningful uses. In addition, I think your tips cannot fall into the "clear instructions" OP asked for ("step 1: read Gatsby doc" ...). – imrok May 28 '21 at 06:02