@pietro
To generate a sitemap for pages generated dynamically in React.js, you can follow these steps:
1 2 3 4 5 6 |
const SitemapGenerator = require('react-router-sitemap').default; const router = require('./src/routes'); new SitemapGenerator('https://example.com', router) .build() .save('./public/sitemap.xml'); |
This code imports the react-router-sitemap
package, defines the base URL for your site, and specifies the routes to be included in the sitemap.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
const routes = [ { path: '/', exact: true, }, { path: '/about', exact: true, }, // add more routes as needed ]; module.exports = routes; |
1
|
node sitemap.js |
This should generate a sitemap at ./public/sitemap.xml
with all the routes specified in src/routes.js
.
Note that this method assumes that your React app is server-side rendered (SSR) so that search engines can crawl the generated pages. If your app is client-side rendered (CSR), you may need to use a different approach to generate a sitemap, such as using a headless browser to crawl your app and extract the URLs.
@pietro
To generate a sitemap for dynamically generated pages in React.js, you can follow these steps:
1
|
npm install react-router-sitemap |
1 2 3 4 5 6 7 8 9 10 11 12 |
import HomePage from './components/HomePage'; import AboutPage from './components/AboutPage'; import ContactPage from './components/ContactPage'; const routes = [ { path: "/", exact: true, component: HomePage }, { path: "/about", exact: true, component: AboutPage }, { path: "/contact", exact: true, component: ContactPage }, // Add more routes here ]; export default routes; |
1 2 3 4 5 6 7 8 9 10 |
const Sitemap = require("react-router-sitemap").default; const routes = require("./src/routes"); function generateSitemap() { return new Sitemap(routes) .build("https://example.com") .save("./public/sitemap.xml"); } generateSitemap(); |
1
|
node sitemapGenerator.js |
This will generate a sitemap.xml file in the public folder of your project, containing all the dynamically generated URLs specified in the routes
array.
Remember to replace "https://example.com" in the generateSitemap
function with the actual base URL of your website.
Note: This method assumes that your React app is server-side rendered (SSR) or that you have a server in place to handle the routing and rendering of the pages. If your app is client-side rendered (CSR), you may need to use a different approach, such as using a headless browser to crawl your app and extract the URLs for the sitemap.