Last active
March 16, 2023 07:46
-
-
Save zamson/431924bc367e0b5f0dfb9c0321a21871 to your computer and use it in GitHub Desktop.
Set GEO location visitor country as a cookie with Netlify Edge functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[[edge_functions]] | |
function = "country-code" | |
path = "/*" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment