# Add Response Compression to a Leptos Axum App

Yes. An agent adds tower-http's CompressionLayer to the Axum router behind your Leptos app, kept on the ssr feature, checks the headers it sends, and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-compression/leptos/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

A Leptos app compiles twice, so the agent reads which target serves responses before it adds a layer. In a project generated from leptos-rs/start-axum, `Cargo.toml` depends on `leptos`, `leptos_router` and `leptos_meta` at 0.8, keeps `axum` 0.8, `leptos_axum` and `tokio` optional behind the `ssr` feature, and `wasm-bindgen` 0.2.106 and `console_error_panic_hook` behind `hydrate`. `[package.metadata.leptos]` sets `site-root` to `target/site`, `site-pkg-dir` to `pkg`, `style-file` to `style/main.scss`, `assets-dir` to `public`, `site-addr` to `127.0.0.1:3000`, `bin-features = ["ssr"]`, `lib-features = ["hydrate"]`, and `lib-profile-release` to a `wasm-release` profile with `opt-level = 'z'` and `lto`. `src/main.rs`, under `#[cfg(feature = "ssr")]`, reads `get_configuration(None)`, builds `generate_route_list(App)`, and serves `Router::new().leptos_routes(...).fallback(leptos_axum::file_and_error_handler(shell)).with_state(leptos_options)` through `axum::serve` on a `tokio` listener. `src/app.rs` renders the shell with `AutoReload`, `HydrationScripts` and `MetaTags`, a `Stylesheet` at `/pkg/<name>.css`, and a counter page. `end2end/` holds a Playwright test that opens `/` and checks the title and the `h1`.

Two facts set the design. `leptos_axum` 0.8 depends on `tower-http` 0.6, and its `file_and_error_handler` serves the site root through `ServeDir` with `precompressed_gzip()` and `precompressed_br()`, so the WebAssembly, JavaScript and CSS under `pkg/` are already sent compressed when `.gz` and `.br` files sit beside them, which `cargo leptos build --release --precompress` writes; the flag's help text says it applies to release builds only. What is never compressed is what the router renders itself: the server-side HTML of every route and the bodies of server functions, which are the responses a page waits for first. That is the gap a `CompressionLayer` closes, and it belongs on the router, not in the static file service.

The agent adds `tower-http = { version = "0.6", features = ["compression-gzip", "compression-br"], optional = true }` to `Cargo.toml`, at the same 0.6 line `leptos_axum` already pulls so no second copy is built, and adds `"dep:tower-http"` to the `ssr` feature so the `hydrate` build for the browser never sees it. In `src/main.rs` it adds `.layer(CompressionLayer::new())` after the `fallback`, so the layer wraps the Leptos routes and the file handler alike, with the import inside the `#[cfg(feature = "ssr")]` function beside the others. The layer's default predicate leaves out responses whose content type is already compressed, such as images, and event streams, and it negotiates from the request's `Accept-Encoding`, so a client that sends none gets the bytes unchanged. Nothing changes in `app.rs`, `lib.rs` or the hydrate feature.

Then the agent proves it. It installs `cargo-leptos` with `cargo install cargo-leptos --locked`, adds the `wasm32-unknown-unknown` target and runs `cargo leptos build --release --precompress`, which produces the server binary and `target/site` with precompressed assets. It starts the binary with the `LEPTOS_*` variables the README lists, and requests `/` three times with `curl -sD -`: with `Accept-Encoding: br`, with `gzip`, and with no header, reading `content-encoding` from each, then requests the `.wasm` under `/pkg/` with `br` to show the precompressed file is served by `ServeDir` rather than compressed again. It adds a test to `end2end/tests/example.spec.ts` that uses Playwright's `request` fixture to fetch `/` with `Accept-Encoding: gzip` and asserts the `content-encoding` header, and runs `cargo leptos end-to-end --release` as the README describes, along with `cargo clippy --features ssr` and `cargo fmt --check`.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Add HTTP response compression to this Leptos Axum app. Read Cargo.toml, src/main.rs, src/app.rs, src/lib.rs, README.md and end2end/tests/example.spec.ts first, and check which tower-http version leptos_axum already depends on with cargo tree. Add tower-http at that version as an optional dependency with the compression-gzip and compression-br features, add dep:tower-http to the ssr feature only, and add a CompressionLayer to the Router in src/main.rs after the fallback so it covers the rendered routes and the file handler. Do not change the hydrate feature, app.rs or the Leptos metadata. Install cargo-leptos, build with cargo leptos build --release --precompress, start the server with the LEPTOS environment variables from the README, and request / with Accept-Encoding br, with gzip and with no header, showing the content-encoding header of each, plus the wasm file under /pkg/ with br. Add a Playwright test in end2end that fetches / with Accept-Encoding gzip and asserts content-encoding, run cargo leptos end-to-end --release, cargo clippy --features ssr and cargo fmt --check, and open a pull request with the curl output.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

