I am trying to add an external embed code to my Gatsby page.
I currently use
import React from 'react' import Link from 'gatsby-link' let test ="<script type='text/javascript'> (function(d, s) { var useSSL = 'https:' == document.location.protocol; var js, where = d.getElementsByTagName(s)[0], js = d.createElement(s); js.src = (useSSL ? 'https:' : 'http:') + '//www.peopleperhour.com/hire/1002307300/1213788.js?width=300&height=135&orientation=vertical&theme=light&rnd='+parseInt(Math.random()*10000, 10); try { where.parentNode.insertBefore(js, where); } catch (e) { if (typeof console !== 'undefined' && console.log && e.stack) { console.log(e.stack); } } }(document, 'script')); </script>" const ContactPage = () => ( <div> <h1>ContactPage</h1> <div dangerouslySetInnerHTML={{ __html: test }} /> </div> ) export default ContactPage
which is full of syntax errors. Can you please point out a better way to include raw snippets in a react component?
Is there alternatively a place in Gatsby to add all additional scripts like custom scripts, Google Analytics, icon fonts, animate.js and anything else I may need?
Thank you for the help
If you need to insert custom HTML into the <head> or <footer> of each page on your site, you can use html.js. Customizing html.js is a workaround solution for when the use of the appropriate APIs is not available in gatsby-ssr.js. Consider using onRenderBody or onPreRenderHTML instead of the method above.
Fortunately adding plain JavaScript to Gatsby isn’† hard at all, you just need to expose a simple file, html.js, which isn’t visible in your Gatsby project by default. To expose (make available) the html.js file, open your terminal, navigate to the root of your Gatsby project folder, and enter the following:
One way - perhaps the preferred way - is using the SSR file. The file gatsby-ssr.js lets you alter the content of static HTML files as they are being Server-Side Rendered (SSR) by Gatsby and Node.js. To use the Gatsby SSR APIs, create a file called gatsby-ssr.js in the root of your site. Export any of the APIs you wish to use in this file.
You can inject external scripts (with no npm modules) to gatsby.js project in many ways. Prefer to use respective gatsby-plugin for "web-analytics" scripts. to a statefull component at the Pages folder inside render () and before return () if you want to execute that script only at that page (=page/component scope).
You can inject external scripts (with no npm modules) to gatsby.js project in many ways. Prefer to use respective gatsby-plugin for "web-analytics" scripts.
Using require()
:
myScript.js
and paste the script contentAdd const myExtScript = require('../pathToMyScript/myScript')
to a statefull component at the Pages folder inside render()
and before return()
if you want to execute that script only at that page(=page/component scope).
export default class Contact extends React.Component { render() { const myExtScript = require('../pathToMyScript/myScript') return ( ........ )}
If you want to execute script in the global scope (=in every page/ component):
add it in html.js
<script dangerouslySetInnerHTML= {{ __html: ` putYourSciptHereInBackticks `}} />`
before closing the </body>
. It is better to manipulate/monitor your global scope at this component because it has this specific purpose... to inject html to every page/route globally.
Another way to inject at the global scope is with require()
before the component declaration. This will work but it's not a good practice, as your components shouldn't inject anything globally.
const myExtScript = require('../pathToMyScript/myScript') export default class Contact extends React.Component { render() { return ( ........ )}
P.S. Sorry if the answer is not edited properly. This my first answer at StackOverflow.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With