Theorder
Theorder

Reputation: 769

Inserting a Custom Script in React Helmet/ Gatsby

I have this custom script that from a third party I am trying to embed into React Helmet

Usually the scripts are given only as a src url. If I am given the code below, what is the best practice to embed the embed the script with React Helmet

    <script>
document.getElementsByTagName('head')[0].appendChild(function(s){
    var d=document,m2g=d.createElement('script'),l=function(){Mobi2Go.load(s.container,s.ready);},jq=window.jQuery&&+window.jQuery.fn.jquery.replace(/^(\d+).*$/,'$1')===1&&+window.jQuery.fn.jquery.replace(/^\d+\.(\d+).*$/,'$1')>=7,qs=window.location.search.substring(1),re='=(.*?)(?:;|$)',c=d.cookie.match('MOBI2GO_SESSIONID'+re),w=window.innerWidth;
    m2g.src='https://www.mobi2go.com/store/embed/1990-v8oL.js?'+qs+(jq?'&no_jquery':'')+(c?'&s='+c[1]:'')+'&device_width='+w;
    if(m2g.onload!==undefined)m2g.onload=l;else m2g.onreadystatechange=function(){if(m2g.readyState!=='loaded'&&m2g.readyState!=='complete')return;m2g.onreadystatechange=null;l();}
    window.Mobi2Go_est = +(new Date);
    return m2g;
}({
    container: 'Mobi2Go-Storefront', // Replace with ID  of the element to inject UI into
    ready: function() {} // Callback to fire when app is ready
}));
</script>

There is obviously errors if I include all of that in the React Helmet Not sure what is the best practice to make it work.

I have also tried placing it in the html.js file in gatsby

 import React from 'react';
import PropTypes from 'prop-types';

export default function HTML(props) {
  return (
    <html {...props.htmlAttributes}>
      <head>
        <meta charSet="utf-8" />
        <meta httpEquiv="x-ua-compatible" content="ie=edge" />
        <meta
          name="viewport"
          content="width=device-width, initial-scale=1, shrink-to-fit=no"
        />
        {props.headComponents}
      </head>
      <body {...props.bodyAttributes}>
        {props.preBodyComponents}
        <div
          key={`body`}
          id="___gatsby"
          dangerouslySetInnerHTML={{ __html: props.body }}
        />
        {props.postBodyComponents}
      </body>
      <script
        dangerouslySetInnerHTML={{
          __html: `
          document.getElementsByTagName('head')[0].appendChild(function(s){
            var d=document,m2g=d.createElement('script'),l=function(){Mobi2Go.load(s.container,s.ready);},jq=window.jQuery&&+window.jQuery.fn.jquery.replace(/^(\d+).*$/,'$1')===1&&+window.jQuery.fn.jquery.replace(/^\d+\.(\d+).*$/,'$1')>=7,qs=window.location.search.substring(1),re='=(.*?)(?:;|$)',c=d.cookie.match('MOBI2GO_SESSIONID'+re),w=window.innerWidth;
            m2g.src='https://www.mobi2go.com/store/embed/1990-v8oL.js?'+qs+(jq?'&no_jquery':'')+(c?'&s='+c[1]:'')+'&device_width='+w;
            if(m2g.onload!==undefined)m2g.onload=l;else m2g.onreadystatechange=function(){if(m2g.readyState!=='loaded'&&m2g.readyState!=='complete')return;m2g.onreadystatechange=null;l();}
            window.Mobi2Go_est = +(new Date);
            return m2g;
        }({
            container: 'Mobi2Go-Storefront', // Replace with ID  of the element to inject UI into
            ready: function() {} // Callback to fire when app is ready
        })););
        `,
        }}
      />
    </html>
  );
}

HTML.propTypes = {
  htmlAttributes: PropTypes.object,
  headComponents: PropTypes.array,
  bodyAttributes: PropTypes.object,
  preBodyComponents: PropTypes.array,
  body: PropTypes.string,
  postBodyComponents: PropTypes.array,
};

Upvotes: 2

Views: 10197

Answers (2)

Jamal Basha
Jamal Basha

Reputation: 498

Use the script tag inside the helmet tag. Most important your script should me inside the curly braces

<Helmet>
  <script>
    {`alert( 'Hello, world!' );`}
  </script>
 </Helmet>

Upvotes: 4

Robin M&#233;tral
Robin M&#233;tral

Reputation: 3212

It should work using react-helmet, there are examples of it in the README.

You might be missing type="text/javascript" in your script tag:

<script type="text/javascript">
  // your script goes here
</script>

Al alternative option if this doesn't work for you is to customize Gatsby's html.js.

Upvotes: 0

Related Questions