Use stable site URL data for route rewrite in the middleware (#3053)

This commit is contained in:
Taran Vohra
2025-03-27 18:26:06 +05:30
committed by GitHub
parent c85d3f9752
commit 5b2bf828e3
2 changed files with 31 additions and 6 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"gitbook-v2": patch
---
Use stable site URL data for route rewrite in the middleware
+26 -6
View File
@@ -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<typeof siteURLData, 'pathname'> = {
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: '<DO_NOT_USE>',
})
rison.encode(
// rison can't encode undefined values
Object.fromEntries(
Object.entries(stableSiteURLData).filter(
([_, v]) => typeof v !== 'undefined'
)
)
)
),
pathname,
].join('/');