ETag
ETag middleware for Fiber that lets caches be more efficient and save bandwidth, as a web server does not need to resend a full response if the content has not changed.

Table of Contents

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/etag"
4
)
Copied!
After you initiate your Fiber app, you can use the following possibilities:

Default Config

1
app.Use(etag.New())
2
​
3
// Get / receives Etag: "13-1831710635" in response header
4
app.Get("/", func(c *fiber.Ctx) error {
5
return c.SendString("Hello, World!")
6
})
Copied!

Custom Config

1
app.Use(etag.New(etag.Config{
2
Weak: true,
3
}))
4
​
5
// Get / receives Etag: "W/"13-1831710635" in response header
6
app.Get("/", func(c *fiber.Ctx) error {
7
return c.SendString("Hello, World!")
8
})
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
// Weak indicates that a weak validator is used. Weak etags are easy
9
// to generate, but are far less useful for comparisons. Strong
10
// validators are ideal for comparisons but can be very difficult
11
// to generate efficiently. Weak ETag values of two representations
12
// of the same resources might be semantically equivalent, but not
13
// byte-for-byte identical. This means weak etags prevent caching
14
// when byte range requests are used, but strong etags mean range
15
// requests can still be cached.
16
Weak bool
17
}
Copied!

Default Config

1
var ConfigDefault = Config{
2
Next: nil,
3
Weak: false,
4
}
Copied!
Last modified 1mo ago