From 7fb9004b4348bc44c18c24157fb80da22ce1dfcd Mon Sep 17 00:00:00 2001 From: conico974 Date: Fri, 20 Jun 2025 19:15:21 +0200 Subject: [PATCH] Properly redirect to the root page (#3369) Co-authored-by: Nicolas Dorseuil --- packages/gitbook-v2/src/lib/links.test.ts | 10 ++++++---- packages/gitbook-v2/src/lib/links.ts | 3 ++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/packages/gitbook-v2/src/lib/links.test.ts b/packages/gitbook-v2/src/lib/links.test.ts index f73ec5ee8..30e6f06c7 100644 --- a/packages/gitbook-v2/src/lib/links.test.ts +++ b/packages/gitbook-v2/src/lib/links.test.ts @@ -35,8 +35,9 @@ describe('toPathInSpace', () => { expect(variantInSection.toPathInSpace('some/path/')).toBe('/section/variant/some/path'); }); - it('should not add a trailing slash', () => { - expect(root.toPathInSpace('')).toBe(''); + it('should not add an unnecessary trailing slash', () => { + // The index page should not be an empty path + expect(root.toPathInSpace('')).toBe('/'); expect(variantInSection.toPathInSpace('')).toBe('/section/variant'); }); }); @@ -52,8 +53,9 @@ describe('toPathInSite', () => { expect(siteGitBookIO.toPathInSite('some/path/')).toBe('/sitename/some/path'); }); - it('should not add a trailing slash', () => { - expect(root.toPathInSite('')).toBe(''); + it('should not add an unnecessary trailing slash', () => { + // The index page should not be an empty path + expect(root.toPathInSite('')).toBe('/'); expect(siteGitBookIO.toPathInSite('')).toBe('/sitename'); }); }); diff --git a/packages/gitbook-v2/src/lib/links.ts b/packages/gitbook-v2/src/lib/links.ts index a64bda541..bb2978505 100644 --- a/packages/gitbook-v2/src/lib/links.ts +++ b/packages/gitbook-v2/src/lib/links.ts @@ -128,7 +128,8 @@ export function createLinker( function joinPaths(prefix: string, path: string): string { const prefixPath = prefix.endsWith('/') ? prefix : `${prefix}/`; const suffixPath = path.startsWith('/') ? path.slice(1) : path; - return removeTrailingSlash(prefixPath + suffixPath); + const pathWithoutTrailingSlash = removeTrailingSlash(prefixPath + suffixPath); + return pathWithoutTrailingSlash === '' ? '/' : pathWithoutTrailingSlash; } function removeTrailingSlash(path: string): string {