Search code examples
javascriptangularjsapiconceptual

Making API call before closing tab to store dashboard widgets data


There are widgets in dashboard page which be can sorted(draggable and droppable). We want the order of widgets to be preserved when user revisits our page, so we have API to dump all widgets to DB. But the problem is whether we should make an API call on every drag-drop of widget or before window close/unload? Having read around, got to know that doing API at window unload is not recommended. So is there any other approach to tackle this? I don't want to make API call on every drag-drop of widget.

Thanks in advance for help.


Solution

  • We figured out a way. Using navigator.sendBeacon() - (https://developer.mozilla.org/en-US/docs/Web/API/Navigator/sendBeacon)

    Thanks to Jaromanda X.