Fastify
Estimated reading time: 2 minutes@kitajs/fastify-html-plugin adds a reply.html() method to Fastify that handles content
type headers, Suspense streaming, and automatic doctype injection.
Setup
You must install and configure @kitajs/ts-html-plugin before using this plugin to avoid
XSS vulnerabilities. See the Getting Started guide for complete
setup instructions including TypeScript configuration and XSS detection.
Register the plugin on your Fastify instance:
Sending HTML
Use reply.html() with any JSX expression. The plugin sets the Content-Type header to
text/html; charset=utf-8.
For async components, reply.html() accepts promises and awaits them before sending.
For synchronous responses, the plugin calculates and sets the Content-Length header
automatically. For Suspense streaming (below), the response uses chunked transfer encoding
without a Content-Length header.
Suspense streaming
When the JSX tree contains Suspense components, the plugin automatically switches from a
buffered response to a chunked stream. Use req.id as the Suspense rid to tie the
stream to the request.
No manual stream handling is required. The plugin detects Suspense usage through the
global SUSPENSE_ROOT and calls resolveHtmlStream internally.
Error handling
The plugin supports ErrorBoundary components for catching async errors. Wrap async
content in an ErrorBoundary to handle failures gracefully.
See the Error Boundaries guide for details on error handling patterns.
Auto-doctype
By default, the plugin prepends <!doctype html> to responses that start with an <html>
tag. To disable this globally:
To disable it for a single request, use the exported kAutoDoctype symbol:
Compatibility
The plugin works with Fastify 4.x and 5.x.