Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

159
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda