Cookie Banner Injector Worker
Uses Cloudflare HTMLRewriter to inject a GDPR-compliant cookie consent banner into any HTML response. No origin code changes needed. Configurable styling.
GDPR-Compliant Cookie Consent on Any Site Without Touching the Code
A Cloudflare Worker that uses Cloudflare's HTMLRewriter API to inject a fully styled, configurable cookie consent banner into every HTML response from your origin. Works on any site — no framework, no JavaScript bundle changes required.
What's Included
- Cloudflare Worker using Cloudflare's HTMLRewriter for streaming HTML injection
- Pre-built cookie consent banner with Accept/Decline buttons
- Configurable banner text, button labels, and link to your privacy policy
- CSS-in-JS injection — no separate stylesheet files or CDN dependencies
- Consent stored in a cookie — banner hidden on return visits
- Optional GTM / analytics activation on consent (configurable)
- README with customization and styling guide
How It Works
The Worker intercepts HTML responses and uses HTMLRewriter's streaming transformer to append the banner markup and inline styles to the <body> element before bytes reach the browser. On first visit, the banner appears. When the user consents or declines, the choice is saved in a cookie and the Worker skips injection on all subsequent requests from that browser.
What You Get
- Drop-in GDPR compliance on any origin without deploying code changes
- Works on static sites, legacy apps, and dynamically rendered pages
- Configurable colours and copy to match your brand
- No third-party consent management platform (CMP) subscription required
- Streaming injection — no visible layout shift or FOUC
Requirements
- Cloudflare account (free tier works)
- Domain on Cloudflare DNS
- Wrangler CLI installed (
npm install -g wrangler)