---
title: "Gatsby"
description: "Add the CookieBeam loader to every Gatsby page with onRenderBody in gatsby-ssr, keep it first in the head, and read consent with a hook."
url: "https://cookiebeam.com/docs/install/frameworks/gatsby"
lang: "en"
last_updated: "2026-11-12"
---

# Gatsby

Add the CookieBeam loader to every Gatsby page with onRenderBody in gatsby-ssr, keep it first in the head, and read consent with a hook.

Gatsby builds the HTML of every page from React, so the loader is added in `gatsby-ssr.js` (or `.tsx`) with the `onRenderBody` API. Read [Install in a web framework](https://cookiebeam.com/docs/install/frameworks) first for the rules every framework shares.

## Add the loader

```tsx
// gatsby-ssr.tsx
import * as React from 'react';
import type { GatsbySSR } from 'gatsby';

const COOKIEBEAM_LOADER =
  'https://cdn.cookiebeam.com/banner/PUBLIC_BANNER_ID/default/loader.js';

export const onRenderBody: GatsbySSR['onRenderBody'] = ({ setHeadComponents }) => {
  setHeadComponents([<script key="cookiebeam" src={COOKIEBEAM_LOADER} />]);
};
```

This adds a synchronous `<script>` to the `<head>` of every page Gatsby builds. Gatsby's own bundles load at the end of the page, so they run after the loader.

Plugins and the Head API can add their own head tags, some of which are scripts. To make sure CookieBeam comes first, move it to the front with `onPreRenderHTML` in the same file:

```tsx
// gatsby-ssr.tsx (continued)
export const onPreRenderHTML: GatsbySSR['onPreRenderHTML'] = ({
  getHeadComponents,
  replaceHeadComponents,
}) => {
  const head = getHeadComponents() as React.ReactElement[];
  const cookiebeam = head.filter((el) => el?.key === 'cookiebeam');
  const others = head.filter((el) => el?.key !== 'cookiebeam');
  replaceHeadComponents([...cookiebeam, ...others]);
};
```

## Route changes

Gatsby's `<Link>` and `navigate` change pages without reloading the document. The loader runs once per full page load, and the runtime, banner and consent stay in place across pages.

## Read consent with a hook

Wrap the `hasConsent` and `onConsentChange` helpers from [Read consent in your code](https://cookiebeam.com/docs/install/frameworks#read-consent-in-your-code):

```tsx
// src/hooks/use-consent.ts
import { useEffect, useState } from 'react';
import { hasConsent, onConsentChange } from '../consent';

export function useConsent(category: string): boolean {
  // false during the build and the first client render, so hydration matches.
  const [granted, setGranted] = useState(false);

  useEffect(() => {
    const update = () => setGranted(hasConsent(category));
    update();
    return onConsentChange(update);
  }, [category]);

  return granted;
}
```

## Gate a component on consent

```tsx
import { useConsent } from '../hooks/use-consent';

export function MarketingVideo() {
  return useConsent('marketing') ? <VideoEmbed /> : null;
}
```

Gatsby analytics plugins (like `gatsby-plugin-google-gtag`) write hard-coded `<script src>` tags into the static HTML at build time. The browser starts fetching those before any JavaScript can intercept them, so [automatic blocking](https://cookiebeam.com/docs/configure/blocking#automatic-blocking) can't hold them back. Remove the plugin and load the tag one of three ways: through Google Tag Manager with [Consent Mode](https://cookiebeam.com/docs/integrations/google-consent-mode); as a `<script type="text/plain" data-category="analytics" data-src="...">` placeholder emitted from `gatsby-ssr.js` `onRenderBody`; or from a component gated with the `useConsent` hook above.

For the cookie settings link, use a plain button with `data-cb="show-preferencesModal"`, not Gatsby's `<Link>`. See [Add a cookie settings link](https://cookiebeam.com/docs/install/frameworks#add-a-cookie-settings-link).

## Content security policy

If your host sends a `Content-Security-Policy` header, see [Content security policy](https://cookiebeam.com/docs/install/content-security-policy). Gatsby builds static HTML with no per-request nonce, so list the CookieBeam hosts in the policy.

## Check the installation

1. Build and serve the site (`gatsby build` and `gatsby serve`), not `gatsby develop`, whose HTML differs from the build. View the page source and confirm the loader is the first script in `<head>`, with no `async` or `defer`.
2. Add `localhost` or your preview hostname under **Banner domains** and publish, or the banner won't appear there.
3. Navigate with links between pages. The Network panel shows a single `loader.js` request.
4. Reject, then accept, a category and confirm gated components follow the choice without a reload.

Then work through [Verify your installation](https://cookiebeam.com/docs/getting-started/verify-installation).
