@kitajs/fastify-html-plugin
TypeScript icon, indicating that this package has built-in type declarations

4.1.0 • Public • Published

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 , 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.


Readme

Keywords

none

Package Sidebar

Install

npm i @kitajs/fastify-html-plugin

Weekly Downloads

4

Version

4.1.0

License

MIT

Unpacked Size

101 kB

Total Files

14

Last publish

Collaborators

  • hazork