1
0
mirror of https://github.com/gofiber/fiber.git synced 2025-02-20 22:12:56 +00:00
Muhammed Efe Çetin 1188144d78
🎉 v3: init
2022-05-31 17:35:49 +03:00
..
🎉 v3: init
2022-05-31 17:35:49 +03:00
🎉 v3: init
2022-05-31 17:35:49 +03:00
🎉 v3: init
2022-05-31 17:35:49 +03:00
2021-10-19 11:36:16 +02:00

ETag Middleware

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

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

Examples

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

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

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

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!")
})

Custom Config

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!")
})

Config

// Config defines the config for middleware.
type Config struct {
	// Next defines a function to skip this middleware when returned true.
	//
	// Optional. Default: nil
	Next func(c *fiber.Ctx) bool

	// Weak indicates that a weak validator is used. Weak etags are easy
	// to generate, but are far less useful for comparisons. Strong
	// validators are ideal for comparisons but can be very difficult
	// to generate efficiently. Weak ETag values of two representations
	// of the same resources might be semantically equivalent, but not
	// byte-for-byte identical. This means weak etags prevent caching
	// when byte range requests are used, but strong etags mean range
	// requests can still be cached.
	Weak bool
}

Default Config

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