Skip to content

Commit

Permalink
fix firefox refreshing loop in debug mode (#2214)
Browse files Browse the repository at this point in the history
  • Loading branch information
ealmloff authored Apr 2, 2024
1 parent 4c209e3 commit 11bf5ae
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 91 deletions.
40 changes: 22 additions & 18 deletions packages/cli/src/assets/autoreload.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
})()
})();
47 changes: 0 additions & 47 deletions packages/fullstack/dist/index.html

This file was deleted.

2 changes: 1 addition & 1 deletion packages/fullstack/src/axum_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
26 changes: 1 addition & 25 deletions packages/fullstack/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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#"<script>"#.as_bytes())?;
to.write_all(disconnect_js.as_bytes())?;
Expand Down

0 comments on commit 11bf5ae

Please sign in to comment.