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

65
Views
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 answers
Answer question

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 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!