Skip to content

Commit 7e98fd3

Browse files
committed
move ws out of window.onload
1 parent 971c2ad commit 7e98fd3

File tree

4 files changed

+31
-36
lines changed

4 files changed

+31
-36
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rusty-live-server"
3-
version = "0.5.0"
3+
version = "0.5.1"
44
edition = "2021"
55

66
[dependencies]

src/routing.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,15 @@ async fn serve_file(
102102
.to_str()
103103
.unwrap_or_default()
104104
.ends_with(".html");
105-
let mut contents = Vec::new();
105+
let mut contents = fs.get_file(file_path).await?.read_to_end().await;
106106
if is_html {
107107
contents.append(
108-
&mut format!("<script>{}</script>", include_str!("updater.js"))
108+
&mut format!("<script defer>{}</script>", include_str!("updater.js"))
109109
.as_bytes()
110110
.to_vec(),
111111
)
112112
}
113113

114-
contents.append(&mut fs.get_file(file_path).await?.read_to_end().await);
115-
116114
let response = format!(
117115
"HTTP/1.1 200 OK\r\nContent-Length: {}\r\n\r\n",
118116
contents.len()

src/updater.js

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,36 @@ function getAbsoluteUrl(url) {
22
return new URL(url, window.location).href;
33
}
44

5-
window.onload = () => {
6-
const socket = new WebSocket(`ws://${window.location.host}/ws`);
7-
8-
socket.onmessage = (event) => {
9-
if (event.data === "reload") {
10-
window.location.reload();
11-
} else if (event.data.startsWith("update-css://")) {
12-
const filePath = event.data.substring(13);
13-
const links = document.getElementsByTagName("link");
14-
for (const link of links) {
15-
if (link.rel !== "stylesheet") continue;
16-
const clonedLink = link.cloneNode(true);
17-
if (getAbsoluteUrl(link.href).startsWith(getAbsoluteUrl(filePath))) {
18-
const indexOf = link.href.indexOf("?");
19-
if (indexOf !== -1 && link.href.slice(indexOf).includes("counter=")) {
20-
const url = new URL(link.href);
21-
const params = new URLSearchParams(url.search);
22-
const counter = params.get("counter");
23-
if (counter) {
24-
params.set("counter", Number.parseInt(counter) + 1);
25-
}
26-
url.search = params.toString();
27-
clonedLink.href = url.toString();
28-
} else {
29-
clonedLink.href += `${indexOf !== -1 ? "&" : "?"}counter=1`;
5+
const socket = new WebSocket(`ws://${window.location.host}/ws`);
6+
socket.onmessage = (event) => {
7+
if (event.data === "reload") {
8+
window.location.reload();
9+
} else if (event.data.startsWith("update-css://")) {
10+
const filePath = event.data.substring(13);
11+
const links = document.getElementsByTagName("link");
12+
for (const link of links) {
13+
if (link.rel !== "stylesheet") continue;
14+
const clonedLink = link.cloneNode(true);
15+
if (getAbsoluteUrl(link.href).startsWith(getAbsoluteUrl(filePath))) {
16+
const indexOf = link.href.indexOf("?");
17+
if (indexOf !== -1 && link.href.slice(indexOf).includes("counter=")) {
18+
const url = new URL(link.href);
19+
const params = new URLSearchParams(url.search);
20+
const counter = params.get("counter");
21+
if (counter) {
22+
params.set("counter", Number.parseInt(counter) + 1);
3023
}
24+
url.search = params.toString();
25+
clonedLink.href = url.toString();
26+
} else {
27+
clonedLink.href += `${indexOf !== -1 ? "&" : "?"}counter=1`;
3128
}
32-
link.replaceWith(clonedLink);
3329
}
30+
link.replaceWith(clonedLink);
3431
}
35-
};
32+
}
33+
};
3634

37-
socket.onerror = (error) => {
38-
console.error("Reload WebSocket error:", error);
39-
};
35+
socket.onerror = (error) => {
36+
console.error("Reload WebSocket error:", error);
4037
};

0 commit comments

Comments
 (0)