Saeven
Saeven
CDCloudflare Developers
Created by Saeven on 2/1/2024 in #workers-help
Workers a good solution to adding GeoCookies?
// Fetch the request and pass it through the function
addEventListener('fetch', event => {
event.respondWith(fetchAndApply(event.request))
});

async function fetchAndApply(request){

let response = await fetch(request);
response = new Response(response.body, response);

// create header in format you like using `request.cf.*` fields - https://developers.cloudflare.com/workers/runtime-apis/request/#incomingrequestcfproperties
response.headers.append("Set-Cookie", `llxp_geo_country=${request.cf.country}; path=/; secure; SameSite=None`);
response.headers.append("Set-Cookie", `llxp_geo_region=${request.cf.regionCode}; path=/; secure; SameSite=None`);

// assumes the worker sits in front of the site you're proxying, this fetch will go to the origin
// if it doesn't, you may need to manipulate the URL here
return response;
}
// Fetch the request and pass it through the function
addEventListener('fetch', event => {
event.respondWith(fetchAndApply(event.request))
});

async function fetchAndApply(request){

let response = await fetch(request);
response = new Response(response.body, response);

// create header in format you like using `request.cf.*` fields - https://developers.cloudflare.com/workers/runtime-apis/request/#incomingrequestcfproperties
response.headers.append("Set-Cookie", `llxp_geo_country=${request.cf.country}; path=/; secure; SameSite=None`);
response.headers.append("Set-Cookie", `llxp_geo_region=${request.cf.regionCode}; path=/; secure; SameSite=None`);

// assumes the worker sits in front of the site you're proxying, this fetch will go to the origin
// if it doesn't, you may need to manipulate the URL here
return response;
}
20 replies
CDCloudflare Developers
Created by Saeven on 2/1/2024 in #workers-help
Workers a good solution to adding GeoCookies?
Thanks for pointing me in the right direction. I was able to get the data into a cookie like so:
20 replies
CDCloudflare Developers
Created by Saeven on 2/1/2024 in #workers-help
Workers a good solution to adding GeoCookies?
Makes sense - thank you so much
20 replies
CDCloudflare Developers
Created by Saeven on 2/1/2024 in #workers-help
Workers a good solution to adding GeoCookies?
sounds like a good idea - I've never authored a Worker. Have only really used CF for WAF and basic network tasks. The worker example code I've found seems to serve pages outright. Are there any that deal with setting cookies whilst still serving the request from the host?
20 replies
CDCloudflare Developers
Created by Saeven on 2/1/2024 in #workers-help
Workers a good solution to adding GeoCookies?
I agree! Just one of those crazy large-corp shenanigans.
20 replies
CDCloudflare Developers
Created by Saeven on 2/1/2024 in #workers-help
Workers a good solution to adding GeoCookies?
appreciate the gesture!
20 replies
CDCloudflare Developers
Created by Saeven on 2/1/2024 in #workers-help
Workers a good solution to adding GeoCookies?
oh interesting
20 replies
CDCloudflare Developers
Created by Saeven on 2/1/2024 in #workers-help
Workers a good solution to adding GeoCookies?
Is this something Workers can do, or am I barking up the wrong tree?
20 replies
CDCloudflare Developers
Created by Saeven on 2/1/2024 in #workers-help
Workers a good solution to adding GeoCookies?
Yeah I had the same want. I do see that CF provides headers, and I could print the headers into script through a server-side transform - but the client wants this in a cookie (which would have to be set preflight)
20 replies