From 18a356715389a5106d5728f173ff536fc88162c6 Mon Sep 17 00:00:00 2001 From: Francois Best Date: Wed, 15 Apr 2020 08:31:54 +0200 Subject: [PATCH] fix: Drop custom headers They would require a CORS preflight, which would take too long and drop some session:end events. All metadata is now passed as query string arguments. --- src/index.ts | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/src/index.ts b/src/index.ts index 6930fbc..5e6b4f3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -30,19 +30,25 @@ export function readConfig(): Config | null { } } +function makeUrl(config: Config, perf: number, xhr: string) { + const query = [`v=${version}`, `perf=${perf.toFixed()}`, `xhr=${xhr}`].join( + '&' + ) + return `${config.pushURL}?${query}` +} + export function sendEvent(event: Event, config: Config) { const tick = performance.now() const json = JSON.stringify(event) const payload = encryptString(json, config.publicKey) const tock = performance.now() - const perf = Math.round(tock - tick).toFixed() - const urlWithPerf = `${config.pushURL}?perf=${perf}` + const perf = Math.round(tock - tick) if (window.localStorage.getItem('chiffre:debug') === 'true') { console.dir({ event, payload, perf, - urlWithPerf + version }) } if (window.localStorage.getItem('chiffre:no-send') === 'true') { @@ -54,31 +60,32 @@ export function sendEvent(event: Event, config: Config) { } if ('fetch' in window) { - fetch(config.pushURL, { + const url = makeUrl(config, perf, 'fetch') + fetch(url, { method: 'POST', body: payload, credentials: 'omit', cache: 'no-store', + mode: 'no-cors', headers: { - 'Content-Type': 'text/plain', - 'X-Chiffre-Perf': perf, - 'X-Chiffre-Version': version, - 'X-Chiffre-XHR': 'fetch' + 'Content-Type': 'text/plain' } }) return true } + const beaconUrl = makeUrl(config, perf, 'beacon') if ( typeof navigator.sendBeacon === 'function' && - navigator.sendBeacon(urlWithPerf, payload) + navigator.sendBeacon(beaconUrl, payload) ) { return true } // Fallback to img GET + const imgUrl = makeUrl(config, perf, 'img') const img = new Image() - img.src = `${urlWithPerf}&payload=${payload}` + img.src = `${imgUrl}&payload=${payload}` return true }