Juan Calderon-Perez 30bb809255
🐛 bug: Fix Etag validation per RFC 9110 (#3554)
* Add strong ETag comparison helper

* Review fixes

* update normalizeEtag

* cleanup new tests, add more cases.

* lint

* name param

* more fixes

* more fixes

* add missing param

* fix funct name

* string not bytes

* format is correct

* fix lint?

* enable debug output for linter

* Fix linter issue
2025-06-30 08:52:01 +02:00

1.9 KiB

id
id
etag

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.

Signatures

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

Examples

Import the middleware package that is part of the Fiber web framework

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

After you initiate your Fiber app, you can use the following possibilities:

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

// Get / receives Etag: "13-1831710635" in response header
app.Get("/", func(c fiber.Ctx) error {
    return c.SendString("Hello, World!")
})

// Or extend your config for customization
app.Use(etag.New(etag.Config{
    Weak: true,
}))

// Get / receives Etag: "W/"13-1831710635" in response header
app.Get("/", func(c fiber.Ctx) error {
    return c.SendString("Hello, World!")
})

Entity tags in requests must follow RFC 9110 and therefore be quoted, for example:

If-None-Match: "example-etag"

Config

Property Type Description Default
Weak bool Weak indicates that a weak validator is used. Weak etags are easy to generate but are less useful for comparisons. false
Next func(fiber.Ctx) bool Next defines a function to skip this middleware when returned true. nil

Default Config

var ConfigDefault = Config{
    Next: nil,
    Weak: false,
}