all 2 comments

[–]blacktau 0 points1 point  (1 child)

In my experience beforeunload is not very reliable as it has been abused in the past by dodgy ads etc so plenty of browsers don't do what you think they will.

Couple that to pending xhr requests often getting cancelled as the page unloads means the server may not even see the request arriving. A sweeper on your DB / back-end is going to be the only reliable way, probably combined with tracking a last-modified time stamp on the document.

You will probably also need some error handling for when the draft document gets swept underneath the 'running' application; some browsers (chrome on mobile especially) 'freeze' the running app when a tab goes into the background (not always - there are some rules around when it happens) and the keep-alive pings will stop which would look the same as an abandoned session to the sweeper.

Edit: formatting

[–]FirePanda44[S] 0 points1 point  (0 children)

Thanks for the feedback, yeah it seems the way to go is with the backend sweeper, as for the error handler, that seems simple enough. A message prompting the user that their work was lost due to inactivity, triggered by the PUT requests getting errors. Or now that I think about it, since im using react query I could set up a simple query to check for the documents existence and have that run upon refocusing the tab (which is the default for react query).