1. 程式人生 > 實用技巧 >Blazor-斷開連線後重新載入瀏覽器 熱更新自動重新整理

Blazor-斷開連線後重新載入瀏覽器 熱更新自動重新整理

解決方案1
<script src="_framework/blazor.server.js"></script>

//增加下面一段 自動重新整理

<script>
   Blazor.defaultReconnectionHandler._reconnectCallback = function(d) {
        document.location.reload(); 
   }
</script>
解決方案2 感覺更好點???
<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://www.cnblogs.com/bisslot/p/12563300.html https://www.it1352.com/1971746.html