26

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?

Nkosi
  • 235,767
  • 35
  • 427
  • 472
baer999
  • 810
  • 2
  • 14
  • 26

5 Answers5

27

You can try this code:

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

<script>
   Blazor.defaultReconnectionHandler._reconnectCallback = function(d) {
        document.location.reload(); 
   }
</script>
Sergius Sizykh
  • 394
  • 3
  • 2
23
<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

Dan Friedman
  • 4,941
  • 2
  • 41
  • 65
  • 3
    My server is behind a proxy server, so this would cause the reload to show the proxy error page. I added "if (response.ok)" just before the location.reload() and that fixes it - the proxy reports an error, and the code keeps retrying because it wasn't a 2xx response. – mj2008 Feb 19 '21 at 10:06
  • That solution is working correct for me. You can also hide the reload if you change the querySelector to only '#components-reconnect-modal' and make a custom css for the #components-reconnect-modal with display: none !important; In that way the reload is nearly imperceptible to the user. – Aljenci Jul 26 '22 at 14:10
8

For .NET 6 & 7 you can use:

<script src="_framework/blazor.server.js" autostart="false"></script>
<script>
    Blazor.start().then(() => {
        Blazor.defaultReconnectionHandler._reconnectCallback = function (d) {
            document.location.reload();
        }
    });
</script>

This keeps all of the original startup process as is and just adds a page reload on connection down, without needing a mutations observer.

aryeh
  • 610
  • 5
  • 13
0

Here's an alternative but I'm not sure it works 100%.

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

<script>
    Blazor.start().then(() => {
        Blazor.defaultReconnectionHandler._reconnectionDisplay = {
            show: () => {},
            update: (d) => {},
            rejected: (d) => document.location.reload()
        };
    });
</script>
Hugo A.
  • 218
  • 2
  • 12
-2

One trick some people forget about is that you can actually "watch" your code base for changes, if you open your favorite terminal and run dotnet run watch debug in the same folder as your cproj file it should watch your changes so when you refresh your browser it should pick up any changes to your application, formore information read: https://learn.microsoft.com/en-us/aspnet/core/tutorials/dotnet-watch?view=aspnetcore-3.1

dotnet watch is a tool that runs a .NET Core CLI command when source files change. For example, a file change can trigger compilation, test execution, or deployment.

Hope this helps

Mark Davies
  • 1,447
  • 1
  • 15
  • 30