Skip to content

Commit 7714afb

Browse files
endilieyyangshun
authored andcommitted
fix(v2): accessing /docs or /docs/xxxx should not be empty (#1903)
* fix(v2): nested routes should have wildcard/ not found page too * better fix * nits * space
1 parent b6667a0 commit 7714afb

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

CHANGELOG-2.x.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- Refactor dark toggle into a hook.
1010
- Changed the way we read the `USE_SSH` env variable during deployment to be the same as in v1.
1111
- Add highlight specific lines in code blocks.
12+
- Fix accessing `docs/` or `/docs/xxxx` that does not match any existing doc page should return 404 (Not found) page, not blank page.
1213

1314
## 2.0.0-alpha.31
1415

packages/docusaurus-theme-classic/src/theme/DocPage/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,26 @@ import renderRoutes from '@docusaurus/renderRoutes';
1313
import Layout from '@theme/Layout';
1414
import DocSidebar from '@theme/DocSidebar';
1515
import MDXComponents from '@theme/MDXComponents';
16+
import NotFound from '@theme/NotFound';
17+
import {matchPath} from '@docusaurus/router';
1618

1719
import styles from './styles.module.css';
1820

21+
function matchingRouteExist(routes, pathname) {
22+
return routes.some(route => matchPath(pathname, route));
23+
}
24+
1925
function DocPage(props) {
2026
const {route, docsMetadata, location} = props;
2127
const {permalinkToSidebar, docsSidebars} = docsMetadata;
2228
const sidebar = permalinkToSidebar[location.pathname.replace(/\/$/, '')];
2329
const {siteConfig: {themeConfig = {}} = {}} = useDocusaurusContext();
2430
const {sidebarCollapsible = true} = themeConfig;
2531

32+
if (!matchingRouteExist(route.routes, location.pathname)) {
33+
return <NotFound {...props} />;
34+
}
35+
2636
return (
2737
<Layout>
2838
<div className={styles.docPage}>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Copyright (c) 2017-present, Facebook, Inc.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
export * from 'react-router-dom';

0 commit comments

Comments
 (0)