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
Thevary middleware manages the Vary HTTP header, telling caches which request headers affect response content.
Use it when you need:
- Proper cache control
- Content negotiation
- CDN configuration
Installation
Quick Start
Configuration
Options
| Option | Type | Default | Description |
|---|---|---|---|
Headers | []string | Required | Headers to vary by |
Examples
Single Header
Multiple Headers
Common Patterns
API Reference
Functions
Technical Details
Implementation Overview
The vary middleware manages theVary HTTP response header by:
- Header Management: Maintains a set of headers that affect response content
- Duplicate Prevention: Uses case-insensitive comparison to prevent duplicate headers
- Header Merging: Combines new Vary headers with existing ones from the response
- Execution Order: Processes after the handler executes to capture all Vary requirements
Key Functions
add(c *mizu.Ctx, headers ...string)
- Adds headers to the Vary response header
- Parses existing Vary header and creates a set for deduplication
- Uses case-insensitive comparison (
strings.ToLower) - Joins headers with comma-space separator
autoDetect(c *mizu.Ctx)
- Automatically detects content negotiation headers from the request
- Checks for:
Accept,Accept-Encoding,Accept-Language - Only adds headers that are present in the request
Options Structure
Convenience Functions
The middleware provides several preset functions:AcceptEncoding(): AddsAccept-Encodingto VaryAccept(): AddsAcceptto VaryAcceptLanguage(): AddsAccept-Languageto VaryOrigin(): AddsOriginto Vary (useful for CORS)All(): Adds common headers:Accept,Accept-Encoding,Accept-LanguageAuto(): Enables automatic header detection
Best Practices
- Include all headers that affect response
- Use with compression middleware
- Consider CDN caching implications
- Donβt over-vary (reduces cache effectiveness)
Testing
The middleware includes comprehensive tests covering various scenarios:| Test Case | Description | Expected Behavior |
|---|---|---|
TestNew | Basic middleware with multiple headers | Sets Vary header with Accept-Encoding and Accept-Language |
TestNoDuplicates | Same header specified twice | Prevents duplicates; header appears only once in Vary |
TestWithOptions_Auto | Auto-detection with request headers | Automatically adds Accept and Accept-Encoding based on request |
TestAdd | Helper function within handler | Adds custom header X-Custom-Header to Vary |
TestAcceptEncoding | AcceptEncoding convenience function | Sets Vary header to Accept-Encoding |
TestAccept | Accept convenience function | Sets Vary header to Accept |
TestAcceptLanguage | AcceptLanguage convenience function | Sets Vary header to Accept-Language |
TestOrigin | Origin convenience function | Sets Vary header to Origin |
TestAll | All convenience function | Sets Vary header with Accept, Accept-Encoding, and Accept-Language |
TestAuto | Auto convenience function with request header | Auto-detects and adds Accept-Language when present in request |
TestCombineWithExisting | Combining with existing Vary header | Preserves existing X-Custom and adds Accept-Encoding |