A partial archive of discourse.wicg.io as of Saturday February 24, 2024.

Detect end of session to sync data with server

collimarco
2018-12-16

In order to sync local data with server, it would be useful to have an event that is triggered when the user leaves / closes a website. It is already possible to detect when a user leaves a page using the Page Visibility API, however it is not possible to detect the end of session (e.g. expiration of sessionStorage).

This event would allow developers to save data changes locally and collect website analytics (e.g. performance, tracking, user preferences) inside sessionStorage (or localStorage) and then send the data to the server only once, at the end of the visit, instead of creating an additional HTTP request for each page view.

Garbee
2019-01-07

If you need it to persist between sessions, use Local Storage. If it doesn’t need to persist, then that’s when you use Session Storage. And if you want the server to track changes, that’s where you use cookies. Trying to have the server store a copy of the session storage doesn’t make much sense. Session storage is intended to be short-lived, so storing data in there needed between them isn’t using the proper technology for a task.

What exactly is the use-case you’re trying to achieve here? In general these techs exist for their intended goals and they work very well.

You also don’t need to send a request for each page view using local storage. You can send a request to update the server data only when a value is modified. That means regardless of when/how it happens, if the value changes the server will know. No need to handle it in a session ending event.