From e8800b9d494193f5641a9eed9894c40526f74ce5 Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Fri, 25 Mar 2022 16:40:56 +0800 Subject: [PATCH] fix(core): prevent useBaseUrl returning /base/base when on /base (#6993) --- .../src/client/exports/__tests__/useBaseUrl.test.tsx | 2 ++ packages/docusaurus/src/client/exports/useBaseUrl.ts | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/packages/docusaurus/src/client/exports/__tests__/useBaseUrl.test.tsx b/packages/docusaurus/src/client/exports/__tests__/useBaseUrl.test.tsx index 4cca965759fb..5478f7c358f9 100644 --- a/packages/docusaurus/src/client/exports/__tests__/useBaseUrl.test.tsx +++ b/packages/docusaurus/src/client/exports/__tests__/useBaseUrl.test.tsx @@ -77,6 +77,7 @@ describe('useBaseUrl', () => { expect(mockUseBaseUrl('/hello/foo', {absolute: true})).toBe( 'https://docusaurus.io/docusaurus/hello/foo', ); + expect(mockUseBaseUrl('/docusaurus')).toBe('/docusaurus/'); expect(mockUseBaseUrl('/docusaurus/')).toBe('/docusaurus/'); expect(mockUseBaseUrl('/docusaurus/hello')).toBe('/docusaurus/hello'); expect(mockUseBaseUrl('#hello')).toBe('#hello'); @@ -143,6 +144,7 @@ describe('useBaseUrlUtils().withBaseUrl()', () => { expect(withBaseUrl('/hello/foo', {absolute: true})).toBe( 'https://docusaurus.io/docusaurus/hello/foo', ); + expect(withBaseUrl('/docusaurus')).toBe('/docusaurus/'); expect(withBaseUrl('/docusaurus/')).toBe('/docusaurus/'); expect(withBaseUrl('/docusaurus/hello')).toBe('/docusaurus/hello'); expect(withBaseUrl('#hello')).toBe('#hello'); diff --git a/packages/docusaurus/src/client/exports/useBaseUrl.ts b/packages/docusaurus/src/client/exports/useBaseUrl.ts index a709e293809f..5ae4640a150c 100644 --- a/packages/docusaurus/src/client/exports/useBaseUrl.ts +++ b/packages/docusaurus/src/client/exports/useBaseUrl.ts @@ -33,6 +33,12 @@ function addBaseUrl( return baseUrl + url.replace(/^\//, ''); } + // /baseUrl -> /baseUrl/ + // https://github.com/facebook/docusaurus/issues/6315 + if (url === baseUrl.replace(/\/$/, '')) { + return baseUrl; + } + // We should avoid adding the baseurl twice if it's already there const shouldAddBaseUrl = !url.startsWith(baseUrl);