Compress
Compression middleware for Fiber that will compress the response using gzip, deflate and brotli compression depending on the Accept-Encoding header.

Signatures

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

Examples

Import the middleware package that is part of the Fiber web framework
1
import (
2
"github.com/gofiber/fiber/v2"
3
"github.com/gofiber/fiber/v2/middleware/compress"
4
)
Copied!
After you initiate your Fiber app, you can use the following possibilities:
1
// Default middleware config
2
app.Use(compress.New())
3
​
4
// Provide a custom compression level
5
app.Use(compress.New(compress.Config{
6
Level: compress.LevelBestSpeed, // 1
7
}))
8
​
9
// Skip middleware for specific routes
10
app.Use(compress.New(compress.Config{
11
Next: func(c *fiber.Ctx) bool {
12
return c.Path() == "/dont_compress"
13
},
14
Level: compress.LevelBestSpeed, // 1
15
}))
Copied!

Config

1
// Config defines the config for middleware.
2
type Config struct {
3
// Next defines a function to skip this middleware when returned true.
4
//
5
// Optional. Default: nil
6
Next func(c *fiber.Ctx) bool
7
​
8
// CompressLevel determines the compression algoritm
9
//
10
// Optional. Default: LevelDefault
11
// LevelDisabled: -1
12
// LevelDefault: 0
13
// LevelBestSpeed: 1
14
// LevelBestCompression: 2
15
Level int
16
}
Copied!

Default Config

1
var ConfigDefault = Config{
2
Next: nil,
3
Level: LevelDefault,
4
}
Copied!

Constants

1
// Compression levels
2
const (
3
LevelDisabled = -1
4
LevelDefault = 0
5
LevelBestSpeed = 1
6
LevelBestCompression = 2
7
)
Copied!
Last modified 7mo ago