How can I use geolocation IP headers?
👋 Hello!
I’m using SvelteKit with the
@sveltejs/adapter-cloudflare
, deploying to Cloudflare Pages.
Some websites show a nice touch like:
On Vercel, I’ve seen this done easily with:
Is there an equivalent or best practice for doing this on Cloudflare Pages?
Any pointers or examples would be much appreciated!
---4 Replies
you can get geo location info in the Request#cf object - https://developers.cloudflare.com/workers/runtime-apis/request/#incomingrequestcfproperties
I don't know how to get that from within Svelte but hopefully should be easy to find out
Hey, @Walshy
Thank you. Got it to work. Maybe I should compile a list of the resources I used and how I actually got there into a blog post? 🤔 Will see.
As for the actual code, it was rather minimal:
That said, I have some concerns. Would appreciate any pointers.
1. Having
await
seems to be the only way to reliably store/retrieve the visitor, but won't that incur any performance penalties?
2. Is there anything you would do differently from what I have thus far?
3. Would the same work if I were to move to workers? Keep seeing the recommendation to move from Pages.KV is fast, there's not really a way to store a persistent thing without some perf loss, unless you store in memory but then you're in a distributed system so memory for one request is usually not the same for another.
Is there anything you would do differently from what I have thus far?I think this looks good
Would the same work if I were to move to workers? Keep seeing the recommendation to move from Pages.yeah it'd be the exact same code Pages Functions are literally Workers
Appreciate the help. That answers my questions.
Cheers.