From eac5fe70a502c2d35ed6958f1345a203c8fd5190 Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Sun, 17 Apr 2022 09:29:59 +0800 Subject: [PATCH 01/12] fix(core): normalize /path.html to /path to prevent 404 --- .../docusaurus/src/client/__tests__/normalizeLocation.test.ts | 2 +- packages/docusaurus/src/client/normalizeLocation.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/docusaurus/src/client/__tests__/normalizeLocation.test.ts b/packages/docusaurus/src/client/__tests__/normalizeLocation.test.ts index 9517a5b0f15d..b3babf7bc075 100644 --- a/packages/docusaurus/src/client/__tests__/normalizeLocation.test.ts +++ b/packages/docusaurus/src/client/__tests__/normalizeLocation.test.ts @@ -79,7 +79,7 @@ describe('normalizeLocation', () => { hash: '#bar', }), ).toEqual({ - pathname: '/docs/introduction/foo.html', + pathname: '/docs/introduction/foo', search: '', hash: '#bar', }); diff --git a/packages/docusaurus/src/client/normalizeLocation.ts b/packages/docusaurus/src/client/normalizeLocation.ts index 94a74da70044..41a19b5816e6 100644 --- a/packages/docusaurus/src/client/normalizeLocation.ts +++ b/packages/docusaurus/src/client/normalizeLocation.ts @@ -19,7 +19,7 @@ export default function normalizeLocation(location: T): T { } const pathname = - location.pathname.trim().replace(/\/index\.html$/, '') || '/'; + location.pathname.trim().replace(/(?:\/index)?\.html$/, '') || '/'; pathnames[location.pathname] = pathname; From 76c2f2cc88f288f4a5f1c12d44ab0a2d6e4e01b4 Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Sun, 17 Apr 2022 10:05:59 +0800 Subject: [PATCH 02/12] test no trailing slash --- website/docusaurus.config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docusaurus.config.js b/website/docusaurus.config.js index 8e396e641b41..5a87cf4d1898 100644 --- a/website/docusaurus.config.js +++ b/website/docusaurus.config.js @@ -73,7 +73,7 @@ const config = { // Dogfood both settings: // - force trailing slashes for deploy previews // - avoid trailing slashes in prod - trailingSlash: isDeployPreview, + trailingSlash: false, stylesheets: [ { href: '/katex/katex.min.css', From 49a9fe2f914f1e8fbc87940c8f099fb70b27ecb7 Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Thu, 21 Apr 2022 17:36:05 +0800 Subject: [PATCH 03/12] dogfood? --- packages/docusaurus/src/client/normalizeLocation.ts | 7 +++++++ website/_dogfooding/_docs tests/dummy.md | 4 ++++ 2 files changed, 11 insertions(+) diff --git a/packages/docusaurus/src/client/normalizeLocation.ts b/packages/docusaurus/src/client/normalizeLocation.ts index 41a19b5816e6..0a609524555d 100644 --- a/packages/docusaurus/src/client/normalizeLocation.ts +++ b/packages/docusaurus/src/client/normalizeLocation.ts @@ -5,6 +5,8 @@ * LICENSE file in the root directory of this source tree. */ +import routes from '@generated/routes'; +import {matchRoutes} from 'react-router-config'; import type {Location} from 'history'; // Memoize previously normalized pathnames. @@ -18,6 +20,11 @@ export default function normalizeLocation(location: T): T { }; } + if (matchRoutes(routes, location.pathname)) { + pathnames[location.pathname] = location.pathname; + return location; + } + const pathname = location.pathname.trim().replace(/(?:\/index)?\.html$/, '') || '/'; diff --git a/website/_dogfooding/_docs tests/dummy.md b/website/_dogfooding/_docs tests/dummy.md index ea913c996231..8ec7fc90fbdb 100644 --- a/website/_dogfooding/_docs tests/dummy.md +++ b/website/_dogfooding/_docs tests/dummy.md @@ -1 +1,5 @@ +--- +slug: dummy.html +--- + # Just a dummy page From 5134e87da8731d835943b8010650f367f05571db Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Fri, 22 Apr 2022 17:59:48 +0800 Subject: [PATCH 04/12] revert last change --- packages/docusaurus/src/client/normalizeLocation.ts | 7 ------- 1 file changed, 7 deletions(-) diff --git a/packages/docusaurus/src/client/normalizeLocation.ts b/packages/docusaurus/src/client/normalizeLocation.ts index 0a609524555d..41a19b5816e6 100644 --- a/packages/docusaurus/src/client/normalizeLocation.ts +++ b/packages/docusaurus/src/client/normalizeLocation.ts @@ -5,8 +5,6 @@ * LICENSE file in the root directory of this source tree. */ -import routes from '@generated/routes'; -import {matchRoutes} from 'react-router-config'; import type {Location} from 'history'; // Memoize previously normalized pathnames. @@ -20,11 +18,6 @@ export default function normalizeLocation(location: T): T { }; } - if (matchRoutes(routes, location.pathname)) { - pathnames[location.pathname] = location.pathname; - return location; - } - const pathname = location.pathname.trim().replace(/(?:\/index)?\.html$/, '') || '/'; From 77513e1207d58f986843e3b4725665cf45f0fa41 Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Fri, 22 Apr 2022 18:21:16 +0800 Subject: [PATCH 05/12] does this fix --- .../src/client/__tests__/__mocks__/@generated/routes.ts | 8 ++++++++ packages/docusaurus/src/client/normalizeLocation.ts | 9 +++++++++ 2 files changed, 17 insertions(+) create mode 100644 packages/docusaurus/src/client/__tests__/__mocks__/@generated/routes.ts diff --git a/packages/docusaurus/src/client/__tests__/__mocks__/@generated/routes.ts b/packages/docusaurus/src/client/__tests__/__mocks__/@generated/routes.ts new file mode 100644 index 000000000000..03927b998d4c --- /dev/null +++ b/packages/docusaurus/src/client/__tests__/__mocks__/@generated/routes.ts @@ -0,0 +1,8 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +export default []; diff --git a/packages/docusaurus/src/client/normalizeLocation.ts b/packages/docusaurus/src/client/normalizeLocation.ts index 41a19b5816e6..6daef3bd7a17 100644 --- a/packages/docusaurus/src/client/normalizeLocation.ts +++ b/packages/docusaurus/src/client/normalizeLocation.ts @@ -5,6 +5,8 @@ * LICENSE file in the root directory of this source tree. */ +import {matchRoutes} from 'react-router-config'; +import routes from '@generated/routes'; import type {Location} from 'history'; // Memoize previously normalized pathnames. @@ -18,6 +20,13 @@ export default function normalizeLocation(location: T): T { }; } + // If the location was registered with an `.html` extension, we don't strip it + // away, or it will render to a 404 page. + if (matchRoutes(routes, location.pathname).length > 0) { + pathnames[location.pathname] = location.pathname; + return location; + } + const pathname = location.pathname.trim().replace(/(?:\/index)?\.html$/, '') || '/'; From 0cf606a4d6c8e53328c1b1d57c901fa5e35fb610 Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Fri, 22 Apr 2022 18:26:40 +0800 Subject: [PATCH 06/12] ignore --- jest.config.mjs | 1 + 1 file changed, 1 insertion(+) diff --git a/jest.config.mjs b/jest.config.mjs index 5c241b301ed8..2bbb403b68be 100644 --- a/jest.config.mjs +++ b/jest.config.mjs @@ -12,6 +12,7 @@ process.env.TZ = 'UTC'; const ignorePatterns = [ '/node_modules/', '__fixtures__', + '__mocks__', '/testUtils.ts', '/packages/docusaurus/lib', '/packages/docusaurus-logger/lib', From 63447e0aea34df79085010da9ec06bd3b1ad6fbe Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Fri, 22 Apr 2022 18:32:01 +0800 Subject: [PATCH 07/12] add test --- .../__tests__/__mocks__/@generated/routes.ts | 7 +++- .../__tests__/normalizeLocation.test.ts | 32 ++++++++++++------- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/packages/docusaurus/src/client/__tests__/__mocks__/@generated/routes.ts b/packages/docusaurus/src/client/__tests__/__mocks__/@generated/routes.ts index 03927b998d4c..7e41aba02378 100644 --- a/packages/docusaurus/src/client/__tests__/__mocks__/@generated/routes.ts +++ b/packages/docusaurus/src/client/__tests__/__mocks__/@generated/routes.ts @@ -5,4 +5,9 @@ * LICENSE file in the root directory of this source tree. */ -export default []; +export default [ + { + path: '/page.html', + component: '', + }, +]; diff --git a/packages/docusaurus/src/client/__tests__/normalizeLocation.test.ts b/packages/docusaurus/src/client/__tests__/normalizeLocation.test.ts index b3babf7bc075..aa92bcd543a8 100644 --- a/packages/docusaurus/src/client/__tests__/normalizeLocation.test.ts +++ b/packages/docusaurus/src/client/__tests__/normalizeLocation.test.ts @@ -43,6 +43,26 @@ describe('normalizeLocation', () => { }); }); + it('removes html extension', () => { + expect( + normalizeLocation({ + pathname: '/docs/introduction/foo.html', + search: '', + hash: '#bar', + }), + ).toEqual({ + pathname: '/docs/introduction/foo', + search: '', + hash: '#bar', + }); + }); + + it('does not strip extension if the route location has one', () => { + expect(normalizeLocation({pathname: '/page.html'})).toEqual({ + pathname: '/page.html', + }); + }); + it('leaves pathnames untouched', () => { const replaceMock = jest.spyOn(String.prototype, 'replace'); @@ -72,18 +92,6 @@ describe('normalizeLocation', () => { }); expect(replaceMock).toBeCalledTimes(1); - expect( - normalizeLocation({ - pathname: '/docs/introduction/foo.html', - search: '', - hash: '#bar', - }), - ).toEqual({ - pathname: '/docs/introduction/foo', - search: '', - hash: '#bar', - }); - expect( normalizeLocation({ pathname: '/', From 67fa9694d476ddee8b42f599540f571528c7f2e1 Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Fri, 22 Apr 2022 18:43:09 +0800 Subject: [PATCH 08/12] fix --- .../src/client/__tests__/__mocks__/@generated/routes.ts | 4 ++++ packages/docusaurus/src/client/normalizeLocation.ts | 3 ++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/docusaurus/src/client/__tests__/__mocks__/@generated/routes.ts b/packages/docusaurus/src/client/__tests__/__mocks__/@generated/routes.ts index 7e41aba02378..ede01ba10a9c 100644 --- a/packages/docusaurus/src/client/__tests__/__mocks__/@generated/routes.ts +++ b/packages/docusaurus/src/client/__tests__/__mocks__/@generated/routes.ts @@ -10,4 +10,8 @@ export default [ path: '/page.html', component: '', }, + { + path: '*', + component: '', + }, ]; diff --git a/packages/docusaurus/src/client/normalizeLocation.ts b/packages/docusaurus/src/client/normalizeLocation.ts index 6daef3bd7a17..68cafaa17afd 100644 --- a/packages/docusaurus/src/client/normalizeLocation.ts +++ b/packages/docusaurus/src/client/normalizeLocation.ts @@ -22,7 +22,8 @@ export default function normalizeLocation(location: T): T { // If the location was registered with an `.html` extension, we don't strip it // away, or it will render to a 404 page. - if (matchRoutes(routes, location.pathname).length > 0) { + const matchedRoutes = matchRoutes(routes, location.pathname); + if (matchedRoutes.length > 0 && matchedRoutes[0]!.match.path !== '*') { pathnames[location.pathname] = location.pathname; return location; } From 91e0fdbe67c9e56cdaeb59ed02fc82ba3dd0d336 Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Fri, 22 Apr 2022 18:58:27 +0800 Subject: [PATCH 09/12] properly fix --- .../client/__tests__/__mocks__/@generated/routes.ts | 13 +++++++++++++ .../src/client/__tests__/normalizeLocation.test.ts | 7 +++++++ packages/docusaurus/src/client/normalizeLocation.ts | 2 +- 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/packages/docusaurus/src/client/__tests__/__mocks__/@generated/routes.ts b/packages/docusaurus/src/client/__tests__/__mocks__/@generated/routes.ts index ede01ba10a9c..ce2206070e1d 100644 --- a/packages/docusaurus/src/client/__tests__/__mocks__/@generated/routes.ts +++ b/packages/docusaurus/src/client/__tests__/__mocks__/@generated/routes.ts @@ -8,8 +8,21 @@ export default [ { path: '/page.html', + exact: true, component: '', }, + { + path: '/docs', + exact: false, + component: '', + routes: [ + { + path: '/docs/installation', + exact: true, + component: '', + }, + ], + }, { path: '*', component: '', diff --git a/packages/docusaurus/src/client/__tests__/normalizeLocation.test.ts b/packages/docusaurus/src/client/__tests__/normalizeLocation.test.ts index aa92bcd543a8..fad94198575c 100644 --- a/packages/docusaurus/src/client/__tests__/normalizeLocation.test.ts +++ b/packages/docusaurus/src/client/__tests__/normalizeLocation.test.ts @@ -44,6 +44,13 @@ describe('normalizeLocation', () => { }); it('removes html extension', () => { + expect( + normalizeLocation({ + pathname: '/docs/installation.html', + }), + ).toEqual({ + pathname: '/docs/installation', + }); expect( normalizeLocation({ pathname: '/docs/introduction/foo.html', diff --git a/packages/docusaurus/src/client/normalizeLocation.ts b/packages/docusaurus/src/client/normalizeLocation.ts index 68cafaa17afd..2d4010e95112 100644 --- a/packages/docusaurus/src/client/normalizeLocation.ts +++ b/packages/docusaurus/src/client/normalizeLocation.ts @@ -23,7 +23,7 @@ export default function normalizeLocation(location: T): T { // If the location was registered with an `.html` extension, we don't strip it // away, or it will render to a 404 page. const matchedRoutes = matchRoutes(routes, location.pathname); - if (matchedRoutes.length > 0 && matchedRoutes[0]!.match.path !== '*') { + if (matchedRoutes.length > 0 && matchedRoutes[0]!.route.exact === true) { pathnames[location.pathname] = location.pathname; return location; } From d4c982e20e2435588d08e255888c64bb056f8679 Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Fri, 22 Apr 2022 19:16:59 +0800 Subject: [PATCH 10/12] fix again --- packages/docusaurus/src/client/normalizeLocation.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/docusaurus/src/client/normalizeLocation.ts b/packages/docusaurus/src/client/normalizeLocation.ts index 2d4010e95112..37c55b4000f0 100644 --- a/packages/docusaurus/src/client/normalizeLocation.ts +++ b/packages/docusaurus/src/client/normalizeLocation.ts @@ -23,7 +23,7 @@ export default function normalizeLocation(location: T): T { // If the location was registered with an `.html` extension, we don't strip it // away, or it will render to a 404 page. const matchedRoutes = matchRoutes(routes, location.pathname); - if (matchedRoutes.length > 0 && matchedRoutes[0]!.route.exact === true) { + if (matchedRoutes.some(({route}) => route.exact === true)) { pathnames[location.pathname] = location.pathname; return location; } From 12eeedb33b03c0c7734184a561aab02f034419ba Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Fri, 22 Apr 2022 21:30:22 +0800 Subject: [PATCH 11/12] revert trailing slash change --- website/docusaurus.config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docusaurus.config.js b/website/docusaurus.config.js index 34d0f430eb4d..df57e09ac88c 100644 --- a/website/docusaurus.config.js +++ b/website/docusaurus.config.js @@ -73,7 +73,7 @@ const config = { // Dogfood both settings: // - force trailing slashes for deploy previews // - avoid trailing slashes in prod - trailingSlash: false, + trailingSlash: isDeployPreview, stylesheets: [ { href: '/katex/katex.min.css', From 2b82e533dbaf9e6048351aa29bcef64063c1e50d Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Fri, 22 Apr 2022 21:43:53 +0800 Subject: [PATCH 12/12] optimize: use map instead of object --- packages/docusaurus/src/client/normalizeLocation.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/docusaurus/src/client/normalizeLocation.ts b/packages/docusaurus/src/client/normalizeLocation.ts index 37c55b4000f0..74192a451c08 100644 --- a/packages/docusaurus/src/client/normalizeLocation.ts +++ b/packages/docusaurus/src/client/normalizeLocation.ts @@ -10,13 +10,13 @@ import routes from '@generated/routes'; import type {Location} from 'history'; // Memoize previously normalized pathnames. -const pathnames: {[rawPathname: string]: string} = {}; +const pathnames = new Map(); export default function normalizeLocation(location: T): T { - if (pathnames[location.pathname]) { + if (pathnames.has(location.pathname)) { return { ...location, - pathname: pathnames[location.pathname], + pathname: pathnames.get(location.pathname), }; } @@ -24,14 +24,14 @@ export default function normalizeLocation(location: T): T { // away, or it will render to a 404 page. const matchedRoutes = matchRoutes(routes, location.pathname); if (matchedRoutes.some(({route}) => route.exact === true)) { - pathnames[location.pathname] = location.pathname; + pathnames.set(location.pathname, location.pathname); return location; } const pathname = location.pathname.trim().replace(/(?:\/index)?\.html$/, '') || '/'; - pathnames[location.pathname] = pathname; + pathnames.set(location.pathname, pathname); return { ...location,