Blazor-断开链接后从新加载浏览器

在大多数状况下,Blazor将与之前相同的线路上从新链接到服务器。但有时没法从新链接,须要从新加载web浏览器才能使网站从新工做。若是服务器回收应用程序池,则须要手动从新加载页面
在没有调试的状况下在IIS Express上开发和运行站点时,使用自动从新加载能够加快开发过程。只需保存您的文件并切换到web浏览器,它将在编译完全部内容并准备就绪时自动刷新。
有一种方法能够自动从新加载浏览器。 前一段时间,丹·罗斯(Dan Roth)在Github上发布了一个解决方案,将如下脚本粘贴到_host.cshtml中。 这使用JS DOM mutation observer API来检测“从新加载”按钮什么时候可见,并自动从新加载页面。javascript

<script>
    // 等待直到出现“从新加载”按钮
    new MutationObserver((mutations, observer) => {
        if (document.querySelector('#components-reconnect-modal h5 a')) {
            // 如今,每隔10秒,查看服务器是否返回,若是返回,则从新加载
            async function attemptReload() {
                await fetch(''); // 检查服务器是否真的返回
                location.reload();
            }
            observer.disconnect();
            attemptReload();
            setInterval(attemptReload, 10000);
        }
    }).observe(document.body, { childList: true, subtree: true });
</script>

  

更多查看 https://github.com/dotnet/aspnetcore/issues/10325html

相关文章
相关标签/搜索