Skip to content

Web UI (WebAssembly) Applications

Rust compiles to WebAssembly (WASM) so you can build browser user interfaces with type safety and shared domain crates. Configure the framework, build toolchain, binary size limits, Content Security Policy (CSP), and browser tests before deployment.

Framework selection

Select a framework that matches your reactivity model, server-side rendering (SSR) needs, and target platforms.

Framework Model Build tool Best fit
Leptos Fine-grained reactive signals without a Virtual DOM cargo-leptos or trunk High-performance web applications, SSR with hydration, and full-stack Rust
Dioxus Virtual DOM with rsx! components dx (Dioxus CLI) Multi-platform products that share UI code across web, desktop, and mobile
Yew Component and hook model trunk Mature single-page applications (SPAs) with stable APIs

Put shared request types, response types, and validation rules in a separate workspace crate. Compile that crate for both the browser (wasm32-unknown-unknown) and the web API server so client and server models stay consistent.

Toolchain and build pipeline

Add the WebAssembly compilation target to your toolchain and to rust-toolchain.toml.

rust-toolchain.toml
[toolchain]
channel = "1.100.0"
components = ["rustfmt", "clippy"]
targets = ["wasm32-unknown-unknown"]

Use these standard tools to compile and package the frontend:

  • wasm-bindgen: Generates the JavaScript glue code and bindings to Web APIs (web-sys, js-sys). Pin the wasm-bindgen-cli version in CI to match the wasm-bindgen crate version in Cargo.lock.
  • Trunk: Builds, bundles, hashes, and serves client-side WASM applications from an index.html entry point.
  • wasm-pack: Compiles Rust crates into reusable npm packages for JavaScript or TypeScript projects.
  • cargo-leptos: Compiles both the server binary and the client WASM bundle for full-stack Leptos applications.

Binary size and performance optimization

Large .wasm bundles slow page load times. Configure a dedicated release profile in Cargo.toml for WASM artifacts.

[profile.wasm-release]
inherits = "release"
opt-level = "z"
lto = true
codegen-units = 1
panic = "abort"
strip = true

Select the custom profile explicitly in your build tool so Cargo applies these settings instead of the default release profile:

  • cargo-leptos: Set lib-profile-release = "wasm-release" in [package.metadata.leptos] (single-package project) or in [[workspace.metadata.leptos]] (multi-package workspace with separate frontend and server crates).
  • Cargo: Pass --profile wasm-release.
  • Trunk: Pass trunk build --cargo-profile wasm-release, or set data-cargo-profile="wasm-release" on the rust <link> in index.html.

Apply these additional size controls:

  • Post-process release .wasm files with wasm-opt from Binaryen (-Oz for smallest size or -O3 for speed). Trunk and wasm-pack can invoke wasm-opt automatically.
  • Keep console_error_panic_hook for readable browser panic messages during development. Avoid large formatting dependencies where size is critical.
  • Serve .wasm assets with the Content-Type: application/wasm header so browsers can use WebAssembly.instantiateStreaming.
  • Compress static .wasm, .js, and .css files with Brotli or gzip on the web server or content delivery network (CDN).

Security controls

Browser code operates in an untrusted environment. Apply browser security headers and keep all secrets on the server.

Content Security Policy (CSP)

Modern browsers support 'wasm-unsafe-eval' in the script-src directive. This directive permits WebAssembly compilation without permitting JavaScript eval().

Content-Security-Policy: default-src 'self'; script-src 'self' 'wasm-unsafe-eval'; object-src 'none'; base-uri 'self'; frame-ancestors 'none';
  • Do not use 'unsafe-eval' in script-src when 'wasm-unsafe-eval' satisfies your browser support policy.
  • Do not embed API secrets, private keys, or privileged tokens in a WASM binary. Any user can inspect or disassemble downloaded .wasm bytes.
  • Treat raw HTML injection APIs (such as inner_html) as high-risk sinks. Sanitize untrusted HTML on the server before rendering.
  • Use Subresource Integrity (SRI) hashes when you load third-party scripts or stylesheets.

Testing WebAssembly code

Combine native Rust tests with headless browser tests.

  1. Keep pure state logic, reducers, and validators independent of web-sys. Check them on the host target with cargo nextest run --locked. Refer to Testing strategy.
  2. Write browser-specific tests with wasm-bindgen-test for DOM manipulation, storage APIs, and fetch wrappers.
  3. Start browser tests in CI with a headless browser driver:
Terminal window
wasm-pack test --headless --chrome
  1. Add end-to-end tests with Playwright against the production bundle served with the real CSP headers. Make sure that the page loads without console errors or CSP violations.

Deployment and caching

  • Include content hashes in .wasm, .js, and .css filenames. Trunk enables file hashing by default. cargo-leptos defaults hash-files to false; set hash-files = true in [package.metadata.leptos] (or [[workspace.metadata.leptos]] for multi-package workspaces), enable LEPTOS_HASH_FILES=true / hash_files at runtime, and deploy the generated hash.txt alongside the server executable (or configure its runtime path) before you apply an immutable cache policy.
  • Set Cache-Control: public, max-age=31536000, immutable on hashed asset files.
  • Set Cache-Control: no-cache on index.html so browsers get new asset hashes after a release.
  • Track .wasm bundle size in CI so unexpected dependency growth fails review.

Further reading