The pull request changes `Cargo.toml`, `src/main.rs` and `end2end/tests/example.spec.ts`, and `Cargo.lock` if the repository commits one. Its description pastes the three `curl` header blocks for `/`, the one for the `.wasm` file, and the Playwright and `clippy` output, so a reviewer sees `content-encoding: br` on a rendered page and no encoding on the request that sent no header.

The starter has no workflow, so the run's record is the proof that the release build with `--precompress`, the end-to-end test and `clippy` pass with the layer in place; a project that has added CI gets the same commands run on the pull request.

Before merging, a reviewer decides whether the service sits behind a proxy that compresses already, in which case the layer is redundant and the pull request says how to drop it, whether `compression-br` is worth its extra crate for a small app or `gzip` alone is enough, and whether server functions that return already-compressed bytes need the predicate narrowed.

## Run it on every Leptos service generated from the starter

A team that generates each service with `cargo leptos new --git https://github.com/leptos-rs/start-axum` starts one run per repository from its own server: a `POST /o/{org}/sessions` each, with this prompt as `input` and that repository in `resources`. Each run reads its own `Cargo.toml`, so a service that chose the `nightly` placeholder keeps its `rust-toolchain.toml`, a service on a newer `tower-http` line gets the version `cargo tree` shows, and one that already wraps its router in a `ServiceBuilder` gets the layer added there.

A team that tracks performance work in Linear runs the same prompt from an automation in the Project with a `linear` trigger on issues labelled `perf`. The trigger adds the issue after the prompt, so an issue that names the route whose HTML is largest, or asks for gzip only, reaches the agent with that detail, and the pull request links back to it.

## Why run it on Rush?

The proof for this change is a set of response headers from a release build, and a Leptos release build means a WebAssembly target, `cargo-leptos`, `dart-sass` for the stylesheet and a Playwright install, none of which belong on a laptop that is doing something else. The agent sets all of that up in its own sandbox, keeps every `curl` header block with the run, and the pull request shows the compressed and uncompressed responses side by side.

The same prompt runs across every Leptos service at once, each in its own sandbox and with its own pull request, and each build log stays with its run for the reviewer who wants to see the `cargo tree` line that fixed the `tower-http` version.

## Frequently asked questions

### Does cargo-leptos already compress anything?

Static assets, when asked. `cargo leptos build --release --precompress` writes `.gz` and `.br` files beside the WebAssembly, JavaScript and CSS under `target/site/pkg`, and `leptos_axum`'s file handler serves them through `ServeDir` with `precompressed_gzip` and `precompressed_br`. The HTML the routes render and server function responses are not covered by that, which is what the layer adds.

### Why keep tower-http behind the ssr feature?

The crate is compiled twice: once as the server binary with `ssr` and once as a WebAssembly library with `hydrate`. `tower-http` has no business in the browser build and would fail to compile there, so it is optional and listed as `dep:tower-http` in the `ssr` feature, beside `axum` and `tokio`, which the template already keeps the same way.

### Which responses are left alone?

The layer's default predicate skips responses whose content type is an image or an event stream, and anything below its minimum size, and it compresses only when the request's `Accept-Encoding` names an encoding the layer has a feature for. A `curl` without the header gets the plain body, which the pull request shows.

### Should the proxy compress instead?

If a reverse proxy in front of the service already compresses, the layer only costs CPU, and the pull request says to remove the one line. For a binary that answers clients directly, as the starter's `axum::serve` on `127.0.0.1:3000` does, the layer is the only place compression can happen.

### Does the layer's position in the Router matter?

Yes. A layer added with `.layer()` applies to the routes registered before it, so it goes after `leptos_routes` and after the `fallback`, which is why the agent places it last before `with_state`. Put before the routes it would wrap nothing.

## More Leptos jobs

- [Add a Dark Mode Toggle to a Leptos App](https://getrush.ai/run/add-dark-mode/leptos/)
- [Add a Dockerfile to a Leptos Axum App](https://getrush.ai/run/add-dockerfile/leptos/)
- [Add GitHub Actions CI to a Leptos App](https://getrush.ai/run/add-github-actions-ci/leptos/)

## The same job on other stacks

- [Add Response Compression to a Litestar App](https://getrush.ai/run/add-compression/litestar/)
- [Add Response Compression to an Actix Web App from a Linear Issue](https://getrush.ai/run/add-compression/actix/)
- [Add Response Compression to an Axum API](https://getrush.ai/run/add-compression/axum/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
