Cloudflare Workers, Pages & Zaraz
Add AffilFinder to a site behind Cloudflare — inject the widget from a Worker, drop it into a Pages project, or fire it from Zaraz. Server-trigger the decline widget from a Worker.
On this page
Cloudflare is three different products that all solve this differently. This recipe covers the three paths we see most:
- Workers — inject the widget into every HTML response at the edge, using
HTMLRewriter. Zero changes to your origin. - Pages — a static or SSR project hosted on Cloudflare Pages. Drop the tag into your template.
- Zaraz — tag-manager style; paste the same one-liner into a Custom HTML tag.
If you use Cloudflare Workers for an advertiser-side backend (e.g. KYC or payment), skip to Server-triggered decline from a Worker.
YOUR_PUBLIC_KEY placeholders.Sign in to pre-fill with your real keys →Option 1 — Inject from a Worker with HTMLRewriter
Use this when your origin is yours-to-break. The Worker sits in front, gets the HTML back, and streams a rewritten response with our tag appended to <head>. Nothing on the origin changes.
Wire it up with wrangler.toml:
Deploy:
Bot-only exclusions
If you want to skip the widget for known bots (SEO, monitoring, health-checks) — keep the origin HTML clean for them — gate the rewrite on the User-Agent:
Option 2 — Cloudflare Pages
If your site is hosted on Cloudflare Pages (static, Next.js, Astro, Nuxt, Remix, SvelteKit…), the widget is just a <script> tag in your layout/template. Same tag the dashboard shows:
For framework-specific placement (Next.js App Router, etc.), see the Next.js recipe.
Option 3 — Cloudflare Zaraz
Zaraz is Cloudflare's built-in tag manager. Pasting our tag is the same short story as Google Tag Manager, covered in full here: GTM recipe.
Short version for Zaraz:
Open Zaraz → Tools → Add a tool → Custom HTML
Pick Custom HTML as the tool type.
Paste the tag
Drop the exact same snippet shown in Option 2. Set the trigger to Page view (all pages).
Publish
Zaraz deploys the tag to your zone immediately — no Save → Publish cycle like GTM.
Server-triggered decline from a Worker
If you're running advertiser-side logic in a Worker (for example, a KYC check that denies a user), you can open the decline overlay on the user's page without calling back through your origin.
The two endpoints you need:
/v1/widget-decline/session/v1/widget-decline/triggerThe client-side widget-decline.js emits the session ID when it mounts. Capture it and forward with your Worker request. Full client-side setup: Decline widget integration.
Cache considerations
If your Pages project or Worker sits behind Cloudflare's cache:
- The widget is a static JS file served from
https://cdn.affilfinder.com/widget.jswith a shortCache-Control. You don't need to cache it yourself. - The API (
api.affilfinder.com) responses setCache-Control: no-store— decisions are per-request. Don't addCache-Rulesthat cache them. - Your origin HTML caches as normal — the
HTMLRewriterinjection from Option 1 re-runs on every HTML response, but sincewidget.jsitself is a tiny external script, there's no duplicate-content concern.
Verify
Visit your site with DevTools open
Check the Network tab for
widget.jsand aGET /v1/decisioncall toapi.affilfinder.com. Both should be 200.Check a blocked country
On
localhostadddata-dev-country="US"to the injected script tag to simulate a blocked visitor (see Debugging). On production, spin up a quick VPN test.Confirm events in the dashboard
Open Websites → your site → Analytics. A successful page-load produces a
snippet-ping; a blocked user produces animpressionper card. If nothing shows after 2 minutes, revisit Debugging.
Related
Need more help?
Can't find what you're looking for? Our team responds within one business day.