Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(v2): accessing /docs or /docs/xxxx should not be empty #1903

Merged
merged 4 commits into from
Oct 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wonder if there's a way to do this automatically for all plugins. Should it be done automatically?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't. Some theme might want different behavior. E.g: render a table of content of all available docs routes, or maybe display all available versions, etc. Very UI dependent

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 @@
/**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we gonna document this as a user-facing module?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably few of them. I am afraid someone will use BrowserRouter inside pages

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets put it as hidden feature for now. I think matchPath is fine, and maybe react router hooks like useLocations

* 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';