Need help on native Fastify middleware #2260
-
Hello, I was trying to migrate the universal middleware in my app to native fastify server middlewares and wrote this simple fastify plugin, am I missing anything? everything seems to work fine on the starter app so far. /// <reference lib="webworker" />
import { renderPage } from "vike/server";
import type { FastifyPluginCallback } from "fastify";
const vikeFastifyPlugin = ((fastify) => {
fastify.all("/*", async (request, reply) => {
const pageContextInit = {
db: request.db,
urlOriginal: request.url,
headersOriginal: request.headers,
};
const pageContext = await renderPage(pageContextInit);
const response = pageContext.httpResponse;
reply.statusCode = response.statusCode;
reply.headers(
response.headers.reduce((acc, [key, value]) => {
acc[key] = value;
return acc;
}, {} as Record<string, string>)
);
return reply.send(await response.getReadableNodeStream());
});
}) satisfies FastifyPluginCallback;
export default vikeFastifyPlugin; |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Everything I know about Vike <-> Fastify is written down at https://vike.dev/fastify. Keep us updated if you've stumbled upon some quirk upon integrating Vike with Fastify. FYI many Vike users are using Fastify, so it's likely a user-land issue and/or some special use case on your side. |
Beta Was this translation helpful? Give feedback.
Everything I know about Vike <-> Fastify is written down at https://vike.dev/fastify. Keep us updated if you've stumbled upon some quirk upon integrating Vike with Fastify. FYI many Vike users are using Fastify, so it's likely a user-land issue and/or some special use case on your side.