@jackdbd/fastify-webc

@jackdbd/fastify-webc

npm version install size CodeCov badge Socket Badge

Fastify plugin to render WebC templates and components.

npm install @jackdbd/fastify-webc

Register this plugin by telling it where to find your WebC components and templates.

This plugin decorates FastifyReply with a render method. This method takes two arguments: a WebC template and an optional data object.

import webc from '@jackdbd/fastify-webc'

fastify.register(webc, {
components: ['src/components/**/*.webc'],
templates: [path.join(__dirname, 'templates')]
})

fastify.get('/demo', function (request, reply) {
const data = { foo: 'bar' }
return reply.render('demo.webc', data)
})

To ensure TypeScript recognizes the render method as part of the FastifyReply object, you must use declaration merging to extend the FastifyReply interface.

declare module 'fastify' {
interface FastifyReply {
render(template: string, data?: Record<string, any>): Promise<void>
}
}

You can access the WebC instance from your WebC template/component using this.webc.

<code>this.webc</code>
<pre @text="JSON.stringify(this.webc, null, 2)"></pre>

You can access the data available to the WebC template/component using this.$data.

<code>this.$data</code>
<pre @text="JSON.stringify(this.$data, null, 2)"></pre>
Package Version
@11ty/webc ^0.11.4
fastify-plugin ^5.0.1
Warning

This package defines 1 peer dependency.

Peer Version range
fastify >=5.0.0

© 2024 - 2025 Giacomo Debidda // MIT License