Skip to content

Commit

Permalink
move ws out of window.onload
Browse files Browse the repository at this point in the history
  • Loading branch information
frederik-uni committed Jun 28, 2024
1 parent 971c2ad commit 7e98fd3
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 36 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rusty-live-server"
version = "0.5.0"
version = "0.5.1"
edition = "2021"

[dependencies]
Expand Down
6 changes: 2 additions & 4 deletions src/routing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,15 @@ async fn serve_file(
.to_str()
.unwrap_or_default()
.ends_with(".html");
let mut contents = Vec::new();
let mut contents = fs.get_file(file_path).await?.read_to_end().await;
if is_html {
contents.append(
&mut format!("<script>{}</script>", include_str!("updater.js"))
&mut format!("<script defer>{}</script>", include_str!("updater.js"))
.as_bytes()
.to_vec(),
)
}

contents.append(&mut fs.get_file(file_path).await?.read_to_end().await);

let response = format!(
"HTTP/1.1 200 OK\r\nContent-Length: {}\r\n\r\n",
contents.len()
Expand Down
57 changes: 27 additions & 30 deletions src/updater.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,36 @@ function getAbsoluteUrl(url) {
return new URL(url, window.location).href;
}

window.onload = () => {
const socket = new WebSocket(`ws://${window.location.host}/ws`);

socket.onmessage = (event) => {
if (event.data === "reload") {
window.location.reload();
} else if (event.data.startsWith("update-css://")) {
const filePath = event.data.substring(13);
const links = document.getElementsByTagName("link");
for (const link of links) {
if (link.rel !== "stylesheet") continue;
const clonedLink = link.cloneNode(true);
if (getAbsoluteUrl(link.href).startsWith(getAbsoluteUrl(filePath))) {
const indexOf = link.href.indexOf("?");
if (indexOf !== -1 && link.href.slice(indexOf).includes("counter=")) {
const url = new URL(link.href);
const params = new URLSearchParams(url.search);
const counter = params.get("counter");
if (counter) {
params.set("counter", Number.parseInt(counter) + 1);
}
url.search = params.toString();
clonedLink.href = url.toString();
} else {
clonedLink.href += `${indexOf !== -1 ? "&" : "?"}counter=1`;
const socket = new WebSocket(`ws://${window.location.host}/ws`);
socket.onmessage = (event) => {
if (event.data === "reload") {
window.location.reload();
} else if (event.data.startsWith("update-css://")) {
const filePath = event.data.substring(13);
const links = document.getElementsByTagName("link");
for (const link of links) {
if (link.rel !== "stylesheet") continue;
const clonedLink = link.cloneNode(true);
if (getAbsoluteUrl(link.href).startsWith(getAbsoluteUrl(filePath))) {
const indexOf = link.href.indexOf("?");
if (indexOf !== -1 && link.href.slice(indexOf).includes("counter=")) {
const url = new URL(link.href);
const params = new URLSearchParams(url.search);
const counter = params.get("counter");
if (counter) {
params.set("counter", Number.parseInt(counter) + 1);
}
url.search = params.toString();
clonedLink.href = url.toString();
} else {
clonedLink.href += `${indexOf !== -1 ? "&" : "?"}counter=1`;
}
link.replaceWith(clonedLink);
}
link.replaceWith(clonedLink);
}
};
}
};

socket.onerror = (error) => {
console.error("Reload WebSocket error:", error);
};
socket.onerror = (error) => {
console.error("Reload WebSocket error:", error);
};

0 comments on commit 7e98fd3

Please sign in to comment.