Get Cookie not working

I am using Cloudflare Workers for Geolocation redirection and it is working properly. But now I am additing additional feature on it. For example: If a user from US region will get redirected to US language version based on the Geolocation rule but then a user change the language to englsih global version and close the browser and next time if user revisit the site then it should open the recent close language version i.e. english global.

Steps to reproduce:

  1. So when I visit with url (https://u-cd.wsp-pb.com/) it set the cookie (_langUrl) and then I change the language version it will again set the cookie with latest value.
  2. Close the browser
  3. Revisit the url (https://u-cd.wsp-pb.com/) it won’t redirect to the last visited url which was set in _langUrl Cookie.

A javascript is already added to update the _langUrl cookie value when we change the language version. But unfortunately it didn’t get the cookie value which was recently set when we revisit that url (https://u-cd.wsp-pb.com/).

Below is the code:
addEventListener(“fetch”, event => {
event.respondWith(redirect(event.request))
})

async function redirect(request) {

const cookieString = request.headers.get('cookie') || "" 

if (cookieString) {

return new Response(JSON.stringify({cookieString:cookieString}))
	const allCookies = cookieString.split("; ")
	const targetCookie = allCookies.find(cookie => cookie.includes('_langUrl'))
	if (targetCookie) {
	const [_, value] = targetCookie.split("=")
	return Response.redirect(value)
	}
}
else{
try{
const country = request.cf.country
  const region = request.cf.regionCode

	if (region != null && region in countryRegionMap) {
		const url = countryRegionMap[region]
		return new Response(null, {
			status: 301,
			headers: {
				'Location': url,
				'Set-Cookie': '_langUrl='+url+';SameSite=true; Secure=true'
			}
		});
	}
	
	if (country != null && country in countryRegionMap) {
		const url = countryRegionMap[country]
  const response = url
		return new Response(null, {
			status: 301,
			headers: {
				'Location': url,
				'Set-Cookie': '_langUrl='+url+';SameSite=true; Secure=true'
			}
		});
	} 
	else {
		return fetch(request)
	}
}catch(e){return new Response(JSON.stringify({ error: e.message }))}

}
}

const countryRegionMap = {
QC: “https://u-cd.wsp-pb.com/fr-CA”, //QUEBEC-CANADA
CA: “https://u-cd.wsp-pb.com/en-CA”, //CANADA
CO: “https://u-cd.wsp-pb.com/es-CO”, //COLOMBIA
MX: “https://u-cd.wsp-pb.com/es-MX”, //MEXICO
PA: “https://u-cd.wsp-pb.com/es-PA”, //PANAMA
PE: “Somos WSP| WSP”, //PERU
CL: “Somos WSP| WSP”, //CHILE
US: “We are WSP | WSP”, //UNITED STATES
IN: “https://u-cd.wsp-pb.com/en-IN”, //INDIA
TW: “https://u-cd.wsp-pb.com/zh-CN”, //TAIWAN
CN: “科进 | WSP”, //CHINA
HK: “https://u-cd.wsp-pb.com/en-HK”, //HONG KONG
MY: “https://u-cd.wsp-pb.com/en-MY”, //MALAYSIA
PH: “https://u-cd.wsp-pb.com/en-PH”, //PHILIPPINES
SG: “https://u-cd.wsp-pb.com/en-SG”, //SINGAPORE
KR: “https://u-cd.wsp-pb.com/ko-KR”, //SOUTH KOREA
TH: “https://u-cd.wsp-pb.com/en-TH”, //THAILAND
ZA: “https://u-cd.wsp-pb.com/en-ZA”, //SOUTH AFRICA
NZ: “https://u-cd.wsp-pb.com/en-NZ”, //NEW ZEALAND
AU: “https://u-cd.wsp-pb.com/en-AU”, //AUSTRALIA
AE: “https://u-cd.wsp-pb.com/en-AE”, //UNITED ARAB EMIRATES
SA: “https://u-cd.wsp-pb.com/en-SA”, //SAUDI ARABIA
QA: “https://u-cd.wsp-pb.com/en-QA”, //QATAR
OM: “https://u-cd.wsp-pb.com/en-OM”, //OMAN
KW: “https://u-cd.wsp-pb.com/en-KW”, //KUWAIT
DK: “https://u-cd.wsp-pb.com/da-DK”, //DENMARK
FI: “https://u-cd.wsp-pb.com/fi-FI”, //FINLAND
FR: “https://u-cd.wsp-pb.com/fr-FR”, //FRANCE
DE: “https://u-cd.wsp-pb.com/de-DE”, //GERMANY
NL: “https://u-cd.wsp-pb.com/nl-NL”, //NETHERLANDS
NO: “https://u-cd.wsp-pb.com/nb-NO”, //NORWAY
PL: “https://u-cd.wsp-pb.com/pl-PL”, //POLAND
RO: “https://u-cd.wsp-pb.com/en-RO”, //ROMANIA
ES: “https://u-cd.wsp-pb.com/es-ES”, //SPAIN
SE: “https://u-cd.wsp-pb.com/sv-SE”, //SWEDEN
CH: “https://u-cd.wsp-pb.com/de-CH”, //SWITZERLAND
GB: “https://u-cd.wsp-pb.com/en-GB” //UNITED KINGDOM
}

Hey, I have the same issue. Did you fix it? if so, what did you do?.