Skip to main content
Version: v3.x

Favicon

Favicon middleware for Fiber that drops repeated /favicon.ico requests or serves a cached icon from memory. Mount it before your logger to suppress noisy requests and avoid disk reads.

It handles only GET, HEAD, and OPTIONS to the configured URL; other methods return 405 Method Not Allowed. A configured icon is always served as image/x-icon with the configured Cache-Control; without File or Data, requests for the URL are answered with 204 No Content.

note

This middleware only serves the default /favicon.ico (or a custom URL). For multiple icons, use the Static middleware.

Signatures​

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

Examples​

Import the middleware package:

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

Once your Fiber app is initialized, use the middleware like this:

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

// Or extend your config for customization
app.Use(favicon.New(favicon.Config{
File: "./favicon.ico",
URL: "/favicon.ico",
}))

Config​

PropertyTypeDescriptionDefault
Nextfunc(fiber.Ctx) boolNext defines a function to skip this middleware when it returns true.nil
Data[]byteRaw data of the favicon file. This can be used instead of File.nil
FilestringFile holds the path to an actual favicon that will be cached. New panics if the file cannot be read.""
URLstringURL for favicon handler."/favicon.ico"
FileSystemfs.FSFileSystem is an optional alternate filesystem from which to load the favicon file (e.g. using os.DirFS or an embed.FS).nil
CacheControlstringCacheControl defines how the Cache-Control header in the response should be set."public, max-age=31536000"
MaxBytesint64MaxBytes limits the maximum size of the cached favicon asset. New panics if the file at File is larger; the limit does not apply to Data.1048576

Default Config​

var ConfigDefault = Config{
Next: nil,
File: "",
URL: fPath,
CacheControl: "public, max-age=31536000",
MaxBytes: 1024 * 1024,
}