最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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>
    )
}
发布评论

评论列表(0)

  1. 暂无评论