Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto reconnect Blazor Serverside

Blazor serverside (dotnet core 3.1)

I run into the problem that on customer side this is shown:

Could not reconnect to the server. Reload the page to restore functionality.

Each time I update the code base or internet is broken or something like this.

Now the goal is that it should reload the page as soon as the server is back again (or in some interval).

Is there any possibility that could help me?

like image 537
baer999 Avatar asked Jan 17 '20 00:01

baer999


2 Answers

You can try this code:

<script src="_framework/blazor.server.js"></script>

<script>
   Blazor.defaultReconnectionHandler._reconnectCallback = function(d) {
        document.location.reload(); 
   }
</script>
like image 94
Sergius Sizykh Avatar answered Nov 11 '22 02:11

Sergius Sizykh


<script>
    // Wait until a 'reload' button appears
    new MutationObserver((mutations, observer) => {
        if (document.querySelector('#components-reconnect-modal h5 a')) {
            // Now every 10 seconds, see if the server appears to be back, and if so, reload
            async function attemptReload() {
                await fetch(''); // Check the server really is back
                location.reload();
            }
            observer.disconnect();
            attemptReload();
            setInterval(attemptReload, 10000);
        }
    }).observe(document.body, { childList: true, subtree: true });
</script>

This will wait until the reload button appears and then will wait until the server is back up before actually reloading.

From https://github.com/dotnet/aspnetcore/issues/10325#issuecomment-537979717

like image 20
Dan Friedman Avatar answered Nov 11 '22 03:11

Dan Friedman