From 319150ea1fb39b5a5f3fd62233e458999cdd9dc5 Mon Sep 17 00:00:00 2001 From: Dillon Kearns Date: Wed, 8 Feb 2023 15:23:35 -0800 Subject: [PATCH] Show a reconnect banner for dev server and attempt to reconnect EventSource. --- generator/static-code/hmr.js | 68 ++++++++++++++++++++++++++++++++---- 1 file changed, 61 insertions(+), 7 deletions(-) diff --git a/generator/static-code/hmr.js b/generator/static-code/hmr.js index dcd254de..5dc386e9 100644 --- a/generator/static-code/hmr.js +++ b/generator/static-code/hmr.js @@ -5,15 +5,69 @@ var eventSource = null; let updateAppContentJson = new Promise((resolve, reject) => resolve(() => {})); function connect(sendContentJsonPort, initialErrorPage) { + let reconnectFrequencySeconds = 1; + // reconnect logic based on: https://stackoverflow.com/a/61148682/383983 // Listen for the server to tell us that an HMR update is available - eventSource = new EventSource("/stream"); - window.reloadOnOk = initialErrorPage; - if (initialErrorPage) { - handleEvent(sendContentJsonPort, { data: "content.dat" }); + function waitFunc() { + return reconnectFrequencySeconds * 1000; } - eventSource.onmessage = async function (evt) { - handleEvent(sendContentJsonPort, evt); - }; + function tryToSetupFunc() { + setupEventSource(); + reconnectFrequencySeconds *= 2; + if (reconnectFrequencySeconds >= 8) { + reconnectFrequencySeconds = 8; + } + } + function reconnectFunc() { + console.log( + `Attempting dev server reconnect in ${reconnectFrequencySeconds}...` + ); + setTimeout(tryToSetupFunc, waitFunc()); + } + function setupEventSource() { + eventSource = new EventSource("/stream"); + window.reloadOnOk = initialErrorPage; + + try { + if (initialErrorPage) { + handleEvent(sendContentJsonPort, { data: "content.dat" }); + } + } catch (e) {} + eventSource.onopen = async function () { + hideError(); + reconnectFrequencySeconds = 1; + }; + eventSource.onerror = async function (evt) { + eventSource && eventSource.close(); + reconnectFunc(); + + showReconnectBanner(); + }; + eventSource.onmessage = async function (evt) { + handleEvent(sendContentJsonPort, evt); + }; + } + + setupEventSource(); +} + +function showReconnectBanner() { + showError({ + type: "compile-errors", + errors: [ + { + path: "", + name: "", + problems: [ + { + title: "", + // region: "", + message: ["Dev server is disconnected..."], + }, + ], + }, + ], + }); } async function handleEvent(sendContentJsonPort, evt) {