@@ -30,19 +30,25 @@ export function readConfig(): Config | null {
3030 }
3131}
3232
33+ function makeUrl ( config : Config , perf : number , xhr : string ) {
34+ const query = [ `v=${ version } ` , `perf=${ perf . toFixed ( ) } ` , `xhr=${ xhr } ` ] . join (
35+ '&'
36+ )
37+ return `${ config . pushURL } ?${ query } `
38+ }
39+
3340export function sendEvent ( event : Event < any , any > , config : Config ) {
3441 const tick = performance . now ( )
3542 const json = JSON . stringify ( event )
3643 const payload = encryptString ( json , config . publicKey )
3744 const tock = performance . now ( )
38- const perf = Math . round ( tock - tick ) . toFixed ( )
39- const urlWithPerf = `${ config . pushURL } ?perf=${ perf } `
45+ const perf = Math . round ( tock - tick )
4046 if ( window . localStorage . getItem ( 'chiffre:debug' ) === 'true' ) {
4147 console . dir ( {
4248 event,
4349 payload,
4450 perf,
45- urlWithPerf
51+ version
4652 } )
4753 }
4854 if ( window . localStorage . getItem ( 'chiffre:no-send' ) === 'true' ) {
@@ -54,31 +60,32 @@ export function sendEvent(event: Event<any, any>, config: Config) {
5460 }
5561
5662 if ( 'fetch' in window ) {
57- fetch ( config . pushURL , {
63+ const url = makeUrl ( config , perf , 'fetch' )
64+ fetch ( url , {
5865 method : 'POST' ,
5966 body : payload ,
6067 credentials : 'omit' ,
6168 cache : 'no-store' ,
69+ mode : 'no-cors' ,
6270 headers : {
63- 'Content-Type' : 'text/plain' ,
64- 'X-Chiffre-Perf' : perf ,
65- 'X-Chiffre-Version' : version ,
66- 'X-Chiffre-XHR' : 'fetch'
71+ 'Content-Type' : 'text/plain'
6772 }
6873 } )
6974 return true
7075 }
7176
77+ const beaconUrl = makeUrl ( config , perf , 'beacon' )
7278 if (
7379 typeof navigator . sendBeacon === 'function' &&
74- navigator . sendBeacon ( urlWithPerf , payload )
80+ navigator . sendBeacon ( beaconUrl , payload )
7581 ) {
7682 return true
7783 }
7884
7985 // Fallback to img GET
86+ const imgUrl = makeUrl ( config , perf , 'img' )
8087 const img = new Image ( )
81- img . src = `${ urlWithPerf } &payload=${ payload } `
88+ img . src = `${ imgUrl } &payload=${ payload } `
8289 return true
8390}
8491
0 commit comments