fix: country composable not working due to nuxt routing. (#3623)

Closes: #2263
This commit is contained in:
Calum H. 2025-05-07 22:01:25 +01:00 committed by GitHub
parent 32920dd825
commit 8ee621295c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 36 additions and 6 deletions

View File

@ -1,6 +0,0 @@
export const useUserCountry = () =>
useState("userCountry", () => {
const headers = useRequestHeaders(["cf-ipcountry"]);
return headers["cf-ipcountry"] ?? "US";
});

View File

@ -0,0 +1,36 @@
import { useState, useRequestHeaders } from "#imports";
export const useUserCountry = () => {
const country = useState<string>("userCountry", () => "US");
const fromServer = useState<boolean>("userCountryFromServer", () => false);
if (import.meta.server) {
const headers = useRequestHeaders(["cf-ipcountry", "accept-language"]);
const cf = headers["cf-ipcountry"];
if (cf) {
country.value = cf.toUpperCase();
fromServer.value = true;
} else {
const al = headers["accept-language"] || "";
const tag = al.split(",")[0];
const val = tag.split("-")[1]?.toLowerCase();
if (val) {
country.value = val;
fromServer.value = true;
}
}
}
if (import.meta.client) {
onMounted(() => {
if (fromServer.value) return;
const lang = navigator.language || navigator.userLanguage || "";
const region = lang.split("-")[1];
if (region) {
country.value = region.toUpperCase();
}
});
}
return country;
};