Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

66
Vistas
adding a script on Gatsby with setPostBodyComponents only on certain pages

Is it possible to render a script that has been inserted within the body tag (using setPostBodyComponents) only in certain pages other than all of them ?

Any ideas if this would be possible ?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

As you can see in gatsby-ssr-js docs, onRenderBody exposes a bunch of props where there's a pathname.

pathname {string}

The pathname of the page currently being rendered.

That said, you can try something like:

const React = require("react")

exports.onRenderBody = ({ setPostBodyComponents, pathname }) => {
   const SCRIPT_PAGES = ['/page-1', '/page-2'];

   if(SCRIPT_PAGES.includes(pathname)){
      setPostBodyComponents([
        <script dangerouslySetInnerHTML={{ __html:`your script here`}} />,
    ]);
  }
};

In that case, your SCRIPT_PAGES will hold the pages where the script will be inserted. Using that approach, you can tweak it as you want.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda