Kind: Interface
Source: packages/platform-fastify/interfaces/external/fastify-static-options.interface.ts
Part of: Platform Fastify
FastifyStaticOptions configures static asset serving for the Fastify platform adapter. It controls the asset root directory, URL prefix, reply decoration, caching headers, redirects, directory listings, and wildcard route behavior when registering static-file support.
Properties
| Property | Type |
|---|---|
root | `string |
prefix | string |
prefixAvoidTrailingSlash | boolean |
serve | boolean |
decorateReply | boolean |
schemaHide | boolean |
setHeaders | (res: SetHeadersResponse, path: string, stat: Stats) => void |
redirect | boolean |
wildcard | boolean |
list | `boolean |
allowedPath | ( pathName: string, root: string, request: FastifyRequest, ) => boolean |
preCompressed | boolean |
acceptRanges | boolean |
cacheControl | boolean |
dotfiles | `'allow' |
etag | boolean |
extensions | string[] |
immutable | boolean |
index | `string[] |
lastModified | boolean |
maxAge | `string |
constraints | RouteOptions['constraints'] |
Diagram
mermaidgraph LR A[FastifyStaticOptions] --> B[root] A --> C[prefix] A --> D[Serving behavior] A --> E[Response behavior] A --> F[Directory listing] D --> D1[serve] D --> D2[redirect] D --> D3[wildcard] E --> E1[decorateReply] E --> E2[schemaHide] E --> E3[setHeaders] F --> F1[list]
Usage
tsimport { join } from 'node:path';
import type { FastifyStaticOptions } from './interfaces/external/fastify-static-options.interface';
const staticOptions: FastifyStaticOptions = {
root: join(process.cwd(), 'public'),
prefix: '/assets/',
prefixAvoidTrailingSlash: false,
serve: true,
decorateReply: true,
schemaHide: true,
redirect: true,
wildcard: true,
list: false,
setHeaders(res, path, stat) {
if (path.endsWith('.js') || path.endsWith('.css')) {
res.setHeader('Cache-Control', 'public, max-age=31536000, immutable');
}
res.setHeader('Content-Length', stat.size);
},
};
// Example: pass staticOptions when configuring Fastify static serving.
AI Coding Instructions
- Use an absolute filesystem path for
root; provide an array of roots only when serving assets from multiple locations. - Keep
prefixaligned with application routing conventions, including whether a trailing slash is expected. - Use
setHeadersfor cache and content-related headers; avoid performing asynchronous work in this callback. - Disable
decorateReplyonly if application code does not need Fastify reply helpers for static files. - Enable
listonly for controlled or development environments, as directory listings can expose file names and structure.
Was this page helpful?