You can simply disable full page cache by bin/magento cache:disable full_page but it will kill performance. So I don't recommend to do that.
In case you use Fastly - you can use built-in GeoIP redirect feature. It's possible simply configure that. Just google it.
In other case - the right solution for me is to send an AJAX POST request during the first website visit to the custom endpoint. Something like that:
- Customer opens any website page
- You have a custom JS script on every page
- You check the
localStorage.get('geo_ip_processed'). If it's false - send a POST request (POST because the result will not be cached) to the custom endpoint - On the custom backend endpoint, you decide whether the customer should be redirected and where. returns something like
[ 'redirect_to' => 'https://website.com/de/' ] - Then your custom JS script writes
localStorage.set('geo_ip_processed', true) and redirects the customer to the needed URL. If a redirect is unnecessary, you need to set geo_ip_processed to true anyway.
The magic here is that your website can be fully cached, but the only part with GeoIP detection will not be cached because we use POST request which is not cached in Magento by default.