From c43bbe6aa501ad6cff4df49bf885468db5e3565d Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Thu, 4 Apr 2024 20:30:14 -0500 Subject: [PATCH] fix the relative paths issue in the fullstack crates.io build (#2248) --- packages/cli/src/assets/autoreload.js | 2 ++ packages/fullstack/src/render.rs | 33 ++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/packages/cli/src/assets/autoreload.js b/packages/cli/src/assets/autoreload.js index 51ca89514..9fc98a791 100644 --- a/packages/cli/src/assets/autoreload.js +++ b/packages/cli/src/assets/autoreload.js @@ -1,5 +1,7 @@ // Dioxus-CLI // https://github.com/DioxusLabs/dioxus/tree/master/packages/cli +// NOTE: This is also used in the fullstack package at ../packages/fullstack/src/render.rs, if you make changes here, make sure to update the version in there as well +// TODO: Extract hot reloading with axum into a separate crate or use the fullstack hot reloading Axum extension trait here (function () { var protocol = window.location.protocol === "https:" ? "wss:" : "ws:"; diff --git a/packages/fullstack/src/render.rs b/packages/fullstack/src/render.rs index ba5d89c3c..edcfaf549 100644 --- a/packages/fullstack/src/render.rs +++ b/packages/fullstack/src/render.rs @@ -263,7 +263,38 @@ 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 = include_str!("../../cli/src/assets/autoreload.js"); + // This is copied from the Dioxus-CLI package at ../packages/cli + let disconnect_js = r#"(function () { + var protocol = window.location.protocol === "https:" ? "wss:" : "ws:"; + var url = protocol + "//" + window.location.host + "/_dioxus/ws"; + var poll_interval = 8080; + + 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); + + ws.onmessage = (ev) => { + console.log("Received message: ", ev, ev.data); + + if (ev.data == "reload") { + window.location.reload(); + } + }; + + ws.onclose = reload_upon_connect; + })();"#; to.write_all(r#"