Automatically reconnect Blazor Server application without loosing data?

Viewed 1285

We have a blazor server application in production and the issue is that users usually are filling forms while spending time on other browser tabs, and they leave the blazor server tab open for hours. When they come back to the tab, the connection to the server is lost and they have to reload the page. The issue is that at refresh they loose the data that they added in the application inputs which creates a bad user experience.

I looked into this question but is seems that the solution involves page reloading.

I there any way I can automatically reconnect to the server without refreshing the page ?

2 Answers

One solution for this would be to use the browsers storage, either the session or local storage (I'm pretty sure session storage would be lost on disconnect though I'm not 100% sure) here is Microsoft's documentation on browser storage.

Whenever they fill something on the form out you push it to the browser's storage and if for whatever reason they lose connection and reconnect you check their browser for any data and pull it.

Just remember that if you use local storage they can open multiple tabs and have access to the same data.

From what I've seen the usual recommendation is to use Blazored Local Storage.

Can also use cookies to accomplish the same effect as well.

I don't think so. You wouldn't want the server to be maintaining the state of dead pages indefinitely.

If these users can be identified (for example by logging them in), then it's trivial to keep state. You can use a SQL database to store a JSON string of the current state of the input model, for example. Or you can use a singleton service with data keyed to the user.

Related