@fastify/static

A Fastify plugin that serves static files and directories as fast as possible, with pre-compressed assets, custom caching, and directory listings built in.

Library
npm
v10.1.3
501stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
84/100Excellent
Development Activity84
Maintenance88
Community84
Maturity60
Momentum20

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
81/100Excellent
Architecture82
Code Quality92
Innovation85
Learning Curve65

@fastify/static is the official Fastify plugin for serving static files directly from a Node.js Fastify server. It registers a wildcard route (or, optionally, pre-globbed explicit routes) under a configurable path prefix, decorates the Fastify reply with sendFile() and download() helpers, and streams file contents through @fastify/send, handling caching headers, ETags, range requests, and content-type negotiation along the way.

Beyond basic file serving, the plugin supports multiple static roots mounted under one prefix, brotli/gzip pre-compressed asset negotiation via @fastify/accept-negotiator, JSON or HTML directory listings with extended folder metadata, and fine-grained control over dotfiles, redirects, and per-file cache-control overrides — making it equally suited to serving a handful of assets or hosting an entire single-page-application build.

What You Get

  • sendFile() / download() reply decorators - call reply.sendFile('name.html') or reply.download(...) from any route handler, not just the wildcard route.
  • Pre-compressed asset support - automatically serves .br or .gz siblings of a file based on the request’s Accept-Encoding header.
  • Directory listings - opt-in JSON or HTML directory index responses, with an extended mode reporting recursive size/file-count metadata per folder.
  • Multi-root mounting - register the plugin multiple times with different root directories under the same or different prefixes, served first-found-first-served.
  • Typed error codes - configuration mistakes throw distinguishable FST_STATIC_* errors built on @fastify/error rather than generic exceptions.

Common Use Cases

  • Serving a built SPA - host a Vite/webpack production build directly from Fastify, with long-lived immutable caching for hashed assets and no caching for index.html.
  • Authenticated file downloads - expose download endpoints via reply.download() with a custom filename and content-disposition header.
  • Static documentation sites - serve a folder of prebuilt HTML/CSS/JS docs with custom 404 handling scoped to an encapsulated Fastify context.
  • Compressed asset delivery - serve versioned, pre-compressed (brotli/gzip) assets behind a CDN or reverse proxy, using the cache-control and ETag headers the plugin sets.

Under The Hood

Architecture The plugin is a single fastify-plugin-wrapped async function (index.js) that, depending on options, registers either one wildcard GET/HEAD route (prefix + '*') or pre-globs the filesystem up front (via glob) and registers one explicit route per file. It decorates the Fastify reply with sendFile and download, both of which funnel through an internal pumpSendToReply helper that streams the response using @fastify/send. Directory listing is factored out into lib/dirList.js, which uses fastq to bound filesystem concurrency when computing recursive folder statistics, and custom error types live in lib/errors.js on top of @fastify/error. This keeps the plugin a thin, single-purpose extension of Fastify’s routing layer rather than a standalone framework — the kind of narrowly scoped, composable plugin pattern the Fastify ecosystem is built around.

Tech Stack Written in plain CommonJS JavaScript for Node.js, targeting current Fastify major versions (compatibility table in the README tracks plugin version against Fastify ^5.x/^4.x/etc). Core dependencies are all from the Fastify org itself — @fastify/send for file streaming, @fastify/accept-negotiator for content-encoding negotiation, @fastify/error for typed errors, and fastify-plugin for plugin registration semantics — plus glob for filesystem globbing, fastq for concurrency-limited async queues, and content-disposition for download headers. Type definitions are hand-written in types/index.d.ts and exercised with tstyche rather than generated.

