# Add Response Compression to a Fiber App

Yes. An agent adds Fiber's compress middleware to your Go app, leaves static files as they are, proves the encoding with a handler test and curl, and opens a PR.

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

## What the agent does

The agent reads how responses leave the app. In an app laid out like gofiber/boilerplate, `go.mod` requires `github.com/gofiber/fiber/v3` v3.6.1 on Go 1.26, and `app.go` builds the whole server: `recover.New()` and `logger.New()` as middleware, a `/api/v1` group with `GET /users` and `POST /users` from `handlers/handlers.go`, `static.New("./static/public")` mounted on `GET /*` for `index.html`, `js/app.js` and the images, and a custom `NotFound` handler that sends `static/private/404.html`. Nothing sets `Content-Encoding`, no middleware looks at `Accept-Encoding`, and `klauspost/compress` sits in `go.mod` only as an indirect dependency of Fiber. The `-prod` flag turns on prefork, `Dockerfile` builds a static binary and runs it under `dumb-init`, and `.github/workflows/test.yml` runs `go test ./... -v -race` on Go 1.26 and 1.27 on Linux and Windows.

The middleware is Fiber's own, `github.com/gofiber/fiber/v3/middleware/compress`, so `go.mod` gains nothing. The agent adds `app.Use(compress.New(compress.Config{Level: compress.LevelBestSpeed}))` after `recover` and `logger` and before the group, so every JSON response and the 404 page go through it. The middleware reads `Accept-Encoding`, honours q-weights, prefers `br`, then `zstd`, `gzip` and `deflate` on a tie, leaves bodies under 200 bytes, range requests, `206` responses and anything with `Cache-Control: no-transform` alone, and compresses nothing for a `HEAD`. `LevelBestSpeed` is the choice for an API whose bodies are small JSON; the pull request names `LevelBestCompression` as the alternative for large exports.

Static files are handled separately on purpose. The `static` middleware has its own `Compress` option, which compresses files with gzip, brotli or zstd based on `Accept-Encoding` and can write compressed copies to disk, so the agent leaves `static.New` as it is and keeps the general middleware in front of it, rather than turning on both and compressing twice. It adds a `Next` function to the compress config that skips paths under `/img/`, since PNG and SVG bytes do not shrink and the logo is the largest file the app serves.

The check is a test and two requests. The agent adds `app_test.go` that first posts enough users through `handlers.UserCreate` for the list to pass the middleware's 200-byte floor, then uses `app.Test` with a request for `/api/v1/users` carrying `Accept-Encoding: gzip`, asserting `Content-Encoding: gzip` and `Vary: Accept-Encoding` on the response and the original JSON after decoding, and a second request without the header asserting no encoding. It runs `go test ./... -race`, the `golangci-lint` configuration in `.golangci.yml`, starts the app, posts a few users with `curl -d`, and uses `curl --compressed -sI` against `/api/v1/users` and `/img/logo.svg` to show one compressed and one skipped, and runs the same two requests with `-prod` to confirm nothing changes under prefork.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Add response compression to this Fiber v3 app. Read go.mod, app.go, handlers/handlers.go, the static/ tree, Dockerfile, .golangci.yml and .github/workflows/test.yml first. Add Fiber's own middleware/compress with Level set to LevelBestSpeed after the recover and logger middleware and before the /api/v1 group, with a Next function that skips paths under /img/. Leave static.New as it is and do not enable its Compress option as well. Add an app_test.go that posts enough users for the list body to exceed the middleware's 200-byte minimum, then uses app.Test to request /api/v1/users with Accept-Encoding gzip and asserts the Content-Encoding and Vary headers and the decoded JSON, and once without the header asserting no encoding. Run go test ./... -race and golangci-lint, start the app, post a few users, and show curl --compressed -sI output for /api/v1/users and /img/logo.svg, then repeat the two requests with -prod. Open a pull request that quotes the test and 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 `app.go` by one import and one `app.Use` call and adds `app_test.go`; `go.mod` and `go.sum` are unchanged because the middleware ships with Fiber. Its description quotes the `go test` output, the lint run, and four `curl -sI` exchanges: the users route with and without `Accept-Encoding`, the logo, and the users route again under prefork.

A reviewer checks the level against the size of the API's largest responses, the skip list against what the app actually serves under `/img/`, and whether any route streams or sets `Content-Encoding` itself, which the middleware already leaves alone. They may want `LevelBestCompression` for a route that returns reports, which is a second middleware instance on that group.

Clients that send `Accept-Encoding` get smaller JSON bodies with the correct `Content-Encoding` and `Vary` headers; clients that do not get the same bytes as before.

## Run it on every new Go service

A handler on the reader's own server watches for new repositories whose `go.mod` requires `github.com/gofiber/fiber/v3` and sends this body once per repository to `POST /o/{org}/sessions`, naming the repository in `resources` and a `request_id` that is a UUID v5 of the repository name so a retried call returns the run it already started. Each run reads its own `app.go`, so a service that already serves pre-compressed assets through the `static` middleware's `Compress` option gets the general middleware with those paths skipped.

An automation on the Project with a `github` trigger on `pull_request.opened` and `pull_request.synchronize` reads pull requests that touch `app.go`. An automation's run is read-only, so when a change moves the compress middleware behind the group, or adds a route that sets `Content-Encoding` by hand, it leaves a review comment with the line to move and pushes nothing.

## Why run it on Rush?

Compression is one line, and the questions around it take longer than the line: whether static files are compressed twice, whether images are wasted effort, whether prefork changes anything, whether a client that sends no header still gets what it sent before. The agent answers each with a request in its own sandbox and keeps the headers it saw with the pull request, so the reviewer reads evidence rather than reasoning about middleware order.

A fleet of small Go services gets the same request at once, each in its own sandbox, and each pull request fits that service's routes and static directory rather than a snippet copied between repositories.

## Frequently asked questions

### Why not use the static middleware's Compress option too?

It compresses files as it serves them and can write compressed copies to disk, which is a different mechanism from compressing responses on the way out. Enabling both would compress twice. The pull request keeps `static.New` unchanged and lets the general middleware handle it; switching to the static option for assets is a one-flag change if the team prefers files on disk.

### Which encoding does a client get?

Whatever `Accept-Encoding` asks for with the highest weight, among gzip, deflate, brotli and zstd. On a tie the middleware prefers brotli, then zstd, gzip and deflate. A request without the header, or with every supported encoding at weight zero, gets an uncompressed body.

### Does prefork change anything?

No. The middleware is stateless, so each forked process compresses its own responses the same way. The pull request shows the same curl headers with and without `-prod`.

### Why skip the images?

PNG and SVG bytes are already compact, so compressing them costs CPU for no saving. The `Next` function skips `/img/`; adding `/js/` would also be reasonable if the bundle were minified, and the pull request says where to extend the list.

## More Fiber jobs

- [Add CORS to a Fiber API](https://getrush.ai/run/add-cors/fiber/)
- [Add CSRF Protection to a Fiber App](https://getrush.ai/run/add-csrf-protection/fiber/)
- [Add govulncheck to CI for a Fiber Multi-Module Repository](https://getrush.ai/run/add-govulncheck-to-ci/fiber/)

## The same job on other stacks

- [Add Compression to a Koa App](https://getrush.ai/run/add-compression/koa/)
- [Add Response Compression to a Ktor API](https://getrush.ai/run/add-compression/ktor/)
- [Add Response Compression to a Leptos Axum App](https://getrush.ai/run/add-compression/leptos/)

## 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/)
