Keep the Privacy Consent cookie banner working on Cloudflare Pages deployments
Cloudflare Pages serves each deployment on a hostname of its own, and the TermsFeed Privacy Consent cookie banner keeps a separate answer on each one. The install code still goes in the <head> of the site you deploy, as your framework or static site guide shows. Cloudflare Pages adds preview hostnames, environment variables, proxy rules, and a _headers file to think about. Place the snippet first with Install TermsFeed Privacy Consent on Next.js, Install TermsFeed Privacy Consent on a Static Site, or Install TermsFeed Privacy Consent on React, Vue, or Angular.
Every Cloudflare Pages deployment gets its own hostname
Cloudflare Pages gives the production site a lasting hostname and gives previews new ones. With Git integration, Cloudflare Pages deploys every commit to a branch other than the production branch as a preview. This table shows the formats, with project standing for the name of your project.
| Deployment | Hostname |
|---|---|
| Production | Your custom domain and project.pages.dev |
| Branch preview | branch.project.pages.dev, the branch name in lowercase with other characters turned into hyphens |
| Each deployment | a1b2c3d4.project.pages.dev, a unique address per deployment |
The consent banner loads on your main website domain and on the additional domains you list, such as project.pages.dev. A preview hostname you did not add does not show the consent banner. A tester who answered on production sees the consent banner again on a listed preview, because each origin stores its own answer in localStorage. The consent log records each hostname in the domain column of the CSV.
Which hostnames to add and why branch previews are skipped
Add your custom domain, project.pages.dev, and any fixed staging branch you test on, and skip the hostnames each push creates. Enter them on the Settings page, under "What is your website domain?" and "Additional domains". One entry such as example.com covers www.example.com and every other subdomain, because the app drops the www you type.
A per-deployment hostname does not show the consent banner unless you add it, and listing one fills the list with dead addresses after the merge. Test the consent banner on your fixed staging branch instead. The number of domains on one consent banner has a limit. Add the lasting hostnames with Run One TermsFeed Privacy Consent Banner Across Several Domains.
Keep the script address in an environment variable
Store the full script address in an environment variable in the settings of your Cloudflare Pages project. Production then loads the consent banner your visitors see. A preview loads either the same address or the address of a separate test consent banner, which keeps test clicks out of the production consent log.
Cloudflare Pages keeps the variables in the Settings of the project, for the production and the preview environment. The value is the complete address from the snippet, such as https://cdn.termsfeedtag.com/plugins/pc/v1/YOUR_TOKEN/plugin.js. The build reads it, so change the value and redeploy to switch consent banners. A project deployed with Direct Upload builds on your own machine, so set the variable where you run the build.
A Next.js root layout prints the element only when the variable holds a value.
// app/layout.tsx
import type { ReactNode } from 'react';
const privacyConsentSrc = process.env.NEXT_PUBLIC_TERMSFEED_PC_SRC;
export default function RootLayout({ children }: { children: ReactNode }) {
return (
<html lang="en">
<head>
{privacyConsentSrc && (
<script type="text/javascript" src={privacyConsentSrc} async />
)}
</head>
<body>{children}</body>
</html>
);
}
An Astro layout reads the variable from import.meta.env.
---
// src/layouts/Layout.astro
const privacyConsentSrc = import.meta.env.PUBLIC_TERMSFEED_PC_SRC;
---
<html lang="en">
<head>
<meta charset="utf-8" />
{privacyConsentSrc && (
<script is:inline type="text/javascript" src={privacyConsentSrc} async></script>
)}
</head>
<body>
<slot />
</body>
</html>
A Hugo base template reads variables whose names start with HUGO_, which the default Hugo security policy allows.
{{ with os.Getenv "HUGO_TERMSFEED_PC_SRC" }}
<script type="text/javascript" src="{{ . }}" async></script>
{{ end }}
A Vite index.html replaces %VITE_...% placeholders at build time. Vite leaves a placeholder unchanged when the variable is missing, so set the variable in every context.
<script type="text/javascript" src="%VITE_TERMSFEED_PC_SRC%" async></script>
Why proxying the script through your domain breaks it
Proxying the script breaks it unless the proxy forwards every Privacy Consent path, because a copy served under the same path sends every later request to your domain. The settings, the code files, and the consent log events all follow the script address. The proxy has to pass each one through with its Origin header unchanged. A proxy that misses a path or the header makes a settings request fail, and the consent banner never appears.
A proxy has 2 more effects. The server locates a visitor from the request it receives, so a proxy makes every visitor appear to come from the proxy location. TermsFeed does not update a copy you serve yourself.
Remove any rule of this kind from the _redirects file or a Cloudflare Worker. Read why the script has its own host in What Is cdn.termsfeedtag.com?.
/plugins/* https://cdn.termsfeedtag.com/plugins/:splat 200
Set Content Security Policy headers on Cloudflare Pages
A Content Security Policy allows https://cdn.termsfeedtag.com in script-src and connect-src, and inline styles in style-src. Cloudflare Pages reads a _headers file from the static asset folder of your project, such as public/, or from the build output folder. Each block starts with a path and lists the headers below it, indented.
/*
Content-Security-Policy: script-src 'self' https://cdn.termsfeedtag.com; connect-src 'self' https://cdn.termsfeedtag.com; style-src 'self' 'unsafe-inline'
Cloudflare Pages does not apply the _headers file to a response from a Pages Function, so set the header in the function. These policies allow Privacy Consent and block every inline script, including an inline vendor tag and your own Consent Mode block. Add what your vendors need with the Content Security Policy reference.
How to verify Privacy Consent banner after a deploy
Deploy, then check the production hostname and one preview hostname, each in a new private window so neither has a stored answer. Strict, the starting consent logic, asks every visitor wherever they are. Standard in Settings passes over a visitor in a US state, so pick a test location from Cookie Banner by Visitor Location.
- Open Chrome DevTools, select the Network tab, type
termsfeedtagin the filter box, and reload. 7 requests appear with status 200, and each goes tocdn.termsfeedtag.com, not to your own domain. - Select the second
settings.jsonrequest and open Response. The response holds"serviceable": true. - Select the HTML document request and read the
content-security-policyresponse header on Headers. The header listshttps://cdn.termsfeedtag.com, and the Console tab does not show a "Refused to load" message. - Filter for
events.pushand click I Agree. A new request with status 201 appears.
A preview that loads a test consent banner shows that consent banner, which confirms the variable for the preview context. Go through the remaining checks in Check That Your TermsFeed Privacy Consent Installation Works.
Common mistakes on Cloudflare Pages
Most problems on Cloudflare Pages come from a variable missing in one environment or a header the _headers file does not reach.
- The variable exists for production only. Previews then render without the script, or a Vite build prints the placeholder as the address. Set a value for the preview environment too.
- The variable name lacks the prefix the framework reads. Vite reads names that start with
VITE_, and client code in Next.js and Astro readsNEXT_PUBLIC_andPUBLIC_names. Rename the variable. - A
_redirectsrule or a Worker proxies the script. A settings request fails, or visitors appear at the proxy location. Remove the rule. - A Pages Function serves the page. The
_headersfile does not reach its response, so the page has no Content Security Policy or the one the function sets. Set the header in the function. - 2 Content Security Policy headers reach the page. The browser enforces both, so a header from the framework and one from the
_headersfile each have to allow Privacy Consent. Keep one source. - A preview answer is expected on production. Each hostname stores its own answer, so a tester sees the consent banner again on every new hostname. Test each one in a new private window.
A hostname without a consent banner points to a missing snippet, a blocked script, or a saved answer. Diagnose it with The TermsFeed Privacy Consent Banner Does Not Appear.