diff --git a/src/http_layer/server.rs b/src/http_layer/server.rs index 8c43935..a90c522 100644 --- a/src/http_layer/server.rs +++ b/src/http_layer/server.rs @@ -42,7 +42,6 @@ pub(crate) struct AppState { pub(crate) index_listing: bool, pub(crate) tx: Arc>, pub(crate) root: PathBuf, - pub(crate) addr: String, } impl Default for Options { @@ -154,8 +153,7 @@ async fn static_assets( let status_code = match err.kind() { ErrorKind::NotFound => { if state.index_listing && is_accessing_dir { - let script = - format_script(&state.addr, state.hard_reload, is_reload, false); + let script = format_script(state.hard_reload, is_reload, false); let html = format!( include_str!("../templates/index.html"), uri_path, @@ -170,7 +168,7 @@ async fn static_assets( _ => StatusCode::INTERNAL_SERVER_ERROR, }; if mime == "text/html" { - let script = format_script(&state.addr, state.hard_reload, is_reload, true); + let script = format_script(state.hard_reload, is_reload, true); let html = format!(include_str!("../templates/error.html"), script, err); let body = Body::from(html); @@ -190,7 +188,7 @@ async fn static_assets( return (StatusCode::INTERNAL_SERVER_ERROR, headers, body); } }; - let script = format_script(&state.addr, state.hard_reload, is_reload, false); + let script = format_script(state.hard_reload, is_reload, false); file = format!("{text}{script}").into_bytes(); } else if state.hard_reload { // allow client to cache assets for a smoother reload. @@ -205,7 +203,7 @@ async fn static_assets( } /// Inject the address into the websocket script and wrap it in a script tag -fn format_script(addr: &str, hard_reload: bool, is_reload: bool, is_error: bool) -> String { +fn format_script(hard_reload: bool, is_reload: bool, is_error: bool) -> String { match (is_reload, is_error) { // successful reload, inject the reload payload (true, false) => format!("", RELOAD_PAYLOAD), @@ -214,10 +212,7 @@ fn format_script(addr: &str, hard_reload: bool, is_reload: bool, is_error: bool) // normal connection, inject the websocket client _ => { let hard = if hard_reload { "true" } else { "false" }; - format!( - r#""#, - WEBSOCKET_FUNCTION, addr, hard - ) + format!(r#""#) } } } diff --git a/src/lib.rs b/src/lib.rs index d15e2a5..9f5e63f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -64,7 +64,6 @@ impl Listener { index_listing: options.index_listing, tx: arc_tx.clone(), root: self.root_path.clone(), - addr: self.link().unwrap()[7..].to_string(), }; let watcher_future = tokio::spawn(watch(self.root_path, self.debouncer, self.rx, arc_tx)); diff --git a/src/templates/websocket.js b/src/templates/websocket.js index b9f8f6e..a887b3a 100644 --- a/src/templates/websocket.js +++ b/src/templates/websocket.js @@ -1,5 +1,6 @@ -(async (addr, hard) => { - addr = `ws://${addr}/live-server-ws`; +(async (hard) => { + const wsProtocol = location.protocol === 'https:' ? 'wss:' : 'ws:'; + const addr = `${wsProtocol}//${location.host}/live-server-ws`; const sleep = (x) => new Promise((r) => setTimeout(r, x)); const preload = async (url, requireSuccess) => { const resp = await fetch(url, { cache: "reload" }); // reset cache diff --git a/tests/test.rs b/tests/test.rs index 853107a..872b55c 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -20,10 +20,9 @@ async fn request() { let text = response.text().await.unwrap().replace("\r\n", "\n"); let target_text = format!( - r#"{}"#, + r#"{}"#, include_str!("./page/index.html"), include_str!("../src/templates/websocket.js"), - HOST ) .replace("\r\n", "\n"); assert_eq!(text, target_text);