Compress

Compression middleware for Fiber that supports gzip, deflate and brotli compression depending on the Accept-Encoding header.

Getting Started

Import the compress package that is shipped with the Fiber web framework

  1. import (
  2. "github.com/gofiber/fiber/v2"
  3. "github.com/gofiber/fiber/v2/middleware/compress"
  4. )

You can pass an optional Config struct to create a new middleware handler.

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

The following configurations are possible

  1. type Config struct {
  2. // Next defines a function to skip this middleware when returned true.
  3. //
  4. // Optional. Default: nil
  5. Next func(c *fiber.Ctx) bool
  6. // CompressLevel determines the compression algoritm
  7. //
  8. // Optional. Default: LevelDefault
  9. // LevelDisabled: -1
  10. // LevelDefault: 0
  11. // LevelBestSpeed: 1
  12. // LevelBestCompression: 2
  13. Level int
  14. }
  1. // Default compression config
  2. app.Use(compress.New())
  3. // Provide a custom compression level
  4. app.Use(compress.New(compress.Config{
  5. Level: compress.LevelBestSpeed, // 1
  6. }))
  7. // Skip compression for specific routes
  8. app.Use(compress.New(compress.Config{
  9. Next: func(c *fiber.Ctx) bool {
  10. return c.Path() == "/dont_compress"
  11. },
  12. Level: compress.LevelBestSpeed, // 1
  13. }))