We are using react-router-sitemap for creating sitemap dynamically. We got stuck with adding routes dynamically from our blog. Code returns output correctly, but doesn't add article links to the sitemap, here is the code:
import React, { useEffect } from 'react';
import {Switch, Route} from 'react-router-dom';
import HomePage from './homePage/HomePage';
import Projects from './projects/Projects';
import Project from './projects/MobileProject/project/Project';
import Contact from './contact/Contact';
import Job from './job/Job';
import Blog from './blog/Blog';
const fetch = require("node-fetch");
const API = "http://wp.mysite.in.ua";
function generatePostsRoutes() {
console.log("generating posts ...");
let posts = [];
fetch(API + "/wp-json/wp/v2/posts")
.then((response) => response.json())
.then(content => {
posts = content;
console.log(posts.map((value) => <Route exact path={"/blog/article/" + value.slug} component={Blog} />));
if(posts.length !== 0) {
return (
posts.map((value) => <Route exact path={"/blog/article/" + value.slug} component={Blog} />)
);
}
});
}
export default (
<Switch>
<Route exact path="/" component={HomePage}/>
<Route exact path="/projects" component={Projects}/>
<Route exact path="/project" component={Project}/>
<Route exact path="/blog" component={Blog}/>
<Route exact path="/blog/:sortId" component={Blog}/>
{
generatePostsRoutes()
}
<Route exact path="/job" component={Job}/>
<Route exact path="/contact" component={Contact}/>
</Switch>
);
Here is our sitemap we wanna add blog links after <url> <loc>http://mysite.in.ua/blog/:sortId</loc> </url>
:
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:news="http://www.google.com/schemas/sitemap-news/0.9"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0"
xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"
xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">
<url> <loc>http://mysite.in.ua/</loc> </url>
<url> <loc>http://mysite.in.ua/</loc> </url>
<url> <loc>http://mysite.in.ua/projects</loc> </url>
<url> <loc>http://mysite.in.ua/project</loc> </url>
<url> <loc>http://mysite.in.ua/blog</loc> </url>
<url> <loc>http://mysite.in.ua/blog/:sortId</loc> </url>
<url> <loc>http://mysite.in.ua/job</loc> </url>
<url> <loc>http://mysite.in.ua/contact</loc> </url>
</urlset>
Thank you for your time, I would be so glad if you can help us)
react-router-sitemap
provides a way to generate url for dynamic contents.
On step 6 of the How to generate a sitemap for your React website with dynamic content it show how to add dynamic contents