0

I need to make an api call on close of browser/tab and on change of url, I tried beforeunload, unload, popstate event, also tried Navigator.sendBeacon api. nothing seems to be working. Any input will be helpful.

Also added api call in componentWillUnmount which is not getting fired.

user2579720
  • 35
  • 2
  • 6
  • Does this answer your question? [JavaScript, browsers, window close - send an AJAX request or run a script on window closing](https://stackoverflow.com/questions/6162188/javascript-browsers-window-close-send-an-ajax-request-or-run-a-script-on-win) – Reyno Oct 15 '20 at 07:56
  • Fundamentally, even if `beforeunload` et al would be guaranteed to work 100%, there are still a ton of scenarios where that *wouldn't* work; e.g. spontaneous loss of network connection. You simply cannot rely on sending an AJAX request on page close in any way, so you shouldn't use it at all, except for some very minor unimportant things like analytics. – deceze Oct 15 '20 at 08:00
  • I need to use is for analytics only, I tried making sync call also, did not work. – user2579720 Oct 15 '20 at 08:04

0 Answers0