Add a cookie consent banner to a Docusaurus site

Cookbook · platform Last verified View raw markdown (for agents)

In short: add the live snippet to headTags in docusaurus.config.js as two script entries (the options inline, then the library). Keep both tags synchronous.

Confirmed on a Docusaurus site: with headTags, the banner tags are among the first scripts in the built page's <head>.

You need #

Where it goes #

  1. Copy the live snippet (see above).

  2. In docusaurus.config.js, add both tags to headTags, keeping the order:

    const config = {
      // ...
      headTags: [
        {
          tagName: 'script',
          attributes: {},
          innerHTML: 'var huOptions = { "appID": "YOUR_APP_ID", "currentLanguage": "en", "blocking": true, "globalCookie": false };',
        },
        {
          tagName: 'script',
          attributes: { src: 'https://cdn.hu-manity.co/v2/hu-banner.min.js', type: 'text/javascript', charset: 'utf-8' },
        },
      ],
      // analytics goes in `scripts` (keep your usual gtag setup), which renders after the entries above
      scripts: ['https://www.googletagmanager.com/gtag/js?id=G-XXXX'],
    }
    
  3. Build with npm run build, serve the build, and check the page source.

Docusaurus adds one tiny inline script of its own first. The banner tags follow it, ahead of the stylesheet and the site bundle. Docusaurus minifies the inline options (appID:"YOUR_APP_ID" with unquoted keys), so check by the AppID, not by matching your pasted text.

A plain string entry in the scripts option (scripts: ['https://cdn.hu-manity.co/v2/hu-banner.min.js']) renders as a plain synchronous tag and also works, as long as the options go in headTags and the entry has no async: true. headTags for both is the simplest to read.

Keep the library tag synchronous: use headTags with no async: true. A plugin that returns the tags as preBodyTags renders them inside <body>, so use headTags there as well.

What the order controls #

The banner holds recognised trackers that load after it until the visitor chooses, in regions whose rule requires consent first. A script placed above the snippet runs straight away, and a script you mark data-hu-category="1" is treated as essential. What runs before consent is your decision, so keep analytics and pixels below the snippet.

Confirm the result #

Build or render the site, then view the page source of the output (not the browser's Elements panel): after Docusaurus's own small inline script, the huOptions script and hu-banner.min.js are the next two <script> tags in <head>, with no async or defer, and your analytics tag comes after them.

Then run the browser checks in plain-html in a private window (banner shows, no tracking cookies before the visitor chooses in a region whose rule requires consent first, trackers start after Allow) and, for the full procedure, verify-install. Test from a region your strictest rule covers (for example the EU), or confirm region rules are off.

Gotchas #

More help #