Skip to main content
Complete reference for the frontend middleware API.

Functions

New

Creates frontend middleware with default options.
Parameters:
  • root (string): Directory containing built frontend files
Returns: mizu.Middleware Example:

Dev

Creates development-only middleware that proxies to dev server.
Parameters:
  • devServerURL (string): URL of frontend dev server
Returns: mizu.Middleware Example:

WithFS

Creates production middleware with embedded filesystem.
Parameters:
  • fsys (fs.FS): Embedded filesystem containing frontend files
Returns: mizu.Middleware Example:

WithOptions

Creates middleware with full configuration.
Parameters:
  • opts (Options): Configuration options
Returns: mizu.Middleware Example:

Types

Options

Configuration for the frontend middleware.
Fields: Mode:
  • Type: Mode
  • Default: ModeAuto
  • Description: Controls how middleware operates
Root:
  • Type: string
  • Default: "dist"
  • Description: Directory containing built frontend files
FS:
  • Type: fs.FS
  • Default: nil
  • Description: Embedded filesystem (takes precedence over Root)
Index:
  • Type: string
  • Default: "index.html"
  • Description: Fallback file for SPA routing
Prefix:
  • Type: string
  • Default: ""
  • Description: URL prefix for serving frontend
IgnorePaths:
  • Type: []string
  • Default: []string{"/api", "/health", "/metrics"}
  • Description: Paths that bypass frontend middleware
CacheControl:
  • Type: CacheConfig
  • Default: See CacheConfig defaults
  • Description: Caching behavior configuration
DevServer:
  • Type: string
  • Default: "http://localhost:5173"
  • Description: URL of frontend dev server
DevServerTimeout:
  • Type: time.Duration
  • Default: 30 * time.Second
  • Description: Timeout for dev server requests
ProxyWebSocket:
  • Type: bool
  • Default: true
  • Description: Enable WebSocket proxying for HMR
Manifest:
  • Type: string
  • Default: ""
  • Description: Path to build manifest file
InjectEnv:
  • Type: []string
  • Default: nil
  • Description: Environment variables to inject into HTML
InjectMeta:
  • Type: map[string]string
  • Default: nil
  • Description: Meta tags to inject into HTML
SecurityHeaders:
  • Type: bool
  • Default: true
  • Description: Enable default security headers
SourceMaps:
  • Type: bool
  • Default: false
  • Description: Allow serving source map files
ServiceWorker:
  • Type: string
  • Default: ""
  • Description: Path to service worker file
ErrorHandler:
  • Type: func(*mizu.Ctx, error) error
  • Default: nil
  • Description: Custom error handler
NotFoundHandler:
  • Type: func(*mizu.Ctx) error
  • Default: nil
  • Description: Handler called before SPA fallback

Mode

Operating mode for the middleware.
Values:
  • ModeDev: Always proxy to dev server
  • ModeProduction: Always serve static files
  • ModeAuto: Auto-detect based on environment

CacheConfig

Caching behavior configuration.
Fields: HashedAssets:
  • Type: time.Duration
  • Default: 365 * 24 * time.Hour (1 year)
  • Description: Cache duration for files with content hash
UnhashedAssets:
  • Type: time.Duration
  • Default: 7 * 24 * time.Hour (1 week)
  • Description: Cache duration for files without hash
HTML:
  • Type: time.Duration
  • Default: 0 (no-cache)
  • Description: Cache duration for HTML files
Patterns:
  • Type: map[string]time.Duration
  • Default: nil
  • Description: Custom cache durations for file patterns
Example:

Manifest API

LoadManifest

Loads a build manifest from filesystem.
Parameters:
  • fsys (fs.FS): Filesystem containing manifest
  • path (string): Path to manifest file
Returns:
  • *Manifest: Loaded manifest
  • error: Error if loading fails
Example:

Manifest Methods

Injection Functions

InjectEnv

Injects environment variables into HTML.
Parameters:
  • html ([]byte): HTML content
  • vars ([]string): Environment variable names to inject
Returns: Modified HTML with injected script

InjectMeta

Injects meta tags into HTML.
Parameters:
  • html ([]byte): HTML content
  • meta (map[string]string): Meta tags (name → content)
Returns: Modified HTML with injected meta tags

InjectPreload

Injects preload link tags into HTML.
Parameters:
  • html ([]byte): HTML content
  • assets ([]PreloadAsset): Assets to preload
Returns: Modified HTML with preload tags

Constants

Default Values

Default Ignore Paths

Default Security Headers

Next Steps

Configuration

Configuration guide

Adapters

Framework adapters

Examples

Code examples