Skip to content

Open source CapCut alternative.

TypeScript

refactor(site): read the visitor's country without Prisma

David Le623f89eee799
diff --git a/site/src/lib/config/country.ts b/site/src/lib/config/country.ts
new file mode 100644
index 00000000..4b17cec3
--- /dev/null
+++ b/site/src/lib/config/country.ts
@@ -0,0 +1,8 @@
+// The visitor's country, as the edge reports it. Free of server imports, so
+// the proxy reads it too.
+const COUNTRY_HEADER = "x-vercel-ip-country";
+
+export function countryFromHeaders(headers: Headers): string | null {
+ const raw = headers.get(COUNTRY_HEADER)?.trim().toUpperCase();
+ return raw && /^[A-Z]{2}$/.test(raw) ? raw : null;
+}
diff --git a/site/src/lib/config/effective.ts b/site/src/lib/config/effective.ts
index 8b10a15e..fcceee91 100644
--- a/site/src/lib/config/effective.ts
+++ b/site/src/lib/config/effective.ts
@@ -2,6 +2,7 @@ import type { Prisma } from "@/generated/prisma/client";
import { isEligible, pickVariant } from "@donkeycut/abexp";
import { audienceNeeds, audienceSchema, type Audience, type AudienceFact } from "@donkeycut/abexp";
import { collectFacts } from "@/lib/config/audienceFacts";
+import { countryFromHeaders } from "@/lib/config/country";
import { variantSchema, type ExperimentVariant } from "@/lib/config/experiment";
import type { SettingKey, Settings } from "@/lib/config/registry";
import { resolveSettings } from "@/lib/config/resolve";
@@ -36,13 +37,6 @@ export type EffectiveConfig = {
invalid: { key: SettingKey; layer: "override" | "variant" }[];
};
-const COUNTRY_HEADER = "x-vercel-ip-country";
-
-export function countryFromHeaders(headers: Headers): string | null {
- const raw = headers.get(COUNTRY_HEADER)?.trim().toUpperCase();
- return raw && /^[A-Z]{2}$/.test(raw) ? raw : null;
-}
-
export function contextFromRequest(request: DonkeyAuthenticatedRequest): Promise<ConfigContext> {
return contextForUser(request.donkey.userId, countryFromHeaders(request.headers));
}