Skip to content

Commit

Permalink
fix(cli): use local ip address for reload (#6285)
Browse files Browse the repository at this point in the history
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
  • Loading branch information
amrbashir and lucasfernog committed Feb 16, 2023
1 parent 2dc71a4 commit 4a82da2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .changes/local-dev-path-mobile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"cli.rs": patch
"cli.js": patch
---

Fixes HMR on mobile when devPath is configured to load a filesystem path.
2 changes: 1 addition & 1 deletion tooling/cli/src/helpers/auto-reload.js
Original file line number Diff line number Diff line change
@@ -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(() => {
Expand Down
19 changes: 14 additions & 5 deletions tooling/cli/src/helpers/web_dev_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const AUTO_RELOAD_SCRIPT: &str = include_str!("./auto-reload.js");

struct State {
serve_dir: PathBuf,
address: SocketAddr,
tx: Sender<()>,
}

Expand Down Expand Up @@ -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(
"/",
Expand All @@ -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();
})
Expand Down Expand Up @@ -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);
});

Expand Down

0 comments on commit 4a82da2

Please sign in to comment.