fix(cli): use local ip address for reload (#6285)

Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
This commit is contained in:
Amr Bashir
2023-02-16 19:59:56 +02:00
committed by GitHub
parent 2dc71a4793
commit 4a82da2919
3 changed files with 21 additions and 6 deletions

View File

@@ -0,0 +1,6 @@
---
"cli.rs": patch
"cli.js": patch
---
Fixes HMR on mobile when devPath is configured to load a filesystem path.

View File

@@ -1,7 +1,7 @@
// taken from https://github.com/thedodd/trunk/blob/5c799dc35f1f1d8f8d3d30c8723cbb761a9b6a08/src/autoreload.js
;(function () {
var url = 'ws:' + '//' + window.location.host + '/_tauri-cli/ws'
var url = '{{reload_url}}'
var poll_interval = 5000
var reload_upon_connect = () => {
window.setTimeout(() => {

View File

@@ -23,6 +23,7 @@ const AUTO_RELOAD_SCRIPT: &str = include_str!("./auto-reload.js");
struct State {
serve_dir: PathBuf,
address: SocketAddr,
tx: Sender<()>,
}
@@ -60,8 +61,12 @@ pub fn start_dev_server<P: AsRef<Path>>(address: SocketAddr, path: P) {
}
});
let state = Arc::new(State { serve_dir, tx });
let router = Router::new()
let state = Arc::new(State {
serve_dir,
tx,
address,
});
let server_router = Router::new()
.fallback(
Router::new().nest(
"/",
@@ -73,13 +78,14 @@ pub fn start_dev_server<P: AsRef<Path>>(address: SocketAddr, path: P) {
),
)
.route(
"/_tauri-cli/ws",
"/__tauri_cli",
get(move |ws: WebSocketUpgrade| async move {
ws.on_upgrade(|socket| async move { ws_handler(socket, state).await })
}),
);
Server::bind(&address)
.serve(router.into_make_service())
.serve(server_router.into_make_service())
.await
.unwrap();
})
@@ -120,7 +126,10 @@ async fn handler<T>(req: Request<T>, state: Arc<State>) -> impl IntoResponse {
with_html_head(&mut document, |head| {
let script_el =
NodeRef::new_element(QualName::new(None, ns!(html), "script".into()), None);
script_el.append(NodeRef::new_text(AUTO_RELOAD_SCRIPT));
script_el.append(NodeRef::new_text(AUTO_RELOAD_SCRIPT.replace(
"{{reload_url}}",
&format!("ws://{}/__tauri_cli", state.address),
)));
head.prepend(script_el);
});