javascript - 如何在反应服务器呈现的应用程序中访问公共 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();
解决方案
我通过将 xml 添加到我的服务器重写规则来解决。
它是</^((?!\.(css|gif|ico|jpg|js|png|txt|svg|woff|ttf)$).)*$/>
更新为</^((?!\.(css|gif|ico|jpg|js|png|txt|svg|woff|xml|ttf)$).)*$/>
推荐阅读
- c# - 如何在声明之前使用本地对象?
- python - 我试图弄清楚如何添加到字典列表,而不是创建字典列表列表
- c++ - vscode中的C++:错误:没有匹配的构造函数用于初始化'std :: thread'
- dart - 将文本传递给 RaisedButton OnTap - Flutter
- java - 将多行 Json 用户输入格式化为单行 Json 字符串以传递给 Java 控制台应用程序中的 JsonParser
- mongodb - 在 MongoDB 中加入查询
- c++ - 虚幻引擎 4.21.2 和 Squareys CISQLite3 插件编译问题
- python - 在 Tensorboard 上显示 PR 曲线
- python - 如何为股票价格添加新列“Next_day_close”
- excel - 使用单元格值创建命名范围/表格