Skip to content

Latest commit

 

History

History
 
 

fastify-html-plugin

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Using this package? Please consider donating to support my open source work ❤️
Help @kitajs/fastify-html-plugin grow! Star and share this amazing repository with your friends and co-workers!


Kita JS logo


Discord License Codecov Downloads Bundlephobia Last commit Stars


🖨️ Fastify KitaJS Html Plugin

@kitajs/fastify-html-plugin is a fastify plugin to seamlessly integrate the KitaJS Html JSX engine into your fastify application.




Installing

npm install @kitajs/fastify-html-plugin

Preview

Example of an error thrown by this LSP plugin.


Installing

Caution

You must have followed the @kitajs/html's Installing guide before continuing, otherwise you will be vulnerable to XSS attacks.

import kitaHtmlPlugin from '@kitajs/fastify-html-plugin';
import fastify from 'fastify';

const app = fastify();

app.register(kitaHtmlPlugin);

Configuration

Every option is well documented through their respective JSDoc comments, below are the default options.

Name Description Default
autoDoctype Whether to automatically add <!doctype html> to a response starting with <html>, if not found. true

Documentation

@kitajs/html is a JSX -> string runtime, this package seamlessly integrates with fastify to improve the developer experience while also providing faster implementations for this use case.


API

reply.html()

Sends the html to the browser. This method supports all types of components, including <Suspense /> and <ErrorBoundary />.

The entire component tree will be awaited before being sent to the browser as a single piece.

When Suspense components are found, their fallback will be synchronously awaited and sent to the browser in the original stream, as its children are resolved, new pieces of html will be sent to the browser. When all Suspenses pending promises are resolved, the connection is closed normally.

Note

req.id must be used as the Suspense's rid parameter

If the HTML does not start with a doctype and opts.autoDoctype is enabled, it will be added automatically. The correct Content-Type header will also be defined.

app
  .get('/html', (req, reply) =>
    reply.html(
      <html lang="en">
        <body>
          <h1>Hello, world!</h1>
        </body>
      </html>
    )
  )
  .get('/stream', (req, reply) =>
    reply.html(
      <Suspense rid={req.id} fallback={<div>Loading...</div>}>
        <MyAsyncComponent />
      </Suspense>
    )
  );

License

Licensed under the MIT. See LICENSE for more informations.