0

I consider creating a webapp. The webapp must be capable of working offline and syncing the data when connection is available, so I think I should use localStorage.

However, I am concerned about users that open the app in multiple tabs multiple times. I believe this has no reasonable use case, but I fear of parallel access to a single localStorage and race conditions arising from that.

The easiest approach seems to be to implement run once behavior, so when the user tries to (rather accidentally) open the app second time, they get a warning or they get ideally switched to the existing app. I know this is not holly grail of UX, but it might be a reasonable tradeoff between UX and development costs. However, I am not sure how to properly implement it. Maybe I could use window names, but this look much like a hack that can easily break.

Alternatively, I am thinking of a more advanced variant that allows multiple instances of the same page, but:

  • it syncs the local copy of data across them and
  • it ensures exactly one instance of background sync

This looks like a task for ServiceWorkers, but their compatibility is limited. I am mostly concerned about iOS, which seems to have some fresh support for them in beta version.

I am not sure which approach to choose and how to properly implement it. I'll probably take the approach that looks easier, unless it is an ugly hack or there is some major drawback.

Some context on this: It is an internal app that is expected to be used by few users on their own devices. So, I expect various devices, mostly Android and iOS. It might be also used on a laptop. The app is just a simplified domain-specific interface for easy data entrance into some Google Sheet, which works as a storage backend. We can educate the users of the app a bit (that's the reason why I consider run once restriction as acceptable), but we should not expect any IT skills from them. So I do not want to rely on user not opening the webapp twice.

v6ak
  • 1,636
  • 2
  • 12
  • 27
  • what if only the tab with the focus (document.hasFocus()) is allowed to sync? – Stef Chäser Mar 22 '18 at 12:04
  • @StefChäser Maybe I could, but it is not enough. Consider following scenario: Sync starts. You switch to another tab. Sync starts on the other tab without stopping in the original one. – v6ak Mar 24 '18 at 15:28
  • 1
    you are right, this gonna be a problem. what if the syncing tab has first to set a flag in localStorage that its starting the sync. after each record it has synced it checks if it is still the active tab, if not it removes the flag from localStorage. Only now the second tab is allowed to sync. Still this solution has some edge cases which would not work, what if a user closes the TAB during sync? Looks like SharedWebWorkers or ServiceWorkers would be the best thing for it, unfortunately, as you said, they are not yet supported widely. – Stef Chäser Mar 26 '18 at 00:11
  • @StefChäser Yes, I am concerned about those edge cases. After all, I'll probably give ServiceWorkers a try. I've read an guess that iOS with ServiceWorkers (11.3) will be there probably in March/April. The app will be used in September, so there is probably enough time. Incompatibility with IE/Edge is not ideal, but it is probably acceptable for our use case. – v6ak Mar 26 '18 at 06:17

0 Answers0