Serving Static Assets with Rust WASM Workers Sites

Hey all, I recently decided to rewrite my personal website with Rust and WASM Workers Sites. Here’s the basic layout of my project:

- website
| - src
| | <Rust Code>
| - worker
| | metadata_wasm.json
| | worker.js
| Cargo.toml
| wrangler.toml

I’d like to be able to access static assets while serving webpages. This tutorial suggests adding the following to wrangler.toml, among a few other things:

bucket = "./bucket" # Add the directory with your static assets!
entry-point = "./worker" # JS folder serving your assets 

So, I’d assume that my new project layout would inlcude:

- website
| - worker
| | - bucket
| | | <Static Assets>
| | <Snip ...>
| <Snip ...>

However, when I do this, Wrangler will stop serving the Rust WASM worker and create a new one to use instead. I’ve tried quite a few different things to prevent this from happening, to no avail. Down the road, I’d like to be able to access the assets directly from Rust itself.

So, my question is twofold:

  1. How can I serve static assets from an existing WASM Worker?
  2. How can said static assets be accessed through Rust?

So, for those interested, I’ve found what I was looking for. I simply had to make a kv database with wrangler then cross-compile some wasm to interact with it from Rust.

I’m very interested. Can you post the details of your solution?

Sure! After poking around, it seems that kv namespaces are the way to go. (The source for my project can be found here, btw).

About KV

Any files other than those used to compile the WASM executable won’t be included in the package, which means that they have to be requested over database. Cloudflare’s KV is a distributed key-value database that we can use for this purpose - here’s the documentation. You’ll first need to create a namespace. You can use the web interface, or the wrangler kv:create command.

Adding KV Namespace to Project

Each namespace has a unique id. To use the namespace in your project, you need to add this to your wrangler.toml (if you haven’t already):

account_id = "..."
zone_id = "..."
kv-namespaces = [
    { binding = "Name", id = "..." },
    # ...

By the way: The tutorial seems to be incorrect, remove [site] ... from your wrangler.toml.

Your account id and your zone id can be found through Cloudflare’s dashboard. You should also make sure your Cloudflare account is connected by running wrangler whoami - if you aren’t authenticated, run wrangler config and follow the instructions.

Making the Namespace Accessible to WASM Worker

Our namespace is bound to our project, but our WASM Worker can’t access it yet. To make it accessible, we need to do two things:

  1. Declare that our WASM worker can access the namespace.
  2. Define an interface that allows us to access the namespace from the worker.

Declaring Namespace Access

To declare that our worker can access the namespace, add the following to your worker/wasm_metadata.json:

    "body_part": "script",
    "bindings": [
            "name": "wasm",
            "type": "wasm_module",
            "part": "wasmprogram"
          "name": "Name", // name of namespace
          "type": "kv_namespace",
          "namespace_id": "..."
        // ... repeat the above block to add additional namespaces

Defining an Interface

Now that our worker can access the namespace, we need to write some Rust so we can read/write from/to the namespace. We can use extern to call kv javascript functions from our Rust WASM worker. Make a new Rust file, src/, and add the following:

use wasm_bindgen::prelude::*;
use js_sys::Promise;
use wasm_bindgen_futures::JsFuture;

extern "C" {
    pub type Name; // name of namespace

    // notice these attributes!
    #[wasm_bindgen(static_method_of = Name)]
    pub fn get(key: &str, data_type: &str) -> Promise;

    #[wasm_bindgen(static_method_of = Name)]
    pub fn put(key: &str, val: &str) -> Promise;

    #[wasm_bindgen(static_method_of = Name)]
    pub fn delete(key: &str) -> Promise;

There are more functions availiable, but this allows us to get, store, and delete keys, which will realistically cover about 95% of use-cases.

Making it More Ergonomic

I also wrote a small helper function, value that awaits promises returned from the kv namespace. This is just for convinience:

// add to src/
pub async fn value(promise: Promise) -> Option<JsValue> {

Then, reading/writing/deleting to/from the kv is as simple as:

pub async fn asset(name: &str) -> String {
    // again, Name is name of namespace
    kv::value(kv::Name::get(name, "text"))

Which I personally find to be really archaic and annoying, which is why I’ve wrapped it with a function called asset.

Finally Serving Static Assets!

To serve these assets, we need to define a route endpoint in our Rust code. You should have some main function that’s bound so it’s externally accessible.

To serve static assets, we’re going to check that the request is a GET request, check that the URL path starts with, extract the name of the asset (e.g., look up the asset in our KV namespace, make a response, then return it. This may seem needlessly complex (and to some degree, it is) but it’s actually quite simple.

// in
// ... imports hidden

/// Takes an event, handles it, and returns a promise containing a response.
pub async fn main(event: FetchEvent) -> Promise {
    // get the request
    let request = event.request();

    // extract the url
    let url = match Url::parse(&request.url()) {
        Ok(v) => v,
        Err(_) => return Promise::reject(&JsValue::from("Could not parse url")),

    // get the URL path (e.g.<path>) 
    // and method (GET, POST, etc.)
    let path = url.path().to_lowercase();
    let method = request.method().to_lowercase();

    // split the path into a list of values
    // e.g. "static/file.txt" becomes ["static", "file.txt"]
    let route = path.split('/')
        .filter(|p| p != &"")
        .map(|p| p.to_string())

    // I'm using an if statement here,
    // but if you have more paths to route against, 
    // a match statement is a better idea
    if route.iter().nth(0).unwrap() == "static" {
        let file = path.iter().nth(1);
        // using the previously defined asset function to read from the ns
        let content = asset(file);
       // ... snip ... building the response
       return Promise::resolve(&JsValue::from(response));

    // ... do other things with the event as normal

Of course, the above logic doesn’t have to be in your main function - you can abstract it out somewhat. (My project, for instance, has one function that handles routing, another that fetches static assets, and yet another that builds responses). I also strongly suggest you implement a more robust error handling mechanism than just unwrapping errors.

Uploading Static Assets

You can upload static assets to serve via Cloudflare’s KV dashboard or in bulk from the command line using wrangler kv::bulk put.

If there’s anything I glossed over or would like me to expand on, let me know. Most of the code above was taken from the repository for my website, so I would check it out. I hope this helps!

By the way: I would not recommend workers for serving static assets, it’s a bit of a hassle. Workers is a bit rough around the edges in general, so unless you have a really good reason to use it, I recommend taking a more established route instead.*

1 Like