首页 > 解决方案 > 如何在反应服务器呈现的应用程序中访问公共 url 上的 sitemap.xml

问题描述

我正在开发一个使用服务器端渲染的反应应用程序,我想在访问 URL 时查看 sitemap.xml 文件的内容,例如 localhost:3000 /sitemap.xml。现在,我正在借助第三方库生成站点地图文件,并在 web 包完成构建后将其放置在公共文件夹中。

我遇到的问题是我在 localhost:3000/sitemap.xml 找不到该文件。但是,我可以看到由 web-pack 捆绑的其他文件,例如 localhost:3000/main.css 有效。我如何去实现预期的结果。

站点地图生成器

import path from 'path'; //add path which will be needed for file write

const SitemapGenerator = require('sitemap-generator');

// create generator
const generator = SitemapGenerator('https://www.example.com', {
  stripQuerystring: false,
  filepath: path.resolve('./public', 'sitemap.xml')
});

// start the crawler
generator.start();

标签: javascriptreactjs

解决方案


我通过将 xml 添加到我的服务器重写规则来解决。

它是</^((?!\.(css|gif|ico|jpg|js|png|txt|svg|woff|ttf)$).)*$/>

更新为</^((?!\.(css|gif|ico|jpg|js|png|txt|svg|woff|xml|ttf)$).)*$/>


推荐阅读