Skip to content

Instantly share code, notes, and snippets.

@zamson
Last active March 16, 2023 07:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zamson/431924bc367e0b5f0dfb9c0321a21871 to your computer and use it in GitHub Desktop.
Save zamson/431924bc367e0b5f0dfb9c0321a21871 to your computer and use it in GitHub Desktop.
Set GEO location visitor country as a cookie with Netlify Edge functions
export default async (request, context) => {
const countryCode = context.geo?.country?.code
const url = new URL(request.url)
if (!context.cookies.get('country')) {
context.cookies.set({
name: 'country',
value: countryCode,
path: '/',
secure: true,
sameSite: 'none',
expires: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000), // set cookie to expire in 7 days (1 week)
})
}
return context.rewrite(url)
}
[[edge_functions]]
function = "country-code"
path = "/*"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment