From e23481dcf8ccf6043267148e08d49e323708a5af Mon Sep 17 00:00:00 2001 From: Nolann Biron Date: Thu, 10 Sep 2026 13:51:18 +0200 Subject: [PATCH] Simplify shouldTrackEvents signature and the insights test --- .../[siteURL]/[siteData]/(content)/layout.tsx | 5 +---- .../[siteData]/~gitbook/embed/layout.tsx | 5 +---- .../[siteURL]/[siteData]/(content)/layout.tsx | 2 +- .../[siteData]/~gitbook/embed/layout.tsx | 2 +- packages/gitbook/src/lib/tracking.ts | 15 ++++++--------- packages/gitbook/tests/insights.test.ts | 13 ++++++------- 6 files changed, 16 insertions(+), 26 deletions(-) diff --git a/packages/gitbook/src/app/sites/dynamic/[mode]/[siteURL]/[siteData]/(content)/layout.tsx b/packages/gitbook/src/app/sites/dynamic/[mode]/[siteURL]/[siteData]/(content)/layout.tsx index 6adf593a5..340b77cce 100644 --- a/packages/gitbook/src/app/sites/dynamic/[mode]/[siteURL]/[siteData]/(content)/layout.tsx +++ b/packages/gitbook/src/app/sites/dynamic/[mode]/[siteURL]/[siteData]/(content)/layout.tsx @@ -21,10 +21,7 @@ export default async function SiteDynamicLayout({ const resolvedParams = await params; const { context, visitorAuthClaims } = await getDynamicSiteContext(resolvedParams); const forcedTheme = await getThemeFromMiddleware(); - const withTracking = shouldTrackEvents({ - mode: resolvedParams.mode, - headers: await headers(), - }); + const withTracking = shouldTrackEvents(resolvedParams.mode, await headers()); return ( ) { const resolvedParams = await params; const { context, visitorAuthClaims } = await getEmbeddableStaticContext(resolvedParams); - const withTracking = shouldTrackEvents({ - mode: resolvedParams.mode, - headers: await headers(), - }); + const withTracking = shouldTrackEvents(resolvedParams.mode, await headers()); // The forced theme (`?theme=`) comes through the route context (set by the middleware), not a // request header, so the embed can honor it while staying statically rendered. RND-11571 const forcedTheme = getSiteURLDataFromParams(resolvedParams).embedTheme ?? null; diff --git a/packages/gitbook/src/app/sites/static/[mode]/[siteURL]/[siteData]/(content)/layout.tsx b/packages/gitbook/src/app/sites/static/[mode]/[siteURL]/[siteData]/(content)/layout.tsx index ca6676baf..d7971da22 100644 --- a/packages/gitbook/src/app/sites/static/[mode]/[siteURL]/[siteData]/(content)/layout.tsx +++ b/packages/gitbook/src/app/sites/static/[mode]/[siteURL]/[siteData]/(content)/layout.tsx @@ -17,7 +17,7 @@ export default async function SiteStaticLayout({ }: React.PropsWithChildren) { const resolvedParams = await params; const { context, visitorAuthClaims } = await getStaticSiteContext(resolvedParams); - const withTracking = shouldTrackEvents({ mode: resolvedParams.mode }); + const withTracking = shouldTrackEvents(resolvedParams.mode); return ( ) { const resolvedParams = await params; const { context, visitorAuthClaims } = await getEmbeddableStaticContext(resolvedParams); - const withTracking = shouldTrackEvents({ mode: resolvedParams.mode }); + const withTracking = shouldTrackEvents(resolvedParams.mode); // The forced theme (`?theme=`) is threaded through the route context by the middleware so the // embed stays statically rendered — read it from the params rather than a request header. RND-11571 const forcedTheme = getSiteURLDataFromParams(resolvedParams).embedTheme ?? null; diff --git a/packages/gitbook/src/lib/tracking.ts b/packages/gitbook/src/lib/tracking.ts index 061f7f1c2..1b1b2da55 100644 --- a/packages/gitbook/src/lib/tracking.ts +++ b/packages/gitbook/src/lib/tracking.ts @@ -13,24 +13,21 @@ import { getLogger } from './logger'; * In the dynamic context, the request headers are checked too - this allows the middleware * to disable tracking for preview requests. */ -export function shouldTrackEvents(args: { - /** Serving mode, from the route params. */ - mode: string; - headers?: Awaited>; -}): boolean { +export function shouldTrackEvents( + mode: string, + headers?: Awaited> +): boolean { if (GITBOOK_DISABLE_TRACKING) { return false; } // `url` mode only serves `/url/:url` on GitBook's own host — local dev and preview // deployments. That traffic is not the site's, so it must stay out of its analytics. - if (args.mode === 'url') { + if (mode === 'url') { return false; } - const disableTrackingHeader = args.headers?.get('x-gitbook-disable-tracking'); - - if (disableTrackingHeader === 'true') { + if (headers?.get('x-gitbook-disable-tracking') === 'true') { return false; } diff --git a/packages/gitbook/tests/insights.test.ts b/packages/gitbook/tests/insights.test.ts index d98598c2c..333b4414b 100644 --- a/packages/gitbook/tests/insights.test.ts +++ b/packages/gitbook/tests/insights.test.ts @@ -7,16 +7,15 @@ describe('Insights', () => { // 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); + it('should reference the insights endpoints relative to the served origin', async () => { + const response = await fetch(TEST_URL); + expect(response.status).toBe(200); - const html = await response.text(); + const html = await response.text(); + for (const endpoint of ['__evt', 'visitor']) { expect(html).toContain(`~gitbook/${endpoint}`); expect(html).not.toMatch(new RegExp(`https?://[^"'\\\\\\s]*~gitbook/${endpoint}`)); } - ); + }); });