Skip to content

Static

Static<T>(options, prefix): Middleware

Defined in: src/plugins/middleware/static.ts:31

Creates a static file serving middleware that falls through to the next handler when files are not found, instead of returning 404.

This is useful for scenarios where you want to serve static files first, but fall back to a route handler if the file doesn’t exist.

T extends Record<string, any>

Static serve options or root directory path

string | StaticServeOptions<T>

string = '/'

URL prefix to strip from paths (defaults to ’/‘)

Middleware

Middleware that serves static files or calls next()

// Basic usage - serves files from ./dist
router.get('/app', Static({ root: './dist' }), async (ctx) => {
// This handler only runs if no static file was found
return ctx.html(await renderSPA());
});
// With custom options
router.get('/assets', Static({
root: './public',
etag: true,
maxAge: 3600
}), ctx => ctx.text('Asset not found'));