Repository navigation
fix(traffic): send the tracking beacon as text/plain so it isn't CORS-blocked - #287
Merged
Merged
Conversation
…-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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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=chatgptproduced a CORS error in the console, while a directcurlto the same endpoint returned204and inserted a row — proving the server + DB were healthy and the failure was purely browser-side.Root cause.
t.jssent the payload vianavigator.sendBeacon(url, new Blob([body], { type: 'application/json' })).application/jsonis not a CORS-safelisted content type, so the cross-origin request (www.ansvisor.com→api.ansvisor.com) requires a preflight — butsendBeaconrequests cannot be preflighted, so the browser silently blocks the beacon.Fix.
server/src/public/t.js: send the beacon (and thefetchfallback) astext/plain;charset=UTF-8, a CORS-safelisted content type that needs no preflight.server/src/routes/traffic.js: the/track/:trackingCoderoute now reads the body as text andJSON.parses it.application/jsonis still accepted (older cachedt.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/plainandapplication/jsonPOSTs 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 featurefix— Bug fixchore— Maintenance / dependenciesdocs— Documentation onlyrefactortestHow to test
t.jsis served and the/trackroute accepts text/plain). Notet.jshas a 1-hour cache, so installed sites pick up the new snippet within an hour (or hard-refresh).?utm_source=chatgpt(forces AI-referral detection). DevTools → Network → filtertrack: the beacon toapi.ansvisor.com/track/<code>should succeed with no CORS error.ai_traffic_logsfor that brand; the Traffic dashboard (last 7 days) shows the visit.curlPOST withContent-Type: application/jsonstill returns204and inserts.Checklist
feature/,fix/,chore/,docs/)yarn lint(web) — n/a, server-only changeyarn typecheck(web) — n/ayarn format:check(web) — n/a