Browser or Tab Close Event to notify server in ASP.NET MVC Application

Hello Fellow Developers,

I am currently working on the ASP.NET MVC application to get trigger browser or tab close event or request to notify the server so we can discard the changes on the server if the user has close the client (Browser or Tab).

Unfortunately, I have had no luck until now, I have tried JavaScript beforeunload event but this is not the requirement as this event also triggered on Refresh and some other issue regarding UX.

Furthermore, I also tried to implement with ServiceWorker Push Notification but it has some other problem as the user can not give permission for Push Notification.

Currently, I just need to notify the server when Browser or Tab is closed or about to close; is there any way to do this, with ServiceWorker or other API?