Skip to content

Commit

Permalink
Add cache busting parameters to wasm demo
Browse files Browse the repository at this point in the history
MD5 is fine for cache busting, and the repo happens to already include an implementation...
  • Loading branch information
ictrobot committed Sep 2, 2024
1 parent 8ee8650 commit b6e28f7
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 11 deletions.
6 changes: 3 additions & 3 deletions crates/aoc_wasm/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@
}
</style>
<script src="web.mjs" type="module"></script>
<link rel="modulepreload" href="aoc.mjs" />
<link rel="modulepreload" href="worker.mjs" />
<link rel="preload" href="aoc_wasm.wasm" as="fetch" crossorigin />
<link rel="modulepreload" href="./aoc.mjs" />
<link rel="modulepreload" href="./worker.mjs" />
<link rel="preload" href="./aoc.wasm" as="fetch" crossorigin />
</head>
<body class="is-flex is-flex-direction-column">
<nav class="navbar" role="navigation" aria-label="main navigation">
Expand Down
4 changes: 2 additions & 2 deletions crates/aoc_wasm/web/web.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Aoc} from "./aoc.mjs";

const module = await WebAssembly.compileStreaming(fetch("aoc_wasm.wasm"));
const worker = new Worker("worker.mjs", {type: "module"});
const module = await WebAssembly.compileStreaming(fetch("./aoc.wasm"));
const worker = new Worker("./worker.mjs", {type: "module"});
worker.postMessage(["init", module]);

const puzzles = Aoc.puzzleList(module);
Expand Down
54 changes: 48 additions & 6 deletions crates/xtask/src/cmd/web.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
use crate::common::{copy_dir, copy_file, delete_dir, repo_dir_path, run_cargo};
use crate::common::{
copy_dir, copy_file, create_dir, delete_dir, repo_dir_path, run_cargo, write_file,
};
use std::error::Error;
use std::fs;
use std::path::Path;
use std::str;
use utils::md5;

pub fn main(args: impl Iterator<Item = String>) -> Result<(), Box<dyn Error>> {
crate::ensure_no_args(args)?;
Expand All @@ -23,22 +29,58 @@ pub fn main(args: impl Iterator<Item = String>) -> Result<(), Box<dyn Error>> {
"--release",
])?;

copy_dir(
repo_dir_path().join("crates").join("aoc_wasm").join("web"),
&output,
)?;
create_dir(&output)?;

let output_wasm = output.join("aoc.wasm");
copy_file(
repo_dir_path()
.join("target")
.join("wasm32-unknown-unknown")
.join("release")
.join("aoc_wasm.wasm"),
output.join("aoc_wasm.wasm"),
&output_wasm,
)?;

let mut rewrites = vec![];
add_rewrite(&mut rewrites, &output_wasm)?;

let web = repo_dir_path().join("crates").join("aoc_wasm").join("web");
for file in ["aoc.mjs", "worker.mjs", "web.mjs", "index.html"] {
let src = web.join(file);
let dst = output.join(file);

let mut contents = fs::read_to_string(src)?;
for (from, to) in &rewrites {
let len = contents.len();
contents = contents.replace(from, to);
if len != contents.len() {
println!("rewritten {from} to {to} in {file}");
}
}
write_file(&dst, contents.as_bytes())?;

add_rewrite(&mut rewrites, &dst)?;
}

copy_dir(
repo_dir_path().join("target").join("doc"),
output.join("doc"),
)?;

Ok(())
}

fn add_rewrite(rewrites: &mut Vec<(String, String)>, path: &Path) -> Result<(), Box<dyn Error>> {
let bytes = fs::read(path)?;
let hash_hex = md5::to_hex(md5::hash(&bytes));
let hex_str = str::from_utf8(&hash_hex)?;

let Some(file_name) = path.file_name().and_then(|x| x.to_str()) else {
return Err("invalid file name".into());
};
let from = format!("\"./{file_name}\"");
let to = format!("\"./{file_name}?hash={hex_str}\"");
rewrites.push((from, to));

Ok(())
}

0 comments on commit b6e28f7

Please sign in to comment.