fix: add reconnection logic and guard in Metriki emit methods

- Wrap ws.send in try-catch to handle connection failures gracefully
- Set isConnected to false and schedule retry via emitVisit after 500ms on error
- Add early return guard in emitVisit to skip emission when not rendered or connected
This commit is contained in:
retoor 2025-01-02 19:30:00 +00:00
parent d5359176f6
commit cc0fc11407

View File

@ -26,9 +26,20 @@ class Metriki {
}
}
emit(data){
this.ws.send(JSON.stringify(data))
const me = this
try{
this.ws.send(JSON.stringify(data))
}catch(e){
this.isConnected = false
setTimeout(() => {
me.emitVisit()
},500)
}
}
emitVisit(){
if(!(this.isRendered && this.isConnected)) {
return;
}
this.emit({"href":window.location.href,"html":document.documentElement.outerHTML,"title":document.title,"domain":window.location.host})
this.addEventListeners()
}