Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

155
Views
Error on Gatsby when adding a script in the body

When I'm trying to add a script on the body of Gatsby I've added the following code into the file called gatsby-ssr.js

const React = require("react")

exports.onRenderBody = ({ setPostBodyComponents }) => {
    setPostBodyComponents([
        <script type="text/javascript"> 
        {`
        (function() {
          var envolveChatWidgetScript = document.createElement('script');
          envolveChatWidgetScript.setAttribute('data-wg-publisher', '222222');
          envolveChatWidgetScript.setAttribute('data-wg-campaign', '333333');
          envolveChatWidgetScript.setAttribute('src', 'https://widget.envolvetech.com/static/js/app.js');
          envolveChatWidgetScript.setAttribute('data-identifier', 'my_identifier');
          envolveChatWidgetScript.setAttribute('data-backend', 'https://bot-dot-envolvetech-001.appspot.com');
          document.body.appendChild(envolveChatWidgetScript);
          })();
          `}
          </script>
    ]);
};

but then somehow what it's actually in the mark up is this

        <script type="text/javascript"> 
        (function() {
          var envolveChatWidgetScript = document.createElement(&#x27;script&#x27;);
          envolveChatWidgetScript.setAttribute(&#x27;data-wg-publisher&#x27;, &#x27;222222&#x27;);
          envolveChatWidgetScript.setAttribute(&#x27;data-wg-campaign&#x27;, &#x27;3333333&#x27;);
          envolveChatWidgetScript.setAttribute(&#x27;src&#x27;, &#x27;https://widget.envolvetech.com/static/js/app.js&#x27;);
          envolveChatWidgetScript.setAttribute(&#x27;data-identifier&#x27;, &#x27;my_identifier#x27;);
          envolveChatWidgetScript.setAttribute(&#x27;data-backend&#x27;, &#x27;https://bot-dot-envolvetech-001.appspot.com&#x27;);
          document.body.appendChild(envolveChatWidgetScript);
          })();
          </script>

any ideas why it gets translated into that ?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Have you tried customizing the html.js?

Run:

cp .cache/default-html.js src/html.js

Or manually copy the default-html.js (placed inside .cache) to /src

And place your script there.

When Gatsby builds your site if there's a html.js inside /src, it will use it as a boilerplate for your site

In the html.js you'll see all the postBodyComponents, onPreRenderHTML and other customizing APIs. However, in this case, you'll be inserting the raw script instead of allowing Gatsby to parse it.

about 4 years ago · Juan Pablo Isaza Report

0

After reading a few GH threads I found out that adding the script using dangerouslySetInnerHTML actually works https://github.com/gatsbyjs/gatsby/issues/11108

const React = require("react")

exports.onRenderBody = ({ setPostBodyComponents }) => {
    setPostBodyComponents([
        <script
     dangerouslySetInnerHTML={{
        __html:`
        (function() {
            var envolveChatWidgetScript = document.createElement("script");
            envolveChatWidgetScript.setAttribute("data-wg-publisher", "222222");
            envolveChatWidgetScript.setAttribute("data-wg-campaign", "333333");
            envolveChatWidgetScript.setAttribute("src","https://widget.envolvetech.com/static/js/app.js");
            envolveChatWidgetScript.setAttribute("data-identifier", "my_identifier");
            envolveChatWidgetScript.setAttribute("data-backend", "https://bot-dot-envolvetech-001.appspot.com");
            document.body.appendChild(envolveChatWidgetScript);
            })();
     `
     }}
     />,
    ]);
};

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!