Skip to content

Commit 5b2bf82

Browse files
authored
Use stable site URL data for route rewrite in the middleware (#3053)
1 parent c85d3f9 commit 5b2bf82

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

.changeset/shiny-hats-love.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"gitbook-v2": patch
3+
---
4+
5+
Use stable site URL data for route rewrite in the middleware

packages/gitbook-v2/src/middleware.ts

+26-6
Original file line numberDiff line numberDiff line change
@@ -194,18 +194,38 @@ async function serveSiteRoutes(requestURL: URL, request: NextRequest) {
194194
);
195195
routeType = routeTypeFromPathname ?? routeType;
196196

197+
// We pick only stable data from the siteURL data to prevent re-rendering of
198+
// the root layout when changing pages..
199+
const stableSiteURLData: Omit<typeof siteURLData, 'pathname'> = {
200+
site: siteURLData.site,
201+
siteSection: siteURLData.siteSection,
202+
siteSpace: siteURLData.siteSpace,
203+
siteBasePath: siteURLData.siteBasePath,
204+
basePath: siteURLData.basePath,
205+
space: siteURLData.space,
206+
organization: siteURLData.organization,
207+
changeRequest: siteURLData.changeRequest,
208+
revision: siteURLData.revision,
209+
shareKey: siteURLData.shareKey,
210+
apiToken: siteURLData.apiToken,
211+
complete: siteURLData.complete,
212+
contextId: siteURLData.contextId,
213+
};
214+
197215
const route = [
198216
'sites',
199217
routeType,
200218
mode,
201219
encodeURIComponent(siteURLWithoutProtocol),
202220
encodeURIComponent(
203-
rison.encode({
204-
...siteURLData,
205-
// The pathname is passed as the next segment of the route and should not cause this segment to change
206-
// based on the page being visited
207-
pathname: '<DO_NOT_USE>',
208-
})
221+
rison.encode(
222+
// rison can't encode undefined values
223+
Object.fromEntries(
224+
Object.entries(stableSiteURLData).filter(
225+
([_, v]) => typeof v !== 'undefined'
226+
)
227+
)
228+
)
209229
),
210230
pathname,
211231
].join('/');

0 commit comments

Comments
 (0)