فهرست منبع

fix the relative paths issue in the fullstack crates.io build (#2248)

Evan Almloff 1 سال پیش
والد
کامیت
c43bbe6aa5
2فایلهای تغییر یافته به همراه34 افزوده شده و 1 حذف شده
  1. 2 0
      packages/cli/src/assets/autoreload.js
  2. 32 1
      packages/fullstack/src/render.rs

+ 2 - 0
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:";

+ 32 - 1
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#"<script>"#.as_bytes())?;
             to.write_all(disconnect_js.as_bytes())?;