From b0931475ae440ed9aa3c139caede4df13a64b03f Mon Sep 17 00:00:00 2001 From: ExploDev Date: Sat, 19 Sep 2026 04:23:35 +0200 Subject: [PATCH] =?UTF-8?q?fix(t=C3=A9l=C3=A9m=C3=A9trie):=20beacon=20en?= =?UTF-8?q?=20text/plain=20=E2=80=94=20=C3=A9vite=20le=20preflight=20CORS?= =?UTF-8?q?=20qui=20perdait=20tous=20les=20=C3=A9v=C3=A9nements?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit sendBeacon avec un Blob application/json déclenche un preflight CORS (application/json n'est pas un type « simple »). Firefox l'exécute puis abandonne le POST : sendBeacon renvoie true, le repli fetch n'est jamais atteint, et aucun événement de navigateur n'arrive au collecteur. text/plain rend la requête « simple » : plus de preflight, beacon délivré. Le collecteur ne contrôle pas le Content-Type (json.loads sur le corps). Preuve navigateur réel (page https://cborweb.com, origine du site) : - Blob application/json : preflight 204, POST jamais abouti, 0 insertion - Blob text/plain : aucun preflight, POST 200, insertion confirmée - fetch (repli) : POST 200, insertion confirmée --- docs/site/t.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/site/t.js b/docs/site/t.js index 8f43938..b0dbeeb 100644 --- a/docs/site/t.js +++ b/docs/site/t.js @@ -25,7 +25,9 @@ function flush() { if (!q.length) return; var body = JSON.stringify({ sid: sid, events: q.splice(0) }); - if (navigator.sendBeacon && navigator.sendBeacon(ENDPOINT, new Blob([body], { type: "application/json" }))) return; + // text/plain = requête « simple » au sens CORS : aucun preflight. En application/json, + // Firefox déclenche un preflight puis abandonne le beacon (sendBeacon renvoie true quand même). + if (navigator.sendBeacon && navigator.sendBeacon(ENDPOINT, new Blob([body], { type: "text/plain" }))) return; fetch(ENDPOINT, { method: "POST", headers: { "Content-Type": "application/json" }, body: body, keepalive: true }) .then(function (r) { if (!r.ok) { failed++; console.warn("[t.js] HTTP " + r.status); } }) .catch(function (e) { failed++; console.warn("[t.js] fetch error:", e.message); });