Skip to content

Commit d370a3f

Browse files
authored
Align the revisions/changes path for multi-id with the normal one (#2769)
1 parent d3e573c commit d370a3f

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

.changeset/olive-pillows-kick.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'gitbook': patch
3+
---
4+
5+
Update the routes for changes/revisions in multi-id mode to match the normal mode

packages/gitbook/src/middleware.ts

+12-8
Original file line numberDiff line numberDiff line change
@@ -482,8 +482,8 @@ async function lookupSiteInMultiMode(request: NextRequest, url: URL): Promise<Lo
482482
*
483483
* The format of the path is:
484484
* - /~space|~site/:id/:path
485-
* - /~space|~site/:id/~changes/:changeId/:path
486-
* - /~space|~site/:id/~revisions/:revisionId/:path
485+
* - /~space|~site/:id/~/changes/:changeId/:path
486+
* - /~space|~site/:id/~/revisions/:revisionId/:path
487487
*/
488488
async function lookupSiteOrSpaceInMultiIdMode(
489489
request: NextRequest,
@@ -492,13 +492,17 @@ async function lookupSiteOrSpaceInMultiIdMode(
492492
const basePathParts: string[] = [];
493493
const pathSegments = url.pathname.slice(1).split('/');
494494

495-
const eatPathId = (prefix: string): string | undefined => {
496-
if (pathSegments[0] !== prefix || pathSegments.length < 2) {
495+
const eatPathId = (...prefixes: string[]): string | undefined => {
496+
const match = prefixes.every((prefix, index) => pathSegments[index] === prefix);
497+
if (!match || pathSegments.length < prefixes.length + 1) {
497498
return;
498499
}
499500

500-
const prefixSegment = pathSegments.shift();
501-
basePathParts.push(prefixSegment!);
501+
// Remove the prefix from the path segments
502+
pathSegments.splice(0, prefixes.length);
503+
504+
// Add the prefix to the base path
505+
basePathParts.push(...prefixes);
502506

503507
const id = pathSegments.shift();
504508
basePathParts.push(id!);
@@ -524,8 +528,8 @@ async function lookupSiteOrSpaceInMultiIdMode(
524528
}
525529

526530
// Extract the change request or revision ID from the path
527-
const changeRequestId = eatPathId('~changes');
528-
const revisionId = eatPathId('~revisions');
531+
const changeRequestId = eatPathId('~', 'changes');
532+
const revisionId = eatPathId('~', 'revisions');
529533

530534
// Get the auth token from the URL query
531535
const AUTH_TOKEN_QUERY = 'token';

0 commit comments

Comments
 (0)