-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(theme-classic): fix breadcrumb home link bug with new useHomePage…
…Route() hook (#6749)
- Loading branch information
Showing
6 changed files
with
123 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
packages/docusaurus-theme-common/src/utils/__tests__/routesUtils.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/** | ||
* 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. | ||
*/ | ||
|
||
import {type Route} from '@generated/routes'; | ||
import {findHomePageRoute} from '../routesUtils'; | ||
|
||
describe('routesUtils findHomePageRoute', () => { | ||
const homePage: Route = { | ||
path: '/', | ||
exact: true, | ||
}; | ||
|
||
test('should return undefined for no routes', () => { | ||
expect(findHomePageRoute([])).toEqual(undefined); | ||
}); | ||
|
||
test('should return undefined for no homepage', () => { | ||
expect( | ||
findHomePageRoute([ | ||
{path: '/a', exact: true}, | ||
{path: '/b', exact: false}, | ||
{path: '/c', exact: undefined}, | ||
{ | ||
path: '/d', | ||
exact: false, | ||
routes: [ | ||
{path: '/d/1', exact: true}, | ||
{path: '/d/2', exact: false}, | ||
{path: '/d/3', exact: undefined}, | ||
], | ||
}, | ||
]), | ||
).toEqual(undefined); | ||
}); | ||
|
||
test('should find top-level homepage', () => { | ||
expect( | ||
findHomePageRoute([ | ||
{path: '/a', exact: true}, | ||
{path: '/b', exact: false}, | ||
{path: '/c', exact: undefined}, | ||
{...homePage, exact: false}, | ||
homePage, | ||
{...homePage, exact: undefined}, | ||
]), | ||
).toEqual(homePage); | ||
}); | ||
|
||
test('should find nested homepage', () => { | ||
expect( | ||
findHomePageRoute([ | ||
{path: '/a', exact: true}, | ||
{ | ||
path: '/', | ||
exact: false, | ||
routes: [ | ||
{path: '/b', exact: true}, | ||
{ | ||
path: '/', | ||
exact: false, | ||
routes: [{path: '/c', exact: true}, homePage], | ||
}, | ||
], | ||
}, | ||
{path: '/d', exact: true}, | ||
]), | ||
).toEqual(homePage); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* 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. | ||
*/ | ||
|
||
import GeneratedRoutes, {type Route} from '@generated/routes'; | ||
import {useMemo} from 'react'; | ||
|
||
function isHomePageRoute(route: Route): boolean { | ||
return route.path === '/' && route.exact === true; | ||
} | ||
|
||
function isHomeParentRoute(route: Route): boolean { | ||
return route.path === '/' && route.exact === false; | ||
} | ||
|
||
// Note that all sites don't always have a homepage in practice | ||
// See https://github.com/facebook/docusaurus/pull/6517#issuecomment-1048709116 | ||
export function findHomePageRoute( | ||
routes: Route[] = GeneratedRoutes, | ||
): Route | undefined { | ||
if (routes.length === 0) { | ||
return undefined; | ||
} | ||
const homePage = routes.find(isHomePageRoute); | ||
if (homePage) { | ||
return homePage; | ||
} | ||
const indexSubRoutes = routes | ||
.filter(isHomeParentRoute) | ||
.flatMap((route) => route.routes ?? []); | ||
return findHomePageRoute(indexSubRoutes); | ||
} | ||
|
||
export function useHomePageRoute(): Route | undefined { | ||
return useMemo(() => findHomePageRoute(), []); | ||
} |