From 56aeb19e49ba11e05d895475dfd10bcd519e1727 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samy=20Pess=C3=A9?= Date: Thu, 7 May 2026 23:15:34 +0200 Subject: [PATCH] Fix events tracked from the middleware missing some properties (#4242) --- .changeset/warm-rabbits-heal.md | 5 +++++ packages/gitbook/src/middleware.ts | 22 ++++++++++++++++------ 2 files changed, 21 insertions(+), 6 deletions(-) create mode 100644 .changeset/warm-rabbits-heal.md diff --git a/.changeset/warm-rabbits-heal.md b/.changeset/warm-rabbits-heal.md new file mode 100644 index 000000000..ce5300cc3 --- /dev/null +++ b/.changeset/warm-rabbits-heal.md @@ -0,0 +1,5 @@ +--- +"gitbook": patch +--- + +Fix events tracked from the middleware missing some properties. diff --git a/packages/gitbook/src/middleware.ts b/packages/gitbook/src/middleware.ts index 168388cb2..63db52b93 100644 --- a/packages/gitbook/src/middleware.ts +++ b/packages/gitbook/src/middleware.ts @@ -1,6 +1,8 @@ import { CustomizationThemeMode, + type PublishedSiteContent, SiteInsightsDisplayContext, + type SiteInsightsEventLocation, SiteInsightsLLMSVariant, } from '@gitbook/api'; import { isAIAgent } from '@vercel/agent-readability'; @@ -441,7 +443,7 @@ async function serveSiteRoutes(requestURL: URL, request: NextRequest) { pathname, routeType: routeTypeFromPathname, events, - } = encodePathInSiteContent(siteURLData.pathname, request); + } = encodePathInSiteContent(siteURLData, request); routeType = routeTypeFromPathname ?? routeType; if (events && events.length > 0) { @@ -680,14 +682,23 @@ const PATH_ALIASES: Record = { * Special paths are not encoded and passed to be handled by the route handlers. */ function encodePathInSiteContent( - rawPathname: string, + siteURLData: PublishedSiteContent, request: Request ): { pathname: string; routeType?: 'static' | 'dynamic'; events?: ServerInsightsEventInput[] | undefined; } { - let pathname = removeLeadingSlash(removeTrailingSlash(rawPathname)); + let pathname = removeLeadingSlash(removeTrailingSlash(siteURLData.pathname)); + + const eventLocation: Partial = { + siteSection: siteURLData.siteSection, + siteSpace: siteURLData.siteSpace, + siteShareKey: siteURLData.shareKey, + space: siteURLData.space, + revision: siteURLData.revision, + displayContext: SiteInsightsDisplayContext.Server, + }; if (pathname.match(/^~gitbook\/ogimage\/\S+$/)) { return { pathname }; @@ -702,9 +713,7 @@ function encodePathInSiteContent( events: [ { type: 'rss_request', - location: { - displayContext: SiteInsightsDisplayContext.Server, - }, + location: eventLocation, }, ], }; @@ -720,6 +729,7 @@ function encodePathInSiteContent( type: 'llms_request', llmsVariant: SiteInsightsLLMSVariant.Full, location: { + siteSection: siteURLData.siteSection, displayContext: SiteInsightsDisplayContext.Server, }, },