core/middleware

Compress

Compression middleware for Fiber that automatically compresses responses with gzip, deflate, brotli, or zstd based on the client's Accept-Encoding header.

Note

Bodies smaller than 200 bytes remain uncompressed because compression would likely increase their size and waste CPU cycles. See the fasthttp source.

Behavior

  • Skips compression for responses that already define Content-Encoding, for range requests, 206 responses, status codes without bodies, text/event-stream responses, or when either side sends Cache-Control: no-transform. fasthttp holds a streamed body's encoder until the stream ends, megabytes for zstd, and an event stream ends only once the client is noticed gone; exclude other long-lived streams with Next. A request that repeats Cache-Control on several lines is read as one list (RFC 9110 Β§5.3), so the directive counts on any of them.
  • The encoding is negotiated from the Accept-Encoding header with its weights, wildcard and list syntax honored (RFC 9110 Β§12.5.3): the coding the client weighs highest wins, and among equal weights the middleware prefers br, then zstd, gzip and deflate. An element with no q parameter, or an unparsable one, carries the default weight of 1. A request that repeats Accept-Encoding on several lines is read as one comma-separated list (RFC 9110 Β§5.3). A request without the header, or one that gives every supported coding a weight of 0, is not compressed. The client's header is left untouched for later handlers.
  • HEAD requests are not compressed; only Accept-Encoding is merged into Vary.
  • When compression runs, strong ETag values are recomputed from the compressed bytes; a streamed body cannot be hashed without buffering it, so its strong ETag becomes a weak one (W/). On every response the middleware handles, compressed or not, Accept-Encoding is merged into Vary unless the header holds * or already names it, in any letter case. Several Vary lines count as one list (RFC 9110 Β§5.3).
  • Request-body decompression is still handled by Fiber's request APIs (for example c.Body()), and those decoders enforce the app BodyLimit for compressed payloads.

Signatures

func New(config ...Config) fiber.Handler

Examples

Import the middleware package:

import (
    "github.com/gofiber/fiber/v3"
    "github.com/gofiber/fiber/v3/middleware/compress"
)

Once your Fiber app is initialized, use the middleware like this:

// Initialize default config
app.Use(compress.New())

// Or extend your config for customization
app.Use(compress.New(compress.Config{
    Level: compress.LevelBestSpeed, // 1
}))

// Skip middleware for specific routes
app.Use(compress.New(compress.Config{
    Next:  func(c fiber.Ctx) bool {
      return c.Path() == "/dont_compress"
    },
    Level: compress.LevelBestSpeed, // 1
}))

Config

PropertyTypeDescriptionDefault
Nextfunc(fiber.Ctx) boolSkips this middleware when the function returns true.nil
LevelLevelCompression level to use.LevelDefault (0)

Possible values for the "Level" field are:

  • LevelDisabled (-1): Compression is disabled.
  • LevelDefault (0): Default compression level.
  • LevelBestSpeed (1): Best compression speed.
  • LevelBestCompression (2): Best compression.

Default Config

var ConfigDefault = Config{
    Next:  nil,
    Level: LevelDefault,
}

Constants

// Compression levels
const (
    LevelDisabled        Level = -1
    LevelDefault         Level = 0
    LevelBestSpeed       Level = 1
    LevelBestCompression Level = 2
)