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 snippet still goes in the <head> of the site you deploy. 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. In these formats, project stands 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 only on the main website domain and the additional domains, such as project.pages.dev, so a preview hostname you did not add shows none. 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. List each lasting Cloudflare Pages hostname that carries the snippet under "Additional domains" on the Settings page in the TermsFeed app.
Listing a per-deployment hostname fills the list with dead addresses after the merge. Test the consent banner on your fixed staging branch instead. Follow the steps in Multiple 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.
<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. Read which requests the proxy has to forward, and how it changes visitor location and script updates, in What Is cdn.termsfeedtag.com?.
Remove any rule of this kind from the _redirects file or a Cloudflare Worker.
/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.
- Open the Chrome DevTools Network tab, filter for
termsfeedtag, 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. Run the remaining checks with Verify Your Installation.
Common mistakes on Cloudflare Pages
- 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 page then has no Content Security Policy, or only 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. 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.