Overview
Thesurrogate middleware manages Surrogate-Control and Surrogate-Key headers for CDN cache control (Fastly, Varnish, etc.).
Use it when you need:
- CDN cache purging
- Edge cache control
- Cache tagging
Installation
Quick Start
Configuration
Options
| Option | Type | Default | Description |
|---|---|---|---|
MaxAge | time.Duration | 0 | Surrogate max-age |
StaleWhileRevalidate | time.Duration | 0 | Stale-while-revalidate |
Examples
Set Surrogate Keys
Surrogate Control
Per-Route Control
API Reference
Functions
CDN Integration
Fastly
Varnish
Uses same headers with xkey support.Technical Details
The surrogate middleware implements CDN cache control through a context-based key management system:Architecture
- Context Storage: Uses Go’s
context.Contextto store surrogate keys during request processing - Keys Structure: Maintains a
Keystype that holds a slice of string keys - Header Management: Automatically sets headers after handler execution using middleware pattern
Implementation Details
- Initialization: The middleware creates a
Keysinstance and stores it in the request context - Default Keys: If configured, default keys are added to every response
- Handler Execution: The next handler in the chain executes, allowing route handlers to add keys via
Add() - Header Setting: After handler completion, the middleware:
- Joins all keys with spaces and sets the configured header (default:
Surrogate-Key) - Builds and sets the
Surrogate-Controlheader based on options (MaxAge, StaleWhileRevalidate, StaleIfError)
- Joins all keys with spaces and sets the configured header (default:
Control Header Format
TheSurrogate-Control header is built from options:
max-age=N: Set viaOptions.MaxAgestale-while-revalidate=N: Set viaOptions.StaleWhileRevalidatestale-if-error=N: Set viaOptions.StaleIfError
CDN Presets
The middleware includes three CDN-specific presets:- Fastly(): Uses
Surrogate-Keyheader - Varnish(): Uses
xkeyheader - CloudFront(): Uses
x-amz-meta-surrogate-keyheader
Best Practices
- Use meaningful, granular keys
- Group related content with shared keys
- Set appropriate TTLs
- Plan purge strategy
Testing
The surrogate middleware includes comprehensive test coverage for all functionality:| Test Case | Description | Expected Behavior |
|---|---|---|
TestNew | Basic middleware initialization with key addition | Adds multiple keys (page, content) and sets Surrogate-Key header |
TestWithOptions_DefaultKeys | Default keys configuration | Automatically includes default keys (site, global) in all responses |
TestWithOptions_CustomHeader | Custom header name | Uses custom header name (xkey) instead of default Surrogate-Key |
TestGet | Retrieve Keys from context | Returns the Keys instance from context and allows manipulation |
TestAdd | Add multiple keys in handler | Adds multiple keys (key1, key2) and includes both in header |
TestClear | Clear all keys including defaults | Removes all keys, resulting in no Surrogate-Key header |
TestWithKeys | Convenience function for default keys | Creates middleware with default keys using WithKeys() helper |
TestFastly | Fastly CDN preset | Uses Surrogate-Key header with Fastly configuration |
TestVarnish | Varnish CDN preset | Uses xkey header for Varnish compatibility |
TestNoKeys | No keys added scenario | Does not set Surrogate-Key header when no keys are added |
TestKeysCleared | Keys.Clear() method | Properly clears keys from the Keys struct |