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

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

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. )

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

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

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. // Weak indicates that a weak validator is used. Weak etags are easy
  8. // to generate, but are far less useful for comparisons. Strong
  9. // validators are ideal for comparisons but can be very difficult
  10. // to generate efficiently. Weak ETag values of two representations
  11. // of the same resources might be semantically equivalent, but not
  12. // byte-for-byte identical. This means weak etags prevent caching
  13. // when byte range requests are used, but strong etags mean range
  14. // requests can still be cached.
  15. Weak bool
  16. }

Default Config

  1. var ConfigDefault = Config{
  2. Next: nil,
  3. Weak: false,
  4. }