Skip to content

fix(traffic): send the tracking beacon as text/plain so it isn't CORS-blocked - #287

Merged
gkhngyk merged 1 commit into
mainfrom
fix/traffic-beacon-cors
Jun 22, 2026
Merged

gkhngyk merged 1 commit into
mainfrom
fix/traffic-beacon-cors

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

Summary

AI-traffic events were never reaching the backend in the browser: the snippet fired, but no rows landed in ai_traffic_logs. A page opened with ?utm_source=chatgpt produced a CORS error in the console, while a direct curl to the same endpoint returned 204 and inserted a row — proving the server + DB were healthy and the failure was purely browser-side.

Root cause. t.js sent the payload via navigator.sendBeacon(url, new Blob([body], { type: 'application/json' })). application/json is not a CORS-safelisted content type, so the cross-origin request (www.ansvisor.com → api.ansvisor.com) requires a preflight — but sendBeacon requests cannot be preflighted, so the browser silently blocks the beacon.

Fix.

  • server/src/public/t.js: send the beacon (and the fetch fallback) as text/plain;charset=UTF-8, a CORS-safelisted content type that needs no preflight.
  • server/src/routes/traffic.js: the /track/:trackingCode route now reads the body as text and JSON.parses it. application/json is still accepted (older cached t.js, direct API callers) — the global json parser handles those and the route-level text parser no-ops once a body is already parsed.

Verified locally that both text/plain and application/json POSTs parse and insert (200/204). All 93 server tests still pass.

Related issue

None — reported live (traffic dashboard showing no data).

Type of change

  • feat — New feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor
  • test

How to test

  1. Deploy the server (so the new t.js is served and the /track route accepts text/plain). Note t.js has a 1-hour cache, so installed sites pick up the new snippet within an hour (or hard-refresh).
  2. Open an instrumented page with ?utm_source=chatgpt (forces AI-referral detection). DevTools → Network → filter track: the beacon to api.ansvisor.com/track/<code> should succeed with no CORS error.
  3. A row appears in ai_traffic_logs for that brand; the Traffic dashboard (last 7 days) shows the visit.
  4. Backward-compat: a direct curl POST with Content-Type: application/json still returns 204 and inserts.

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/)
  • Commits follow Conventional Commits
  • yarn lint (web) — n/a, server-only change
  • yarn typecheck (web) — n/a
  • yarn format:check (web) — n/a
  • Changes are focused — one concern per PR

Server-only change (server/src/...). node --check + npx prettier --check pass; server test suite green (93/93); local integration test confirms both content types parse.

…-blocked

navigator.sendBeacon was sending the payload as a Blob with Content-Type
application/json. application/json is not a CORS-safelisted content type, so the
request needs a preflight — but beacons can't preflight, so browsers silently
block the cross-origin beacon (www.ansvisor.com -> api.ansvisor.com). The
snippet fired and the server was healthy, but no event ever reached the DB; a
direct curl (no CORS) worked, confirming it was purely browser-side.

t.js now sends the beacon (and the fetch fallback) as text/plain;charset=UTF-8,
a CORS-safelisted type that needs no preflight. The /track route reads the body
as text and JSON-parses it; application/json is still accepted for older cached
t.js and direct API callers (the global json parser handles those, and the
route-level text parser no-ops once a body is already parsed).

Verified locally: both text/plain and application/json POSTs parse and insert.
@gkhngyk
gkhngyk merged commit 9812592 into main Jun 22, 2026
4 checks passed
@gkhngyk
gkhngyk deleted the fix/traffic-beacon-cors branch June 22, 2026 09:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant