react-router-sitemap 不会为列表中的动态内容生成 url

react-router-sitemap doesn't generate urls for dynamic content from list

我们正在使用 react-router-sitemap 动态创建站点地图。我们无法从我们的博客中动态添加路由。代码 returns 输出正确,但没有将文章链接添加到站点地图,代码如下:

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>
);

这是我们的站点地图,我们想在 <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>

感谢您抽出宝贵时间,如果您能帮助我们,我将非常高兴)

react-router-sitemap 提供了一种为动态内容生成 url 的方法。 How to generate a sitemap for your React website with dynamic content 的第 6 步展示了如何添加动态内容