Documentation Index
Fetch the complete documentation index at: https://docs.go-mizu.dev/llms.txt
Use this file to discover all available pages before exploring further.
Overview
Thefavicon middleware serves favicon files efficiently, caching them in memory and handling HEAD requests properly. It prevents favicon requests from reaching your application routes.
Use it when you need:
- Serve favicon from memory
- Prevent favicon 404 logs
- Custom favicon handling
Installation
Quick Start
Configuration
Options
| Option | Type | Default | Description |
|---|---|---|---|
File | string | Required | Path to favicon file |
Data | []byte | nil | Favicon data (alternative to file) |
CacheControl | string | "public, max-age=31536000" | Cache-Control header |
Examples
From File
From Embedded Data
Custom Cache Control
Disable Favicon
API Reference
Functions
Behavior
- Caches favicon in memory after first read
- Sets proper Content-Type (image/x-icon)
- Handles both GET and HEAD requests
- Only matches exact
/favicon.icopath
Technical Details
Implementation
The favicon middleware implements efficient favicon serving with the following characteristics:- Memory Caching: Favicon data is loaded once during middleware initialization and cached in memory for the lifetime of the application.
-
Content Type Detection: Uses
http.DetectContentType()to automatically detect the content type from the favicon data. Falls back toimage/x-iconif detection returnsapplication/octet-streamortext/plain. -
File System Support: Supports three data sources:
- Direct file path using
os.ReadFile - Embedded data using
[]byte - File system interface (
fs.FS) for advanced use cases
- Direct file path using
-
Request Handling:
- Only processes requests matching the configured URL path (default:
/favicon.ico) - Only handles GET and HEAD methods, passes other methods to next handler
- Sets appropriate headers: Content-Type, Content-Length, and Cache-Control
- HEAD requests return headers without body
- Only processes requests matching the configured URL path (default:
-
Special Behaviors:
- Empty configuration (no data) returns HTTP 204 No Content
- SVG middleware supports both
/favicon.icoand/favicon.svgpaths - Redirect middleware returns HTTP 301 Moved Permanently
Performance Considerations
- Favicon is loaded once at startup (panic on file errors)
- No disk I/O during request handling
- Minimal memory allocation per request
- Early path matching prevents unnecessary processing
Best Practices
- Use embedded data for portable deployments
- Set long cache duration for production
- Place before other middleware to avoid unnecessary processing
Testing
The favicon middleware includes comprehensive test coverage for all features:| Test Case | Description | Expected Behavior |
|---|---|---|
TestNew - serve favicon | Serves favicon from file path | Returns 200 OK with correct Content-Type (image/png) |
TestNew - other paths pass through | Non-favicon paths are not intercepted | Request passes to next handler, returns route response |
TestFromData | Serves favicon from byte data | Returns 200 OK with favicon data |
TestWithOptions_CustomURL - custom url serves | Serves favicon at custom URL path | Returns 200 OK at /icon.png |
TestWithOptions_CustomURL - default url does not serve | Default path not served with custom URL | Returns 404 at /favicon.ico |
TestWithOptions_FS | Serves favicon from fs.FS interface | Returns 200 OK with data from MapFS |
TestWithOptions_MaxAge | Custom cache control max-age | Sets Cache-Control: public, max-age=3600 |
TestWithOptions_HeadMethod | HEAD request handling | Returns 200 OK with headers but no body |
TestEmpty | Empty middleware returns no content | Returns 204 No Content for favicon requests |
TestRedirect | Redirects favicon to external URL | Returns 301 with Location header to CDN |
TestSVG - favicon.ico serves SVG | SVG data served at .ico path | Returns 200 OK with image/svg+xml content type |
TestSVG - favicon.svg serves SVG | SVG data served at .svg path | Returns 200 OK at /favicon.svg |
TestWithOptions_NoData | No data provided in options | Returns 204 No Content |