From 5b2bf828e31c68209141771b7884c72bbe1a560f Mon Sep 17 00:00:00 2001 From: Taran Vohra Date: Thu, 27 Mar 2025 18:26:06 +0530 Subject: [PATCH] Use stable site URL data for route rewrite in the middleware (#3053) --- .changeset/shiny-hats-love.md | 5 +++++ packages/gitbook-v2/src/middleware.ts | 32 ++++++++++++++++++++++----- 2 files changed, 31 insertions(+), 6 deletions(-) create mode 100644 .changeset/shiny-hats-love.md diff --git a/.changeset/shiny-hats-love.md b/.changeset/shiny-hats-love.md new file mode 100644 index 000000000..223d684f5 --- /dev/null +++ b/.changeset/shiny-hats-love.md @@ -0,0 +1,5 @@ +--- +"gitbook-v2": patch +--- + +Use stable site URL data for route rewrite in the middleware diff --git a/packages/gitbook-v2/src/middleware.ts b/packages/gitbook-v2/src/middleware.ts index 6e1d855fa..b16d5ed9a 100644 --- a/packages/gitbook-v2/src/middleware.ts +++ b/packages/gitbook-v2/src/middleware.ts @@ -194,18 +194,38 @@ async function serveSiteRoutes(requestURL: URL, request: NextRequest) { ); routeType = routeTypeFromPathname ?? routeType; + // We pick only stable data from the siteURL data to prevent re-rendering of + // the root layout when changing pages.. + const stableSiteURLData: Omit = { + site: siteURLData.site, + siteSection: siteURLData.siteSection, + siteSpace: siteURLData.siteSpace, + siteBasePath: siteURLData.siteBasePath, + basePath: siteURLData.basePath, + space: siteURLData.space, + organization: siteURLData.organization, + changeRequest: siteURLData.changeRequest, + revision: siteURLData.revision, + shareKey: siteURLData.shareKey, + apiToken: siteURLData.apiToken, + complete: siteURLData.complete, + contextId: siteURLData.contextId, + }; + const route = [ 'sites', routeType, mode, encodeURIComponent(siteURLWithoutProtocol), encodeURIComponent( - rison.encode({ - ...siteURLData, - // The pathname is passed as the next segment of the route and should not cause this segment to change - // based on the page being visited - pathname: '', - }) + rison.encode( + // rison can't encode undefined values + Object.fromEntries( + Object.entries(stableSiteURLData).filter( + ([_, v]) => typeof v !== 'undefined' + ) + ) + ) ), pathname, ].join('/');