-
Notifications
You must be signed in to change notification settings - Fork 10.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How do you insert an external script using gatsby-browser? #11013
Comments
React helmet is a great package for that. You can place it in your page/template or other react components. |
Sorry if I didn't I make it clear, I mentioned on my original post that I'm running into this exact issue nfl/react-helmet#324 with react-helmet. According to one of the comments, they were also using gatsbyjs and they ended up using |
No problem, for that you can put your js file in the
|
@daydream05 Since the external script you're referring to uses |
Hiya! This issue has gone quiet. Spooky quiet. 👻 We get a lot of issues, so we currently close issues after 30 days of inactivity. It’s been at least 20 days since the last update here. If we missed this issue or if you want to keep it open, please reply here. You can also add the label "not stale" to keep this issue open! Thanks for being a part of the Gatsby community! 💪💜 |
Hey again! It’s been 30 days since anything happened on this issue, so our friendly neighborhood robot (that’s me!) is going to close it. Please keep in mind that I’m only a robot, so if I’ve closed this issue in error, I’m Thanks again for being part of the Gatsby community! |
For future visitors: I had a similar problem and what worked for me was to load my (remote) script using a plugin, gatsby-plugin-load-script. https://www.gatsbyjs.org/packages/gatsby-plugin-load-script/ The instructions on that page are currently just an example Sentry but you only really need to use the
|
The above solution works for me - had been trying to get Lottie to load in the browser via CDN once the DOM content had been rendered, and nothing else I tried, including everything in the current Gatsby docs, worked. Thank you @christopherfrance for the tip! |
Script tags should be placed at the end of page. Whereas Basically, you just need a Create import React from 'react'
export const onRenderBody = ({ setPostBodyComponents }) => {
setPostBodyComponents([
<script
key="https://code.jquery.com/jquery-3.2.1.slim.min.js"
src="https://code.jquery.com/jquery-3.2.1.slim.min.js"
integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN"
crossOrigin="anonymous"
defer
/>,
<script
key="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"
src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"
integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"
crossOrigin="anonymous"
defer
/>,
<script
key="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"
src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"
integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
crossOrigin="anonymous"
defer
/>
])
} |
What if the same files i want to add gatsby-browser.js ? |
Love this answer! |
Hi, I am a newbie to gatsby. Can anyone help me by proving me an example repository that explains adding multiple external javascript files into a gatsby site and using them? Thanks in advance. |
@SNandini8698 the answer above shows how to pull in external javascript files |
I tried gatsby-browser.js and gatsby-ssr.js methods but neither worked well for me. |
Summary
I would like to external scripts that would work after the React Dom is rendered. In addition, I would only like it loaded on specific pages.
Relevant information
I'm trying to load an external script that uses the global
document
. It works locally but I keep getting aWebpackError: ReferenceError: document is not defined
when deployed on Netlify. I've tried using React-Helmet but I was running into this issue nfl/react-helmet#324 .File contents (if changed)
gatsby-config.js
: N/Apackage.json
: N/Agatsby-node.js
: N/Agatsby-browser.js
gatsby-ssr.js
: N/AThe text was updated successfully, but these errors were encountered: