reactjs - Lighthouse 读取我的索引而不是 robots.txt

标签 reactjs robots.txt

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

enter image description here

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

最佳答案

如果您使用 节点和 express 为了运行您的 react 应用 项目在生产。
如果您的 服务器文件代码 看起来像下面的片段,这意味着你是 提供相同的文件 所有 请求 (路线)。灯塔审核期间,浏览器 发出获取请求http://localhost:5000/robots.txt 作为返回,它会得到 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'));
 });

关于reactjs - Lighthouse 读取我的索引而不是 robots.txt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52219815/

相关文章:

reactjs - 即使值相同,为什么双花括号子组件会导致我的 React.memo 组件重新渲染?

javascript - 从 2 个数组中的 1 个构建的 React .map

javascript - React 中的 JSON 解析嵌套对象

javascript - 在 ReactJS 中隐藏条件文本

php - 如何制作私有(private)网址?

robots.txt - 为什么在javascript文件上使用robot.txt?

javascript - 如何使用短路评估在内联条件后渲染多个元素?

web - 无效的 robot.txt 文件问题

apache - apache 反向代理上的 robots.txt

seo - robots.txt 阻止除 lighthouse 之外的所有内容