simulate
simulate

Reputation: 1263

How to serve multiple HTML pages using Wasm with Rust?

I am trying to build a web application running wasm on the client side, and I am wondering what would be a good way of serving multiple pages. My main concern is performance, because I would like to split the application up into contextual chunks instead of having one page with all of the content.

I am using Rust with the Yew framework for the client side, and so far I have only used yew_router to handle different routes on the client side, but they all work from the same HTML page and Wasm module. Now I would like to be able to serve different HTML pages with separate Wasm modules, and I am unsure how to realize this. Do I really have to write a dedicated crate for each page and compile those to individual Wasm modules, so I can serve them individually? Or is there some way I can compile one rust crate to multiple Wasm modules?

My current project structure looks like this:

.
├── Cargo.toml // workspace manifest
├── client
│   ├── Cargo.toml
│   ├── favicon.ico
│   ├── index.html
│   ├── main.js
│   ├── Makefile
│   ├── pkg // built using wasm-pack and rollup
│   │  ├── bundle.js
│   │  ├── client_bg.d.ts
│   │  ├── client_bg.wasm
│   │  ├── client.d.ts
│   │  ├── client.js
│   │  ├── package.json
│   │  └── snippets
│   ├── src
│   ├── statics
│   ├── styles
│   └── vendor
├── server
│   ├── Cargo.toml
│   └── src
└── ... other crates

and I run the server inside client/ where it responds with index.html to any incoming GET requests. The index.html links to the bundle.js in pkg/ which sets up the client_bg.wasm module.

Now I basically want to do this with another HTML page, with another Wasm module, preferably from the same Yew App in the client crate.

Thank you

Upvotes: 2

Views: 870

Answers (1)

MaxV
MaxV

Reputation: 2810

Currently it's impossible to generate two wasm files from one crate. The solution is to use one create per wasm file and then let another build system to put output to correct places.

Upvotes: 2

Related Questions