witty-carpet-85180
03/06/2025, 8:01 AMreturn await handleRequest(request, env, config, {
onBeforeOriginFetch: (ctx) => {
const { growthbook } = ctx;
const features = Object.keys(growthbook.getFeatures()).map(
(featureKey) => ({
feature: featureKey,
isEnabled: growthbook.isOn(featureKey),
})
);
const featuresString = encodeURIComponent(JSON.stringify(features));
const newHeaders = new Headers(ctx.req.headers);
newHeaders.append("X-Growthbook-Features", featuresString);
ctx.req.headers = newHeaders;
},
});
Note: appending the header to ctrx.req.headers also don't workhappy-autumn-40938
03/06/2025, 8:59 AMhelpers.fetch
function, which is what is used to call the origin. In the new function, you could clone the original request via const newReq = new Request(request, {headers: { ...yourNewHeaders }})
. You may need to stuff the desired headers into context
(and suppress any sort of typescript warnings if using TS) so that you can retrieve them inside the helper fetch.happy-autumn-40938
03/06/2025, 9:01 AMhandleRequest()
, construct a new request
with the desired headers, and then pass that into handleRequest(request...
witty-carpet-85180
03/06/2025, 7:47 PMwitty-carpet-85180
03/06/2025, 7:56 PMreturn await handleRequest(request, env, config, undefined, {
fetch(ctx: Context<Request, Response>, url: string, req: Request) {
const headers = new Headers(req.headers);
headers.set('x-growthbook-features', 'test');
if (ctx.config.followRedirects) {
return fetch(url, { ...req, headers, redirect: 'follow' });
}
return fetch(url, { ...req, headers });
},
});
witty-carpet-85180
03/06/2025, 8:53 PMhappy-autumn-40938
03/07/2025, 12:59 AMhappy-autumn-40938
03/07/2025, 1:38 AMconst newRequest = new Request({ method, headers, body })
with const headers = new Headers(request.headers)
, and then passing that into fetch directly like return fetch(newRequest)
?witty-carpet-85180
03/07/2025, 10:08 AMhelpers.fetch
function, but I got the expected behavior when I overwrote proxyRequest
instead.