Skip to main content

Overview

The static 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.html for 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 embedded fs.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:
  1. Direct File Serving (default)
    • Uses http.ServeContent for 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
  2. FileServer Mode (directory browsing enabled)
    • Uses http.FileServer when Browse: true
    • Generates HTML directory listings automatically
    • Ensures directory paths end with / to prevent redirects

Index File Handling

When a directory is requested:
  1. Checks if an index file exists (default: index.html)
  2. Serves the index file if found
  3. If not found and browsing disabled: calls NotFoundHandler or falls through to next middleware
  4. If not found and browsing enabled: shows directory listing

Cache Control

  • Sets Cache-Control: public, max-age=<seconds> when MaxAge > 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 itoa function 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 MaxAge for caching static assets
  • Use versioned filenames for cache busting
  • Disable directory browsing in production

Testing

The static middleware includes comprehensive test coverage for all major functionality:
  • spa - Single Page Application support
  • favicon - Favicon serving
  • embed - Embedded filesystem helpers
  • cache - Cache control headers