Recipes
Geo-restrict and redirect users
Redirect visitors to /en/ or /de/ (or other locale paths) based on their detected country using a Surface workflow and 302 redirects.
What you'll build
An HTTP surface with a surface-level workflow that inspects the visitor's country and returns a 302 redirect to the appropriate locale path. German-speaking countries (DE, AT, CH) go to /de/ all others go to /en/
1. Prerequisites
2. Add a surface-level workflow
const : <string, string> = {
: "/de/",
: "/de/",
: "/de/",
: "/fr/",
: "/es/",
: "/pt/",
};
const = [
...new ([
"en",
....().(() => .(/^\/|\/$/g, "")),
]),
];
const : = async ({ , }) => {
const = (?. as string) ?? "";
const = .("/").()[0];
const =
&& .();
if () return {}; // No redirect—already on a locale path
const = .?.() ?? null;
const = ( && []) ?? "/en/";
return {
: {
: 302,
: "Found",
: { : },
: null,
},
};
};
export default ;Result
When a request hits the Edge Worker for this surface, the workflow runs and returns a 302 redirect. Visitors from Germany, Austria, or Switzerland are sent to /de/ all others to /en/