Use Enhance's renderer as a view engine in a Fastify application.
💁 Not to be confused with
@enhance/fastify-plugin
, which offers the full Enhance stack in your Fastify app.
import Fastify from 'fastify';
import enhanceSsr from 'fastify-enhance-ssr';
import HelloYou from './views/elements/hello-you.js';
const fastify = Fastify();
fastify.register(enhanceSsr, {
enhanceOptions: {
elements: { 'hello-you': HelloYou },
},
});
fastify.get('/', async (_request, reply) => {
const page = await import('./views/a-page.js');
reply.enhance(page.default, {
title: 'Enhance SSR on Fastify',
user: { name: 'Enhanstify' },
});
});
fastify.get('/simple', (_request, reply) => {
reply.enhance('<hello-you></hello-you>');
});
fastify.listen();
Also see example/server.js
for further usage.
fastify-enhance-ssr
adds reply.enhance
.
{
layout: function(body: string, data: object): string,
enhanceOptions: object,
}
string | function(data): string
a string of HTML or a function that returns a string of HTML. This will be passed to Enhance SSR.
object
state provided to the body, layout, and Enhance (as initialState
)
{ layout: function }
override the default layout function