How to avoid data loss, if user close the window before the data is saved and sent in the wire?

hi , I’m new to Meteor, I heard that this could be an issue if users if user close the window before the data is saved and sent in the wire ? with the latest version, is this still an issue ?

This is not a Meteor issue, but plain JavaScript. What you can do is register a window.beforeunload event handler.

WebApp.UnsavedEdits = false;
window.onbeforeunload = function(){
 if(WebApp.UnsavedEdits) {
  return "You have attempted to leave this page. Are you sure?";
 }
}

// some action makes changes
WebApp.UnsavedEdits = true;

Just, for the love of Glod, don’t make this function a popup. Because those need to die in a fire.

I have an app and do something similar, here’s a different way to approach data loss:

  • After user input key, window.last_action = 0; window.characters = $(“some_div”).count();
  • SetInterval for window.last_action++
  • If (window.last_action == 3600 && window.characters != $(“some_div”).count()) { save(); }

So what you’re doing here is comparing a change client side, waiting some time, and saving. This will keep the data up to date most of the time.

@rhywden that is how browsers implement it

Then please don’t use that unless it’s really, really important data. Everything else is positively user-hostile.

If I close the browser window I want to close the browser window.

We currently autosave data, so that isn’t an issue, but in our case, we sometimes want to cross reference the status of related docs, and alert the user of a potential conflict before they leave the page. This is an async call, so onbeforeunload doesn’t work for us.

Also, per https://www.chromestatus.com/feature/5349061406228480, you can see that even custom strings are being deprecated in most browsers, so that complicates things further, as we really need a verbose message, and that just isn’t going to happen.

We’ve tried funky stuff with the browser history, but that is also messy and still fails to provide a good UX.

Posting here to see if anyone else has creative solutions for this problem.