From 96ed20f391bf7dafc1365e52b9352036e30db71c Mon Sep 17 00:00:00 2001 From: Da-Jin Chu Date: Thu, 29 Jan 2026 10:52:09 -0500 Subject: [PATCH 1/3] pass res to buildpagecontextinit --- bifrost-fastify/index.ts | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/bifrost-fastify/index.ts b/bifrost-fastify/index.ts index cdab042..fab0310 100644 --- a/bifrost-fastify/index.ts +++ b/bifrost-fastify/index.ts @@ -39,7 +39,6 @@ declare module "fastify" { /// Only set when proxy mode is false or wrapped vikePageContext?: Partial | null; getLayout: GetLayout | null; - customPageContextInit: Partial> | null; } } @@ -58,7 +57,8 @@ interface ViteProxyPluginOptions extends Omit< host: URL; onError?: (error: any, pageContext: RenderedPageContext) => void; buildPageContextInit?: ( - req: FastifyRequest + req: FastifyRequest, + res?: RawServerResponse ) => Promise>>; beforeWrappedRender?: ( req: FastifyRequest, @@ -109,7 +109,6 @@ export const viteProxyPlugin: FastifyPluginAsync< fastify.decorateRequest("bifrostSentProxyHeaders", false); fastify.decorateRequest("vikePageContext", null); fastify.decorateRequest("getLayout", null); - fastify.decorateRequest("customPageContextInit", null); await fastify.register(proxy, { ...opts, upstream: upstream.href, @@ -119,14 +118,14 @@ export const viteProxyPlugin: FastifyPluginAsync< (req.method === "GET" || req.method === "HEAD") && req.accepts().type(["html"]) === "html" ) { - req.customPageContextInit = buildPageContextInit + const customPageContextInit = buildPageContextInit ? await buildPageContextInit(req) : {}; const pageContextInit = { urlOriginal: req.url, headersOriginal: req.headers, - ...req.customPageContextInit, + ...customPageContextInit, }; const pageContext = await renderPage(pageContextInit); @@ -263,7 +262,7 @@ export const viteProxyPlugin: FastifyPluginAsync< headInnerHtml, proxyLayoutInfo, } satisfies WrappedServerOnly, - ...req.customPageContextInit, + ...(buildPageContextInit ? await buildPageContextInit(req, res) : {}), }; const pageContext = await renderPage(pageContextInit); req.vikePageContext = pageContext; From 30da6021bd5de966f392abb09e2843b89e70c707 Mon Sep 17 00:00:00 2001 From: Da-Jin Chu Date: Thu, 29 Jan 2026 10:57:00 -0500 Subject: [PATCH 2/3] beforeWrappedRender --- bifrost-fastify/index.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/bifrost-fastify/index.ts b/bifrost-fastify/index.ts index fab0310..cdab042 100644 --- a/bifrost-fastify/index.ts +++ b/bifrost-fastify/index.ts @@ -39,6 +39,7 @@ declare module "fastify" { /// Only set when proxy mode is false or wrapped vikePageContext?: Partial | null; getLayout: GetLayout | null; + customPageContextInit: Partial> | null; } } @@ -57,8 +58,7 @@ interface ViteProxyPluginOptions extends Omit< host: URL; onError?: (error: any, pageContext: RenderedPageContext) => void; buildPageContextInit?: ( - req: FastifyRequest, - res?: RawServerResponse + req: FastifyRequest ) => Promise>>; beforeWrappedRender?: ( req: FastifyRequest, @@ -109,6 +109,7 @@ export const viteProxyPlugin: FastifyPluginAsync< fastify.decorateRequest("bifrostSentProxyHeaders", false); fastify.decorateRequest("vikePageContext", null); fastify.decorateRequest("getLayout", null); + fastify.decorateRequest("customPageContextInit", null); await fastify.register(proxy, { ...opts, upstream: upstream.href, @@ -118,14 +119,14 @@ export const viteProxyPlugin: FastifyPluginAsync< (req.method === "GET" || req.method === "HEAD") && req.accepts().type(["html"]) === "html" ) { - const customPageContextInit = buildPageContextInit + req.customPageContextInit = buildPageContextInit ? await buildPageContextInit(req) : {}; const pageContextInit = { urlOriginal: req.url, headersOriginal: req.headers, - ...customPageContextInit, + ...req.customPageContextInit, }; const pageContext = await renderPage(pageContextInit); @@ -262,7 +263,7 @@ export const viteProxyPlugin: FastifyPluginAsync< headInnerHtml, proxyLayoutInfo, } satisfies WrappedServerOnly, - ...(buildPageContextInit ? await buildPageContextInit(req, res) : {}), + ...req.customPageContextInit, }; const pageContext = await renderPage(pageContextInit); req.vikePageContext = pageContext; From 585701a4459ce8d00b54dfc363ab3fd08c281e99 Mon Sep 17 00:00:00 2001 From: Da-Jin Chu Date: Thu, 29 Jan 2026 13:00:56 -0500 Subject: [PATCH 3/3] ts --- bifrost-fastify/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bifrost-fastify/index.ts b/bifrost-fastify/index.ts index cdab042..1fae502 100644 --- a/bifrost-fastify/index.ts +++ b/bifrost-fastify/index.ts @@ -4,6 +4,7 @@ import { RawServerBase, FastifyPluginAsync, RouteGenericInterface, + RawReplyDefaultExpression, } from "fastify"; import { FastifyRequest, RequestGenericInterface } from "fastify/types/request"; import proxy, { type FastifyHttpProxyOptions } from "@fastify/http-proxy"; @@ -16,7 +17,6 @@ import { extractDomElements } from "./lib/extractDomElements"; import { Http2ServerRequest } from "http2"; import { text } from "node:stream/consumers"; import { parse as parseContentType } from "fast-content-type-parse"; -import { RawServerResponse } from "@fastify/reply-from"; type RenderedPageContext = Awaited< ReturnType< @@ -62,7 +62,7 @@ interface ViteProxyPluginOptions extends Omit< ) => Promise>>; beforeWrappedRender?: ( req: FastifyRequest, - res: RawServerResponse + res: RawReplyDefaultExpression ) => void; } /**