Getting Started
Core Concepts
Built-In Middleware
Advanced Features
Troubleshooting
The Serve Static feature allows your Hono application to host files such as images, stylesheets, or client-side scripts directly from your server's filesystem or a storage service.
Instead of building complex routing for every asset, you can point the middleware to a directory, and it will automatically handle request mapping, MIME type detection, and basic file serving.
To serve static files, use the serveStatic function provided by the adapter corresponding to your runtime environment (e.g., Bun, Deno).
Example: Serving files in Bun
import { Hono } from 'hono'
import { serveStatic } from 'hono/bun'
const app = new Hono()
// Serve files from the 'public' directory
app.use('/static/*', serveStatic({ root: './public' }))
export default appThe serveStatic middleware accepts an options object to customize how files are retrieved and processed.
Note
The serveStatic middleware is environment-specific. Always import it from hono/bun, hono/deno, or the relevant adapter to ensure it has the correct permissions to access the local file system.
serveStatic is a standard Hono middleware. It will only serve a file if it is found. If it is not found, it calls next() to allow other routes or middleware to handle the request.index.html file from within that directory.precompressed: true, the server will check for files with encoding extensions (e.g., .br for Brotli) if the client supports them via the Accept-Encoding header. This can significantly reduce bandwidth usage.Warning
When serving files, ensure that your root directory is correctly scoped. Never point the root to sensitive system directories.
Important
The Cloudflare Workers adapter for serveStatic is currently deprecated. For Cloudflare environments, it is recommended to use the official Cloudflare Static Assets or Cloudflare Pages deployment patterns.