I’m implementing GrowthBook with Cloudflare Edge, ...
# ask-questions
w
I’m implementing GrowthBook with Cloudflare Edge, and everything is working great. However, I’d like to pass the enabled features to my origin server, ideally as a cookie. The issue is that headers are immutable, causing the Edge app to crash. Is there a way to achieve this, or should I instantiate GrowthBook within the fetch function as described here?https://docs.growthbook.io/lib/edge/cloudflare#manual-sdk-integration-on-edge
Copy code
return 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 work
h
I think you can probably achieve what you're looking for by providing your own
helpers.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.
alternately, you could parse the features before even calling
handleRequest()
, construct a new
request
with the desired headers, and then pass that into
handleRequest(request...
w
It seems impossible. I tried all the scenarios you described, hardcoding the headers, but my application still doesn't receive them. I even created an endpoint to output all headers, and when I send a GET request with Postman using modified headers, they appear correctly.
Copy code
return 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 });
			},
		});
Will try again tomorrow 😵‍💫
h
I guess the naive question I have would be whether console.log on the outgoing request (within the worker / helper method) shows mutated headers or not?
also have you tried constructing a
const newRequest = new Request({ method, headers, body })
with
const headers = new Headers(request.headers)
, and then passing that into fetch directly like
return fetch(newRequest)
?
w
Ah, I finally found the solution! I was overwriting the
helpers.fetch
function, but I got the expected behavior when I overwrote
proxyRequest
instead.