Request insights and visitor endpoints relative to the served origin

This commit is contained in:
Nolann Biron
2026-09-10 12:25:18 +02:00
committed by Nolann B.
parent 1e8c34a426
commit 5ecf746b64
3 changed files with 36 additions and 9 deletions
@@ -0,0 +1,5 @@
---
"gitbook": patch
---
Fix analytics and adaptive content silently breaking on sites served from a different host than the one configured (apex vs www, domain alias, CDN). The insights and visitor-claims endpoints are now requested relative to the page's own origin instead of the configured host, which a prerendered page cannot know.
@@ -58,15 +58,15 @@ export function SpaceLayoutServerContext(props: SpaceLayoutProps) {
? context.linker.toPathInSite('~gitbook/auth/login')
: null;
const eventUrl = new URL(
context.linker.toAbsoluteURL(context.linker.toPathInSite('/~gitbook/__evt'))
);
eventUrl.searchParams.set('o', context.organizationId);
eventUrl.searchParams.set('s', context.site.id);
// Kept relative: a prerendered page has no request to read the host from, so an absolute URL
// pins the configured host and turns these fetches cross-origin when it differs (apex vs www).
const eventParams = new URLSearchParams({
o: context.organizationId,
s: context.site.id,
});
const eventUrl = `${context.linker.toPathInSite('/~gitbook/__evt')}?${eventParams}`;
const getVisitorClaimsUrl = context.linker.toAbsoluteURL(
context.linker.toPathInSite('/~gitbook/visitor')
);
const getVisitorClaimsUrl = context.linker.toPathInSite('/~gitbook/visitor');
return (
<SpaceLayoutContextProvider
@@ -92,7 +92,7 @@ export function SpaceLayoutServerContext(props: SpaceLayoutProps) {
appURL={GITBOOK_APP_URL}
visitorCookieTrackingEnabled={customization.insights?.trackingCookie}
>
<InsightsProvider enabled={withTracking} eventUrl={eventUrl.toString()}>
<InsightsProvider enabled={withTracking} eventUrl={eventUrl}>
<AIChatProvider
renderMessageOptions={aiChatRenderMessageOptions}
withPageFeedback={customization.feedback.enabled}
+22
View File
@@ -0,0 +1,22 @@
import { describe, expect, it } from 'bun:test';
import { getContentTestURL } from './utils';
describe('Insights', () => {
// The harness serves the site from BASE_URL while it is configured on gitbook.gitbook.io,
// reproducing the split (apex vs www, alias, CDN) that turns an absolute URL cross-origin.
const TEST_URL = getContentTestURL('https://gitbook.gitbook.io/test-gitbook-open');
it.each(['__evt', 'visitor'])(
'should reference ~gitbook/%s relative to the served origin',
async (endpoint) => {
const response = await fetch(TEST_URL);
expect(response.status).toBe(200);
const html = await response.text();
expect(html).toContain(`~gitbook/${endpoint}`);
expect(html).not.toMatch(new RegExp(`https?://[^"'\\\\\\s]*~gitbook/${endpoint}`));
}
);
});