From 11bf5ae34fc52d2ec1c1a5a76d18b3ba199b20db Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Tue, 2 Apr 2024 12:52:40 -0500 Subject: [PATCH] fix firefox refreshing loop in debug mode (#2214) --- packages/cli/src/assets/autoreload.js | 40 ++++++++++++---------- packages/fullstack/dist/index.html | 47 -------------------------- packages/fullstack/src/axum_adapter.rs | 2 +- packages/fullstack/src/render.rs | 26 +------------- 4 files changed, 24 insertions(+), 91 deletions(-) delete mode 100644 packages/fullstack/dist/index.html diff --git a/packages/cli/src/assets/autoreload.js b/packages/cli/src/assets/autoreload.js index 3d7d40012..51ca89514 100644 --- a/packages/cli/src/assets/autoreload.js +++ b/packages/cli/src/assets/autoreload.js @@ -2,29 +2,33 @@ // https://github.com/DioxusLabs/dioxus/tree/master/packages/cli (function () { - var protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'; - var url = protocol + '//' + window.location.host + '/_dioxus/ws'; - var poll_interval = 8080; + var protocol = window.location.protocol === "https:" ? "wss:" : "ws:"; + var url = protocol + "//" + window.location.host + "/_dioxus/ws"; + var poll_interval = 8080; - var reload_upon_connect = () => { - window.setTimeout( - () => { - var ws = new WebSocket(url); - ws.onopen = () => window.location.reload(); - ws.onclose = reload_upon_connect; - }, - poll_interval); + var reload_upon_connect = (event) => { + // Firefox will send a 1001 code when the connection is closed because the page is reloaded + // Only firefox will trigger the onclose event when the page is reloaded manually: https://stackoverflow.com/questions/10965720/should-websocket-onclose-be-triggered-by-user-navigation-or-refresh + // We should not reload the page in this case + if (event.code === 1001) { + return; + } + window.setTimeout(() => { + var ws = new WebSocket(url); + ws.onopen = () => window.location.reload(); + ws.onclose = reload_upon_connect; + }, poll_interval); }; - var ws = new WebSocket(url); + var ws = new WebSocket(url); - ws.onmessage = (ev) => { - console.log("Received message: ", ev, ev.data); + ws.onmessage = (ev) => { + console.log("Received message: ", ev, ev.data); - if (ev.data == "reload") { - window.location.reload(); - } + if (ev.data == "reload") { + window.location.reload(); + } }; ws.onclose = reload_upon_connect; -})() +})(); diff --git a/packages/fullstack/dist/index.html b/packages/fullstack/dist/index.html deleted file mode 100644 index f7830275d..000000000 --- a/packages/fullstack/dist/index.html +++ /dev/null @@ -1,47 +0,0 @@ - - - - dioxus | ⛺ - - - - - - -
- - - - \ No newline at end of file diff --git a/packages/fullstack/src/axum_adapter.rs b/packages/fullstack/src/axum_adapter.rs index d02dc9b3d..b7dde8b95 100644 --- a/packages/fullstack/src/axum_adapter.rs +++ b/packages/fullstack/src/axum_adapter.rs @@ -285,7 +285,7 @@ where "/_dioxus", Router::new() .route( - "/disconnect", + "/ws", get(|ws: axum::extract::WebSocketUpgrade| async { ws.on_upgrade(|mut ws| async move { use axum::extract::ws::Message; diff --git a/packages/fullstack/src/render.rs b/packages/fullstack/src/render.rs index 7a46a0fbd..ba5d89c3c 100644 --- a/packages/fullstack/src/render.rs +++ b/packages/fullstack/src/render.rs @@ -263,31 +263,7 @@ impl dioxus_ssr::incremental::WrapBody for FullstackRenderer { #[cfg(all(debug_assertions, feature = "hot-reload"))] { // In debug mode, we need to add a script to the page that will reload the page if the websocket disconnects to make full recompile hot reloads work - let disconnect_js = r#"(function () { - const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'; - const url = protocol + '//' + window.location.host + '/_dioxus/disconnect'; - const poll_interval = 1000; - const reload_upon_connect = () => { - console.log('Disconnected from server. Attempting to reconnect...'); - window.setTimeout( - () => { - // Try to reconnect to the websocket - const ws = new WebSocket(url); - ws.onopen = () => { - // If we reconnect, reload the page - window.location.reload(); - } - // Otherwise, try again in a second - reload_upon_connect(); - }, - poll_interval); - }; - - // on initial page load connect to the disconnect ws - const ws = new WebSocket(url); - // if we disconnect, start polling - ws.onclose = reload_upon_connect; -})()"#; + let disconnect_js = include_str!("../../cli/src/assets/autoreload.js"); to.write_all(r#"