javascript - How to integrate dynamic routes in Docusaurus with react-router - Stack Overflow

I have a website made with Docusaurus v2 that currently contains documentation. However, I would like t

I have a website made with Docusaurus v2 that currently contains documentation. However, I would like to add a page of a list of workflows where if a workflow in the list is clicked, the user would be shown a page of additional details of that workflow. For now it seems docusaurus.config seems to be handling most of the routing, but is there a way I can add a dynamic route like /workflows/:id? I made a separate standalone app which had a Router object and it worked if my App.js looks like this:

// App.js
import Navigation from './Navigation'
import {BrowserRouter as Router, Switch, Route} from 'react-router-dom';

function App() {
  return (
  <Router>
    <Navigation />
    <Switch>
      <Route path="/" exact ponent={Home}></Route>
      <Route path="/workflows" exact ponent={Workflows}></Route>
      <Route path="/workflows/:id" ponent={WorkflowItem}></Route>
    </Switch>
  </Router>
  )
}

Is it possible to add the Router somewhere in Docusaurus? Thanks!

I have a website made with Docusaurus v2 that currently contains documentation. However, I would like to add a page of a list of workflows where if a workflow in the list is clicked, the user would be shown a page of additional details of that workflow. For now it seems docusaurus.config seems to be handling most of the routing, but is there a way I can add a dynamic route like /workflows/:id? I made a separate standalone app which had a Router object and it worked if my App.js looks like this:

// App.js
import Navigation from './Navigation'
import {BrowserRouter as Router, Switch, Route} from 'react-router-dom';

function App() {
  return (
  <Router>
    <Navigation />
    <Switch>
      <Route path="/" exact ponent={Home}></Route>
      <Route path="/workflows" exact ponent={Workflows}></Route>
      <Route path="/workflows/:id" ponent={WorkflowItem}></Route>
    </Switch>
  </Router>
  )
}

Is it possible to add the Router somewhere in Docusaurus? Thanks!

Share Improve this question asked Aug 5, 2020 at 18:52 kaytankaytan 511 silver badge2 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

I solved this by creating a simple plugin to add my own custom routes. Documentation here.

Let's call the plugin plugin-dynamic-routes.

// {SITE_ROOT_DIR}/plugin-dynamic-routes/index.js

module.exports = function (context, options) {
    return {
        name: 'plugin-dynamic-routes',

        async contentLoaded({ content, actions }) {
            const { routes } = options
            const { addRoute } = actions

            routes.map(route => addRoute(route))
        }
    }
}
// docusaurus.config.js
const path = require('path')

module.exports = {
    // ...
    plugins: [
        [
            path.resolve(__dirname, 'plugin-dynamic-routes'),
            { // this is the options object passed to the plugin
                routes: [
                    { // using Route schema from react-router
                        path: '/workflows',
                        exact: false, // this is needed for sub-routes to match!
                        ponent: '@site/path/to/ponent/App'
                    }
                ]
            }
        ],
    ],
}

You may be able to use the above method to configure sub-routes as well but I haven't tried it. For the custom page, all you need is the Switch ponent (you are technically using nested routes at this point). The Layout ponent is there to integrate the page into the rest of the Docusaurus site.

// App.js

import React from 'react'
import Layout from '@theme/Layout'
import { Switch, Route, useRouteMatch } from '@docusaurus/router'

function App() {
    let match = useRouteMatch()

    return (
        <Layout title="Page Title">
            <Switch>
                <Route path={`${match.path}/:id`} ponent={WorkflowItem} />
                <Route path={match.path} ponent={Workflows} />
            </Switch>
        </Layout>
    )
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745295827a4621135.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信