From f58783c2600921e81dd934fc3c630fc2410543e5 Mon Sep 17 00:00:00 2001 From: Nicolas Dorseuil Date: Wed, 7 Oct 2026 09:35:49 +0200 Subject: [PATCH] Enhance visitor country handling by prioritizing forwarded header on revalidation requests --- .changeset/send-visitor-country.md | 2 +- packages/gitbook/src/lib/visitors.test.ts | 32 +++++++++++++++++++++++ packages/gitbook/src/lib/visitors.ts | 5 +++- 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/.changeset/send-visitor-country.md b/.changeset/send-visitor-country.md index a8af8037b..7b2669f47 100644 --- a/.changeset/send-visitor-country.md +++ b/.changeset/send-visitor-country.md @@ -2,4 +2,4 @@ "gitbook": patch --- -Send the visitor country to resolvePublishedContentByUrl on dev, preview, staging and selected sites. +Send the visitor country to resolvePublishedContentByUrl on dev, preview, staging and selected sites, using the forwarded `x-gitbook-country` header on revalidation requests. diff --git a/packages/gitbook/src/lib/visitors.test.ts b/packages/gitbook/src/lib/visitors.test.ts index 110c7fdfa..0a583d330 100644 --- a/packages/gitbook/src/lib/visitors.test.ts +++ b/packages/gitbook/src/lib/visitors.test.ts @@ -628,6 +628,38 @@ describe('getVisitorCountry', () => { it('should return undefined when no country header is present', () => { expect(getVisitorCountry(requestWith({}))).toBeUndefined(); }); + + it('should prefer the forwarded country header on revalidation requests', () => { + expect( + getVisitorCountry( + requestWith({ + 'user-agent': 'gitbook-open-revalidation-worker', + 'x-gitbook-country': 'DE', + 'x-open-next-country': 'FR', + }) + ) + ).toBe('DE'); + }); + + it('should ignore the forwarded country header outside revalidation requests', () => { + expect( + getVisitorCountry( + requestWith({ 'x-gitbook-country': 'DE', 'x-open-next-country': 'FR' }) + ) + ).toBe('FR'); + expect(getVisitorCountry(requestWith({ 'x-gitbook-country': 'DE' }))).toBeUndefined(); + }); + + it('should fall back to geolocation headers on revalidation requests without a forwarded country', () => { + expect( + getVisitorCountry( + requestWith({ + 'user-agent': 'gitbook-open-revalidation-worker', + 'x-open-next-country': 'FR', + }) + ) + ).toBe('FR'); + }); }); describe('shouldSendVisitorCountry', () => { diff --git a/packages/gitbook/src/lib/visitors.ts b/packages/gitbook/src/lib/visitors.ts index 405342860..e65f855e8 100644 --- a/packages/gitbook/src/lib/visitors.ts +++ b/packages/gitbook/src/lib/visitors.ts @@ -124,10 +124,13 @@ export function shouldSendVisitorCountry( } /** - * Get the ISO 3166-1 alpha-2 country code of the visitor from the geolocation headers. + * Get the ISO 3166-1 alpha-2 country code of the visitor from the geolocation headers, + * or from `x-gitbook-country` when the request comes from the revalidation worker. */ export function getVisitorCountry(request: { headers: Headers }): string | undefined { + // The revalidation worker forwards the original visitor country; only trust it from there. const country = ( + (isRevalidationRequest(request.headers) && request.headers.get('x-gitbook-country')) || request.headers.get('x-open-next-country') || request.headers.get('x-vercel-ip-country') || ''