Overview
Thestatic middleware serves static files from a directory or embedded filesystem. It supports index files, directory browsing, and cache control.
Use it when you need:
- Serve static assets (CSS, JS, images)
- Serve a web application’s static files
- Serve files from embedded filesystem
Installation
Quick Start
Configuration
Options
Examples
Basic File Serving
With URL Prefix
From Embedded Filesystem
With Cache Control
Directory Browsing
Custom Index File
Custom 404 Handler
Multiple Static Directories
API Reference
Functions
Behavior
- Falls through to next handler if file not found (unless NotFoundHandler set)
- Automatically serves
index.htmlfor directory requests - Sets appropriate Content-Type based on file extension
- Supports conditional requests with If-Modified-Since
Technical Details
Implementation Overview
The static middleware serves files from either a local filesystem directory or an embeddedfs.FS interface. It implements intelligent path resolution and content serving with the following key features:
Path Resolution
- Strips URL prefix if configured
- Cleans and normalizes file paths using
filepath.Clean - Converts URL paths to filesystem paths safely
- Checks file existence before serving
File Serving Strategy
The middleware uses two different serving approaches:-
Direct File Serving (default)
- Uses
http.ServeContentfor individual files - Provides proper Content-Type detection based on file extension
- Supports HTTP range requests for partial content
- Handles conditional requests with If-Modified-Since headers
- Sets Last-Modified based on file modification time
- Uses
-
FileServer Mode (directory browsing enabled)
- Uses
http.FileServerwhenBrowse: true - Generates HTML directory listings automatically
- Ensures directory paths end with
/to prevent redirects
- Uses
Index File Handling
When a directory is requested:- Checks if an index file exists (default:
index.html) - Serves the index file if found
- If not found and browsing disabled: calls NotFoundHandler or falls through to next middleware
- If not found and browsing enabled: shows directory listing
Cache Control
- Sets
Cache-Control: public, max-age=<seconds>whenMaxAge > 0 - No caching headers by default (
MaxAge: 0) - Relies on HTTP standard caching mechanisms
Filesystem Abstraction
The middleware supports two filesystem sources:- FS (fs.FS): Takes precedence if set, ideal for embedded filesystems
- Root (string): Uses OS filesystem with the specified root directory
Error Handling
- Returns 404 for non-existent files (via custom handler or next middleware)
- Returns 500 for internal file stat errors
- Safely handles file close operations with deferred cleanup
Performance Optimizations
- Custom
itoafunction to avoid allocations for Cache-Control header - Direct file serving without unnecessary FileServer overhead
- Minimal path manipulation and string operations
Best Practices
- Use embedded filesystem for portable deployments
- Set appropriate
MaxAgefor caching static assets - Use versioned filenames for cache busting
- Disable directory browsing in production