Wednesday, 23 December 2020

Closing Chrome window not sending data with sendBeacon in unload event handler

I am trying to send data when the window closes to prevent 2 people from editing and overwriting each others data. Currently I am using a sendBeacon within a unload event handler.

FireFox:

  • Refresh: Works
  • Back button: Works
  • Close window: Works

Chrome:

  • Refresh: Works
  • Back button: Works
  • Close window: Doesn't work

Here is my code

function sendDataOnClose(edit,trans){

    var url = "../../save.php"; //This has a post request handler and works properly with other functions for saving data

    const data = JSON.stringify
    ({
      "translations": trans,
      "edit": edit
    });

    navigator.sendBeacon(url, data);
  }

function handleClose(){
    if(edit){
      console.log("sending a false when edit is: "+ edit)
      sendDataOnClose(false, translations);
    }
  }

window.addEventListener('unload', handleClose); 


from Closing Chrome window not sending data with sendBeacon in unload event handler

No comments:

Post a Comment