devtools.rs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. //! Handler code for hotreloading.
  2. //!
  3. //! This sets up a websocket connection to the devserver and handles messages from it.
  4. //! We also set up a little recursive timer that will attempt to reconnect if the connection is lost.
  5. use std::fmt::Display;
  6. use std::rc::Rc;
  7. use std::time::Duration;
  8. use dioxus_core::prelude::RuntimeGuard;
  9. use dioxus_core::{Runtime, ScopeId};
  10. use dioxus_devtools::{DevserverMsg, HotReloadMsg};
  11. use dioxus_document::eval;
  12. use futures_channel::mpsc::{unbounded, UnboundedReceiver, UnboundedSender};
  13. use js_sys::JsString;
  14. use wasm_bindgen::JsCast;
  15. use wasm_bindgen::{closure::Closure, JsValue};
  16. use web_sys::{window, CloseEvent, MessageEvent, WebSocket};
  17. const POLL_INTERVAL_MIN: i32 = 250;
  18. const POLL_INTERVAL_MAX: i32 = 4000;
  19. const POLL_INTERVAL_SCALE_FACTOR: i32 = 2;
  20. /// Amount of time that toats should be displayed.
  21. const TOAST_TIMEOUT: Duration = Duration::from_secs(5);
  22. const TOAST_TIMEOUT_LONG: Duration = Duration::from_secs(3600); // Duration::MAX is too long for JS.
  23. pub(crate) fn init(runtime: Rc<Runtime>) -> UnboundedReceiver<HotReloadMsg> {
  24. // Create the tx/rx pair that we'll use for the top-level future in the dioxus loop
  25. let (tx, rx) = unbounded();
  26. // Wire up the websocket to the devserver
  27. make_ws(runtime, tx.clone(), POLL_INTERVAL_MIN, false);
  28. playground(tx);
  29. rx
  30. }
  31. fn make_ws(
  32. runtime: Rc<Runtime>,
  33. tx: UnboundedSender<HotReloadMsg>,
  34. poll_interval: i32,
  35. reload: bool,
  36. ) {
  37. // Get the location of the devserver, using the current location plus the /_dioxus path
  38. // The idea here being that the devserver is always located on the /_dioxus behind a proxy
  39. let location = web_sys::window().unwrap().location();
  40. let url = format!(
  41. "{protocol}//{host}/_dioxus",
  42. protocol = match location.protocol().unwrap() {
  43. prot if prot == "https:" => "wss:",
  44. _ => "ws:",
  45. },
  46. host = location.host().unwrap(),
  47. );
  48. let ws = WebSocket::new(&url).unwrap();
  49. // Set the onmessage handler to bounce messages off to the main dioxus loop
  50. let tx_ = tx.clone();
  51. let runtime_ = runtime.clone();
  52. ws.set_onmessage(Some(
  53. Closure::<dyn FnMut(MessageEvent)>::new(move |e: MessageEvent| {
  54. let Ok(text) = e.data().dyn_into::<JsString>() else {
  55. return;
  56. };
  57. // The devserver messages have some &'static strs in them, so we need to leak the source string
  58. let string: String = text.into();
  59. // let leaked: &'static str = Box::leak(Box::new(string));
  60. match serde_json::from_str::<DevserverMsg>(&string) {
  61. Ok(DevserverMsg::HotReload(hr)) => _ = tx_.unbounded_send(hr),
  62. // todo: we want to throw a screen here that shows the user that the devserver has disconnected
  63. // Would be nice to do that with dioxus itself or some html/css
  64. // But if the dev server shutsdown we don't want to be super aggressive about it... let's
  65. // play with other devservers to see how they handle this
  66. Ok(DevserverMsg::Shutdown) => {
  67. web_sys::console::error_1(&"Connection to the devserver was closed".into())
  68. }
  69. // The devserver is telling us that it started a full rebuild. This does not mean that it is ready.
  70. Ok(DevserverMsg::FullReloadStart) => show_toast(
  71. runtime_.clone(),
  72. "Your app is being rebuilt.",
  73. "A non-hot-reloadable change occurred and we must rebuild.",
  74. ToastLevel::Info,
  75. TOAST_TIMEOUT_LONG,
  76. false,
  77. ),
  78. // The devserver is telling us that the full rebuild failed.
  79. Ok(DevserverMsg::FullReloadFailed) => show_toast(
  80. runtime_.clone(),
  81. "Oops! The build failed.",
  82. "We tried to rebuild your app, but something went wrong.",
  83. ToastLevel::Error,
  84. TOAST_TIMEOUT_LONG,
  85. false,
  86. ),
  87. // The devserver is telling us to reload the whole page
  88. Ok(DevserverMsg::FullReloadCommand) => {
  89. show_toast(
  90. runtime_.clone(),
  91. "Successfully rebuilt.",
  92. "Your app was rebuilt successfully and without error.",
  93. ToastLevel::Success,
  94. TOAST_TIMEOUT,
  95. true,
  96. );
  97. window().unwrap().location().reload().unwrap()
  98. }
  99. Err(e) => web_sys::console::error_1(
  100. &format!("Error parsing devserver message: {}", e).into(),
  101. ),
  102. }
  103. })
  104. .into_js_value()
  105. .as_ref()
  106. .unchecked_ref(),
  107. ));
  108. // Set the onclose handler to reload the page if the connection is closed
  109. ws.set_onclose(Some(
  110. Closure::<dyn FnMut(CloseEvent)>::new(move |e: CloseEvent| {
  111. // Firefox will send a 1001 code when the connection is closed because the page is reloaded
  112. // 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
  113. // We should not reload the page in this case
  114. if e.code() == 1001 {
  115. return;
  116. }
  117. // set timeout to reload the page in timeout_ms
  118. let tx = tx.clone();
  119. let runtime = runtime.clone();
  120. web_sys::window()
  121. .unwrap()
  122. .set_timeout_with_callback_and_timeout_and_arguments_0(
  123. Closure::<dyn FnMut()>::new(move || {
  124. make_ws(
  125. runtime.clone(),
  126. tx.clone(),
  127. POLL_INTERVAL_MAX.min(poll_interval * POLL_INTERVAL_SCALE_FACTOR),
  128. true,
  129. );
  130. })
  131. .into_js_value()
  132. .as_ref()
  133. .unchecked_ref(),
  134. poll_interval,
  135. )
  136. .unwrap();
  137. })
  138. .into_js_value()
  139. .as_ref()
  140. .unchecked_ref(),
  141. ));
  142. // Set the onopen handler to reload the page if the connection is closed
  143. ws.set_onopen(Some(
  144. Closure::<dyn FnMut(MessageEvent)>::new(move |_evt| {
  145. if reload {
  146. window().unwrap().location().reload().unwrap()
  147. }
  148. })
  149. .into_js_value()
  150. .as_ref()
  151. .unchecked_ref(),
  152. ));
  153. // monkey patch our console.log / console.error to send the logs to the websocket
  154. // this will let us see the logs in the devserver!
  155. // We only do this if we're not reloading the page, since that will cause duplicate monkey patches
  156. if !reload {
  157. // the method we need to patch:
  158. // https://developer.mozilla.org/en-US/docs/Web/API/Console/log
  159. // log, info, warn, error, debug
  160. let ws: &JsValue = ws.as_ref();
  161. dioxus_interpreter_js::minimal_bindings::monkeyPatchConsole(ws.clone());
  162. }
  163. }
  164. /// Represents what color the toast should have.
  165. enum ToastLevel {
  166. /// Green
  167. Success,
  168. /// Blue
  169. Info,
  170. /// Red
  171. Error,
  172. }
  173. impl Display for ToastLevel {
  174. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  175. match self {
  176. ToastLevel::Success => write!(f, "success"),
  177. ToastLevel::Info => write!(f, "info"),
  178. ToastLevel::Error => write!(f, "error"),
  179. }
  180. }
  181. }
  182. /// Displays a toast to the developer.
  183. fn show_toast(
  184. runtime: Rc<Runtime>,
  185. header_text: &str,
  186. message: &str,
  187. level: ToastLevel,
  188. duration: Duration,
  189. after_reload: bool,
  190. ) {
  191. let as_ms = duration.as_millis();
  192. let js_fn_name = match after_reload {
  193. true => "scheduleDXToast",
  194. false => "showDXToast",
  195. };
  196. // Create the guard before running eval which uses the global runtime context
  197. let _guard = RuntimeGuard::new(runtime);
  198. ScopeId::ROOT.in_runtime(|| {
  199. eval(&format!(
  200. r#"
  201. if (typeof {js_fn_name} !== "undefined") {{
  202. {js_fn_name}("{header_text}", "{message}", "{level}", {as_ms});
  203. }}
  204. "#,
  205. ));
  206. });
  207. }
  208. /// Force a hotreload of the assets on this page by walking them and changing their URLs to include
  209. /// some extra entropy.
  210. ///
  211. /// This should... mostly work.
  212. pub(crate) fn invalidate_browser_asset_cache() {
  213. // it might be triggering a reload of assets
  214. // invalidate all the stylesheets on the page
  215. let links = web_sys::window()
  216. .unwrap()
  217. .document()
  218. .unwrap()
  219. .query_selector_all("link[rel=stylesheet]")
  220. .unwrap();
  221. let noise = js_sys::Math::random();
  222. for x in 0..links.length() {
  223. use wasm_bindgen::JsCast;
  224. let link: web_sys::Element = links.get(x).unwrap().unchecked_into();
  225. if let Some(href) = link.get_attribute("href") {
  226. let (url, query) = href.split_once('?').unwrap_or((&href, ""));
  227. let mut query_params: Vec<&str> = query.split('&').collect();
  228. // Remove the old force reload param
  229. query_params.retain(|param| !param.starts_with("dx_force_reload="));
  230. // Add the new force reload param
  231. let force_reload = format!("dx_force_reload={noise}");
  232. query_params.push(&force_reload);
  233. // Rejoin the query
  234. let query = query_params.join("&");
  235. _ = link.set_attribute("href", &format!("{url}?{query}"));
  236. }
  237. }
  238. }
  239. /// Initialize required devtools for dioxus-playground.
  240. ///
  241. /// This listens for window message events from other Windows (such as window.top when this is running in an iframe).
  242. fn playground(tx: UnboundedSender<HotReloadMsg>) {
  243. let window = web_sys::window().expect("this code should be running in a web context");
  244. let binding = Closure::<dyn FnMut(MessageEvent)>::new(move |e: MessageEvent| {
  245. let Ok(text) = e.data().dyn_into::<JsString>() else {
  246. return;
  247. };
  248. let string: String = text.into();
  249. let Ok(hr_msg) = serde_json::from_str::<HotReloadMsg>(&string) else {
  250. return;
  251. };
  252. _ = tx.unbounded_send(hr_msg);
  253. });
  254. let callback = binding.as_ref().unchecked_ref();
  255. window
  256. .add_event_listener_with_callback("message", callback)
  257. .expect("event listener should be added successfully");
  258. binding.forget();
  259. }