Simplify shouldTrackEvents signature and the insights test

This commit is contained in:
Nolann Biron
2026-09-10 13:51:18 +02:00
parent 031a2e9a56
commit e23481dcf8
6 changed files with 16 additions and 26 deletions
@@ -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 (
<CustomizationRootLayout
@@ -19,10 +19,7 @@ export default async function RootLayout({
}: React.PropsWithChildren<SiteStaticLayoutProps>) {
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;
@@ -17,7 +17,7 @@ export default async function SiteStaticLayout({
}: React.PropsWithChildren<SiteStaticLayoutProps>) {
const resolvedParams = await params;
const { context, visitorAuthClaims } = await getStaticSiteContext(resolvedParams);
const withTracking = shouldTrackEvents({ mode: resolvedParams.mode });
const withTracking = shouldTrackEvents(resolvedParams.mode);
return (
<CustomizationRootLayout
@@ -17,7 +17,7 @@ export default async function RootLayout({
}: React.PropsWithChildren<SiteStaticLayoutProps>) {
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;
+6 -9
View File
@@ -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<ReturnType<typeof nextHeaders>>;
}): boolean {
export function shouldTrackEvents(
mode: string,
headers?: Awaited<ReturnType<typeof nextHeaders>>
): 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;
}
+6 -7
View File
@@ -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}`));
}
);
});
});