+1 (415) 779-8456

Taming Third-Party Scripts on Gatsby: The Script Component, Loading Strategies, and Off-Main-Thread Options

A Gatsby site can ship almost no JavaScript of its own and still feel slow. The usual reason is third-party code: a tag manager, a chat widget, a consent banner, a heatmap tool. Each is added by a marketer with good intentions, and each competes with your pages for the main thread. This tutorial shows how to load them deliberately on Gatsby 5.

1. Stop using raw script tags

The common pattern is a <script> in gatsby-ssr.js through setHeadComponents, or in the Head API. That puts the script in the HTML head, where it blocks or races with hydration and runs before Gatsby has finished its own work. It also gives you no per-page control.

Gatsby ships a Script component, import { Script } from "gatsby", that replaces this pattern. It takes a strategy prop with three values.

2. Pick a strategy per script

  • post-hydrate loads the script after the page has hydrated. Use it for anything that needs to be present early but not before interaction, such as a consent manager.
  • idle loads the script once the browser is idle, after post-hydrate work. Use it for chat widgets, feedback buttons, and session replay.
  • off-main-thread loads the script in a web worker through Partytown. It is experimental, so test it carefully. Analytics-style scripts that only send data are the best fit.
import * as React from "react"
import { Script } from "gatsby"

export default function Layout({ children }) {
  return (
    <>
      {children}
      <Script src="https://consent.example.com/banner.js" strategy="post-hydrate" />
      <Script src="https://chat.example.com/widget.js" strategy="idle" />
    </>
  )
}

Place Script in a layout component to load it on every page, or in a single page component to load it only there. A checkout-help widget does not need to load on your blog.

3. Inline scripts and onLoad handlers

For an inline snippet, put the code inside the component as a template string and give it an id. For a script that exposes a global you need to call, use onLoad:

<Script
  src="https://chat.example.com/widget.js"
  strategy="idle"
  onLoad={() => window.ChatWidget && window.ChatWidget.init({ id: "abc123" })}
  onError={() => console.warn("chat widget failed to load")}
/>

Always handle onError. A blocked or failed third-party request should never break a page.

4. Setting up off-main-thread safely

Off-main-thread scripts run in a worker and cannot touch the DOM synchronously. Many scripts break under it. Before enabling it:

  1. Test in a production build (gatsby build && gatsby serve). Development mode behaves differently.
  2. Check that the vendor's endpoints send CORS headers, because the worker fetches them through a proxy. Gatsby's Partytown setup needs a reverse proxy for scripts that do not.
  3. Open the browser console and look for Partytown errors on every page type.
  4. Keep a rollback ready: switching the strategy back to idle is a one-line change.

If a script fails under the worker, do not spend a day debugging it. Use idle and move on.

5. Audit what each script costs

Run this process each quarter and whenever someone requests a new tag.

  1. List every third-party origin in a production page's network panel.
  2. In Chrome DevTools Performance, record a page load and a few interactions. Use the Bottom-Up view grouped by domain to see main-thread time per vendor.
  3. Compare INP and Total Blocking Time with and without each script. A local override that blocks the request is enough for this.
  4. Assign each script a strategy and an owner. Remove any script with no owner.

6. Tag managers deserve special care

A tag manager is a script that loads other scripts, so Script cannot control what is inside it. Load the container itself with post-hydrate or idle, and keep its contents under review. Many sites find that the container has quietly grown larger than the whole Gatsby bundle.

7. Guard against regressions

Add a build or CI check that fails if a new <script src> appears in gatsby-ssr.js, html.js, or a Head export. A simple grep is enough. Pair it with field INP data, as described in our Core Web Vitals playbook, so that a script added in a tag manager shows up in your numbers.

Summary

Use Script for every third-party tag, choose post-hydrate or idle by default, treat off-main-thread as an opt-in optimization you verify, and audit regularly. Most Gatsby performance problems we are called in to fix are not framework problems at all. They come from code the framework never controlled.