github.com/gofiber/fiber/v2@v2.47.0/docs/api/middleware/compress.md (about) 1 --- 2 id: compress 3 title: Compress 4 --- 5 6 Compression middleware for [Fiber](https://github.com/gofiber/fiber) that will compress the response using `gzip`, `deflate` and `brotli` compression depending on the [Accept-Encoding](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Encoding) header. 7 8 ## Signatures 9 10 ```go 11 func New(config ...Config) fiber.Handler 12 ``` 13 14 ## Examples 15 16 Import the middleware package that is part of the Fiber web framework 17 18 ```go 19 import ( 20 "github.com/gofiber/fiber/v2" 21 "github.com/gofiber/fiber/v2/middleware/compress" 22 ) 23 ``` 24 25 After you initiate your Fiber app, you can use the following possibilities: 26 27 ```go 28 // Initialize default config 29 app.Use(compress.New()) 30 31 // Or extend your config for customization 32 app.Use(compress.New(compress.Config{ 33 Level: compress.LevelBestSpeed, // 1 34 })) 35 36 // Skip middleware for specific routes 37 app.Use(compress.New(compress.Config{ 38 Next: func(c *fiber.Ctx) bool { 39 return c.Path() == "/dont_compress" 40 }, 41 Level: compress.LevelBestSpeed, // 1 42 })) 43 ``` 44 45 ## Config 46 47 ```go 48 // Config defines the config for middleware. 49 type Config struct { 50 // Next defines a function to skip this middleware when returned true. 51 // 52 // Optional. Default: nil 53 Next func(c *fiber.Ctx) bool 54 55 // Level determines the compression algoritm 56 // 57 // Optional. Default: LevelDefault 58 // LevelDisabled: -1 59 // LevelDefault: 0 60 // LevelBestSpeed: 1 61 // LevelBestCompression: 2 62 Level int 63 } 64 ``` 65 66 ## Default Config 67 68 ```go 69 var ConfigDefault = Config{ 70 Next: nil, 71 Level: LevelDefault, 72 } 73 ``` 74 75 ## Constants 76 77 ```go 78 // Compression levels 79 const ( 80 LevelDisabled = -1 81 LevelDefault = 0 82 LevelBestSpeed = 1 83 LevelBestCompression = 2 84 ) 85 ```