I have a Cloudflare Worker which uses
request.cf.country (as outlined in the docs). This works great when the client makes the request directly.
But I also have a server which does server-side rendering. That server calls the same Cloudflare Worker during server-side rendering and I’d like the
request.cf.country to match the original visitor’s information, instead of the information of my own server.
Is there a way to pass along the original IP address using a header like
X-Forwarded-For to make Cloudflare resolve the
request.cf of that IP instead of the one from the request?
Instead of calling another worker, can’t you just send the original request (or a reconstructed request) to the appropriate function that handles logic ?
I wanted to keep the question short, but I might have ended up providing too little information. I’m offering a feature flagging service, so the request to my Cloudflare Worker is not made within Cloudflare’s infrastructure. I provide a library which my customers integrate and use during server-side rendering.
The full flow is
- visitor opens example dot com
- example dot com’s Node.js server receives request and starts server-side rendering
- during server-side rendering a request to my Cloudflare Worker is made
- the response of my Cloudflare Worker is used to generate the example dot com site
As the request to the Cloudflare Worker is made during server-side rendering on the Node.js server, I have access to the original request made by the visitor’s browser. I can forward any information necessary, but I couldn’t find any info of how to use that to my advantage.
If this is not possible I’ll probably pass the original IP along in the request body and use a third-party Geolocation API within the Cloudflare Worker to get the information based on the visitor IP. But it would have been epic if I could let Cloudflare know about the original IP and have it resolve the IncomingRequestCfProperties (country, language, timezone etc) based on that.
I had to use the weird dot com-notation as I’m a new user and I’m not allowed to post “links”.