Skip to content

Commit

Permalink
fix(v2): accessing /docs or /docs/xxxx should not be empty (#1903)
Browse files Browse the repository at this point in the history
* fix(v2): nested routes should have wildcard/ not found page too

* better fix

* nits

* space
  • Loading branch information
endiliey authored and yangshun committed Oct 28, 2019
1 parent b6667a0 commit 7714afb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- Refactor dark toggle into a hook.
- Changed the way we read the `USE_SSH` env variable during deployment to be the same as in v1.
- Add highlight specific lines in code blocks.
- Fix accessing `docs/` or `/docs/xxxx` that does not match any existing doc page should return 404 (Not found) page, not blank page.

## 2.0.0-alpha.31

Expand Down
10 changes: 10 additions & 0 deletions packages/docusaurus-theme-classic/src/theme/DocPage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,26 @@ import renderRoutes from '@docusaurus/renderRoutes';
import Layout from '@theme/Layout';
import DocSidebar from '@theme/DocSidebar';
import MDXComponents from '@theme/MDXComponents';
import NotFound from '@theme/NotFound';
import {matchPath} from '@docusaurus/router';

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

function matchingRouteExist(routes, pathname) {
return routes.some(route => matchPath(pathname, route));
}

function DocPage(props) {
const {route, docsMetadata, location} = props;
const {permalinkToSidebar, docsSidebars} = docsMetadata;
const sidebar = permalinkToSidebar[location.pathname.replace(/\/$/, '')];
const {siteConfig: {themeConfig = {}} = {}} = useDocusaurusContext();
const {sidebarCollapsible = true} = themeConfig;

if (!matchingRouteExist(route.routes, location.pathname)) {
return <NotFound {...props} />;
}

return (
<Layout>
<div className={styles.docPage}>
Expand Down
8 changes: 8 additions & 0 deletions packages/docusaurus/src/client/exports/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Copyright (c) 2017-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

export * from 'react-router-dom';

0 comments on commit 7714afb

Please sign in to comment.