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