Favicon

Favicon middleware for Fiber that ignores favicon requests or caches a provided icon in memory to improve performance by skipping disk access. User agents request /favicon.ico frequently and indiscriminately, so you may wish to exclude these requests from your logs by using this middleware before your logger middleware.

Note This middleware is exclusively for serving the default, implicit favicon, which is GET /favicon.ico.

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/favicon"
  4. )

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

  1. // Default config
  2. app.Use(favicon.New())
  3. // Or extend your config for customization
  4. app.Use(favicon.New(favicon.Config{
  5. File: "./favicon.ico"
  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. // File holds the path to an actual favicon that will be cached
  8. //
  9. // Optional. Default: ""
  10. File string
  11. }

Default Config

  1. var ConfigDefault = Config{
  2. Next: nil,
  3. File: ""
  4. }