Lighthouse读取我的网站主页而非robots.txt文件

5

我想在我的React应用程序上使用Chrome审核工具,但它一直说我的robots.txt文件无效。问题是,文件似乎完全正常,只是它不读取robots.txt而是读取我的index.html文件,因此导致出现此错误:

enter image description here

这两个文件都在我的 /public 文件夹中,为什么会读取 index 文件?


你是否正在使用服务器端渲染你的应用? - Lazar Nikolic
@LazarNikolic 是的 - Pierre Olivier Tran
1个回答

1
如果您使用Node和Express来在生产环境中运行React应用程序项目,并且您的服务器文件代码如下片段所示,那么这意味着您为所有请求(路由)提供相同的文件。在Lighthouse审核期间,浏览器会向http://localhost:5000/robots.txt发出GET请求,然后返回index.html。
 app.get('/*', function (req, res) {
   res.sendFile(path.join(__dirname, 'build', 'index.html'));
 });

为解决这个问题,您可以在上述代码片段之前添加如下路由以处理robots.txt文件。
 app.get('/robots.txt', function (req, res) {
  res.sendFile(path.join(__dirname, 'build', 'robots.txt'));
});

app.get('/*', function (req, res) {
   res.sendFile(path.join(__dirname, 'build', 'index.html'));
 });

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接