Code Quality Testing uses borp (Node’s built-in test runner wrapper) with c8 coverage enforced at 100% lines via --check-coverage --lines 100, backed by an extensive test/static.test.js suite (nearly 5,000 lines) plus dedicated suites for directory listing and content-type handling, alongside fixture directories covering dotfiles, symlinks, pre-compressed variants, and path-encoding edge cases. Errors are raised as distinguishable typed error classes rather than generic Error instances or silent fallbacks. Linting runs through neostandard (ESLint-based) with a dedicated lint/lint:fix script, and CI runs on GitHub Actions. Overall this reads as a mature, thoroughly tested plugin with no notable quality gaps.

API Design The public surface is intentionally small: register the plugin with a root, optionally set a prefix, and either rely on the automatic wildcard route or call the added sendFile/download reply methods explicitly. Sensible defaults (serve everything under /, cache-control on, dotfiles disallowed) mean a working setup takes one fastify.register() call, while power users get fine-grained knobs — multi-root arrays, per-request header overrides, custom directory-list rendering, and route constraints — without those options getting in the way of the common case. The README documents every option with runnable examples, which keeps the learning curve low despite the surface area.

Used by 9 apps in this directory

TypeScript
99%
Other

Activepieces

Automation · AI Assistants

24,298

Open-source AI automation platform that converts 280+ workflow integrations into MCP servers for LLMs, with no-code builders and TypeScript extensibility.

View details
92
Repo Health
85
Technical
64
Dependency
Built with
TypeScript99%
Updated today
TypeScript
95%
AGPL 3.0

Docmost

Productivity · Note Taking · Collaboration

21,600

Self-hosted collaborative wiki and knowledge base with real-time editing, diagrams, AI assistance, and enterprise access controls — a modern alternative to Confluence and Notion.

View details
89
Repo Health
70
Technical
70
Dependency
Built with
TypeScript95%
Updated yesterday
TypeScript
99%
MIT

GraphQL Hive

Developer Tools · Devops · Monitoring

484

Open-source GraphQL schema registry and observability platform with breaking change detection, federation support, and CI/CD integration for teams of any size.

View details
89
Repo Health
81
Technical
69
Dependency
Built with
TypeScript99%
Updated 2 days ago
TypeScript
97%
Other

Infisical

Security · Devops

29,145

The open-source platform for secrets, certificates, privileged access, and AI agent security — all in one self-hostable system.

View details
91
Repo Health
84
Technical
65
Dependency
Built with
TypeScript97%
Updated yesterday
TypeScript
80%
Apache 2.0

nao

AI Development · Analytics

1,611

Build and deploy an open-source analytics agent that understands your data warehouse and answers business questions in plain English.

View details
84
Repo Health
76
Technical
67
Dependency
Built with
TypeScript80%
Python19%
Updated yesterday
TypeScript
82%
Apache 2.0

Palmr.

File Storage · Security

2,401

Self-hosted, privacy-focused file sharing without limits

View details
45
Repo Health
62
Technical
72
Dependency
Built with
TypeScript82%
MDX13%
Updated 6 months ago
Python
48%
Other

Arize Phoenix

Devops · Analytics · Monitoring

11,346

Open-source AI observability platform for tracing, evaluating, and debugging LLM applications with built-in intelligence and MCP support.

View details
90
Repo Health
88
Technical
67
Dependency
Built with
Python48%
TypeScript41%
Updated yesterday
TypeScript
91%
MIT

Super Productivity

Productivity · Project Management

21,845

A privacy-respecting, local-first task manager with built-in timeboxing, Pomodoro timer, and deep integrations for Jira, GitHub, GitLab, and CalDAV — no accounts, no data collection, ever.

View details
90
Repo Health
81
Technical
72
Dependency
Built with
TypeScript91%
Updated 2 days ago
TypeScript
79%
MIT

Trench

Analytics · Data Engineering · Monitoring

1,662

Open-source event tracking infrastructure built on Kafka and ClickHouse that handles thousands of events per second on a single node, with full Segment API compatibility and no cookies.

View details
45
Repo Health
81
Technical
74
Dependency
Built with
TypeScript79%
MDX15%
Updated 5 months ago

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search