Polyscope - The agent-first dev environment for Laravel

Geocoding with Cloudflare

Last updated on by

Geocoding with Cloudflare image

I like to offer Laravel Shift for "20 bucks". But $20 may be more or less depending on where you live. So if you're in a country where the value of your dollar is 50% less than USD, I offer purchasing power parity.

To determine the country you live in, I use a geocoding service. One that translates your IP address to your country. If that country is one where I offer purchasing power, you see a banner at the top to activate your discount.

Now, I've been on a quest to make the laravelshift.com as fast as possible using Cloudflare. Along the way I stumbled upon another one of their free services - geocoding. Cloudflare appends a country header to the request before forwarding to your web server.

The CF-IPCountry header is a 2 character country code. For example, US or UK. In my case, I was able to use this header directly. Then map the code to a country name to check for purchasing power.

This allowed me to do two things. First, it allowed me to remove hundreds of lines of code. Second, it allowed me to cache the page.

Since the page previously had logic to check the request headers, it was too dynamic to cache. Instead, I sent an API call to check the country after the page load.

All of this was not only more code, but an additional, albeit lightweight, request.

But since this header is available at the Cloudflare level, I was actually able to create a transformation rule. Within it I could check the country code and append a query string to the request.

This allows me to cache two versions of the page. One for the default page display, and another displaying the purchasing power banner.

Again, since this is opt in, your location header is double checked before activating the discount.

If you're interested in seeing the full code refactor, I livestreamed this for my Confident Laravel testing course..

If you're interested in making your page load super fast, check out my video course Fast Laravel.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Bacancy

Outsource a dedicated Laravel developer for $3,200/month. With over a decade of experience in Laravel development, we deliver fast, high-quality, and cost-effective solutions at affordable rates.

Visit Bacancy
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $3200/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Get expert guidance in a few days with a Laravel code review logo

Get expert guidance in a few days with a Laravel code review

Expert code review! Get clear, practical feedback from two Laravel devs with 10+ years of experience helping teams build better apps.

Get expert guidance in a few days with a Laravel code review
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit

The latest

View all →
The Inertia v3 Beta is Here image

The Inertia v3 Beta is Here

Read article
Polyscope Is an Ai-First Dev Environment for Orchestrating Agents image

Polyscope Is an Ai-First Dev Environment for Orchestrating Agents

Read article
Filament v5.3.0 Released with Deferred Tab Badges and Column Manager Improvements image

Filament v5.3.0 Released with Deferred Tab Badges and Column Manager Improvements

Read article
Ward: A Security Scanner for Laravel image

Ward: A Security Scanner for Laravel

Read article
Kit: An Opinionated API Starter Kit for Laravel image

Kit: An Opinionated API Starter Kit for Laravel

Read article
Livewire v4.2.0 Released with Security Hardening and Laravel 13 Support image

Livewire v4.2.0 Released with Security Hardening and Laravel 13 Support

Read article