Caching WebR from CDN
Notes from developing webr-js-rs
I am developing Rust bindings to WebR. Because WebR is not compiled for WASI and only WebAssembly, native Rust bindings are not possible. Instead, bindings are done through wasm-bindgen which creates bindings to JavaScript.
The WIP Rust crate is called webr-js-rs
.
I’m building these bindings to support flrsh.dev.
Sign up for an account to be notified when I launch our first course (a deep dive on DuckDB)!
webr-js-rs
works only on wasm targets.
- See this informative blog post from Mozilla on what WASI is.
- There is an outstanding issue on the WebR GitHub.
The problem I was encountering:
WebR wasn’t caching the binaries!
Turns out that this is because my code had this:
#[wasm_bindgen(module = "https://webr.r-wasm.org/latest/webr.mjs")]
I brought this issue up in a GitHub issue. @GeorgeStagg pointed out
“The webR CDN assets under /latest/ are intentionally served with Cache-Control: no-cache so that the latest commit is always downloaded by the browser.”
This makes sense! It means since there is no cache instruction, it will fetch the binaries every time! Instead he recommended to use a tagged version
“The longer-term builds under e.g. /v0.3.3/ are served with the HTTP header cache-control: max-age=604800, and so the webR assets should automatically be cached by browsers for 1 week.”
👆🏼 emphasis mine.
This works! So I’ve changed webr-js-rs to use a fixed version.
The one challenge with this, though, is that even though the binaries are cached, the R session will be restarted from scratch if the browser is refreshed. So that is something I need to figure out next!