【问题标题】:react-router-sitemap generating a sitemap.xml with just the URLreact-router-sitemap 仅使用 URL 生成 sitemap.xml
【发布时间】:2022-07-04 17:55:39
【问题描述】:

我正在尝试使用 react-router-sitemap 为我们的站点生成 sitemap.xml。但是,输出文件只列出了 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://localhost:3000/</loc> </url>
</urlset>

我的脚本:

require('babel-register');
const router = "./components/main/contentArea.tsx"

const sitemap =  require('react-router-sitemap').default;

(
    new sitemap(router)
        .build('http://localhost:3000/')
        .save('public/sitemap.xml')
);

我们的路由器看起来像这样(我们使用 react-router-dom,但也使用 react-router 进行了测试,输出仍然相同)

 <Switch>
      <Route exact path="/" component={Dashboard} />
      <Route exact path="/admin/users/users" component={Users} />
</Switch>

我已经尝试了一段时间,但我很困惑为什么这种方法不起作用。任何帮助或想法将不胜感激。谢谢。

【问题讨论】:

    标签: reactjs react-router react-router-dom


    【解决方案1】:

    我找到了 2 篇关于它的文章:

    1. https://blog.josedromero.com/create-sitemap-for-your-react-app/
    2. https://www.amitsn.com/blog/how-to-generate-a-sitemap-for-your-react-website-with-dynamic-content

    第一个实际上描述了一个类似的问题。也许创建一个新的干净 router.ts 文件会有所帮助,您可以尝试删除“组件”。

    此外,在我们的实现中,我们像这样导入站点地图路线:

    const Sitemap = require('react-router-sitemap').default;
    const router = require('./sitemap-routes').default;
    

    sitemap-routes.js 看起来像这样:

    import  React from 'react';
    import { Route, Switch } from "react-router";
    export default ( 
    <Switch>
        <Route path="/en/about-us" />
        <Route path="/en/contact-us" />
    </Switch>);
    

    【讨论】:

      猜你喜欢
      • 2020-10-12
      • 2020-06-03
      • 2020-09-29
      • 2020-09-18
      • 1970-01-01
      • 2011-12-31
      • 1970-01-01
      • 1970-01-01
      • 2018-12-22
      相关资源
      最近更新 更多