admin管理员组

文章数量:1287856

I am using react-router-dom for routing in my react app, There are nearly 5000 pages (which are generated dynamically based on API), now I don't know how can I generate a sitemap for these dynamic pages

Piece of code from my Routes ponent

<Route
  path={process.env.PUBLIC_URL + '/genres/:name'}
  exact
  ponent={MovieGenre}
/>
<Route
  path={process.env.PUBLIC_URL + '/discover/:name'}
  exact
  ponent={DiscoverMovies}
/>
<Route
  path={process.env.PUBLIC_URL + '/find/:query'}
  exact
  ponent={SearchPage}
/>
<Route
  path={process.env.PUBLIC_URL + '/moviesdetail/:id'}
  exact
  ponent={MovieScrees}
/>  

I am using react-router-dom for routing in my react app, There are nearly 5000 pages (which are generated dynamically based on API), now I don't know how can I generate a sitemap for these dynamic pages

Piece of code from my Routes ponent

<Route
  path={process.env.PUBLIC_URL + '/genres/:name'}
  exact
  ponent={MovieGenre}
/>
<Route
  path={process.env.PUBLIC_URL + '/discover/:name'}
  exact
  ponent={DiscoverMovies}
/>
<Route
  path={process.env.PUBLIC_URL + '/find/:query'}
  exact
  ponent={SearchPage}
/>
<Route
  path={process.env.PUBLIC_URL + '/moviesdetail/:id'}
  exact
  ponent={MovieScrees}
/>  
Share Improve this question edited Jun 24, 2021 at 5:10 thelovekesh 1,4521 gold badge9 silver badges22 bronze badges asked Jun 24, 2021 at 5:06 Suhail KakarSuhail Kakar 3261 gold badge4 silver badges17 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 6

You can use react-router-sitemap to generate a sitemap.xml file for your React application.

Package:

npm i --save react-router-sitemap

Package link - https://www.npmjs./package/react-router-sitemap

If you want to implement this in your app then grab all code from here

When we say dynamic routing, we mean routing that takes place as your app is rendering, not in a configuration or convention outside of a running app. that means almost everything is a ponent in react-router.

  • First, grab yourself a Router ponent for the environment you’re targeting and render it at the top of your app.
  • Next, grab the link ponent to link to a new location:
  • Finally, render a Route to show some UI when the user visits /dashboard.

   // react-dom (what we'll use here)
import { BrowserRouter } from "react-router-dom";

ReactDOM.render(
  <BrowserRouter>
    <App />
  </BrowserRouter>,
  el
);
Next, grab the link ponent to link to a new location:const App = () => (
  <div>
    <nav>
      <Link to="/dashboard">Dashboard</Link>
    </nav>
  </div>
);
Finally, render a Route to show some UI when the user visits /dashboard.const App = () => (
  <div>
    <nav>
      <Link to="/dashboard">Dashboard</Link>
    </nav>
    <div>
      <Route path="/dashboard" ponent={Dashboard} />
    </div>
  </div>
);

**In this blog author explained in an easy waylink **

Solution 2

There are some npm packages which will handle sitemap

  • https://www.npmjs./package/react-router-sitemap
  • https://www.npmjs./package/react-router-sitemap-generator
  • How to generate sitemap with react router

本文标签: javascriptHow to generate a sitemap dynamically in React JSStack Overflow