Log out user on Last Tab Close

What I’m trying to do:
Trying to figure out a design issue.

In

code is suggested for the case where a logged-in App user has exactly one tab open, and they close it.

However, a user may trivially have multiple tabs/windows open on the same app, e.g., for working with different subsets of data.

When using local storage, all these tabs/windows will share the same storage area. (Private/incognito shares with private/incognito, regular with regular.) In effect, it is a shared cache, and can be used to keep all the tabs/windows’ data in sync with each other, as well as avoid cross-globe data accesses, to improve App performance.

Firefox will persist that data between log-ins, but Chrome and Edge will automatically clear it, when the last tab/window closes. Hence, at that time, if there are any unsaved local changes in the cache, they will need to be flushed back to the Anvil database. Otherwise, the changes will be lost.

The last available time to do this is when that App-user’s last tab/window closes. If you haven’t got around to doing it beforehand – or the user gave you no warning – then this is your last opportunity to

  1. log them out and
  2. save their work.

Is there any way to determine, client-side, when this happens? And trigger client-side code to handle it?

What I’ve tried and what’s not working:
It’s a design-time question, so I haven’t tried anything yet.

Code Sample:
None yet, as this is a design-time question.

Clone link:
Not applicable.

Just brainstorming, but what about keeping a count of open tabs in the local storage? Barring a crash keeping you from decrementing it appropriately, you should be able to track when a tab is closing that would take that count to 0.

I like that. That should work – assuming that the browser hasn’t already cleared local storage! Will likely have to run a few experiments, with the various browsers…

Some relevant links for others researching the same issues: