React router v4 服务静态文件 (robot.txt)

Jer*_*way 6 javascript robots.txt reactjs react-router

如何将我的 robots.txt 文件放到路径 www.domain.com/robots.txt?没有使用服务器,它唯一的前端带有反应路由器。

robots.txt --> 在根文件夹中。/

app.js --> 在 src 文件夹中 ./src/

(...)

export class App extends React.Component {
 render() {
  return (
   <div>
    <Switch>
      <Route exact path='/stuff' component={Stuff}/>
      <Route exact path='/' component={HomePage}/>
    </Switch>
   </div>
  )
 }
}
Run Code Online (Sandbox Code Playgroud)

如果我在本地测试它,它工作正常,localhost:4000/robots.txt 在浏览器中正确打开文件。但是在生产环境中恐怕不是这样?有任何想法吗?先感谢您

blu*_*eal 5

只需将robots.txtsitemap.xml如果有)放在build文件夹中。

看图片

我这样做并将其部署到firebase,我可以robots.txt通过https://my-site.firebaseapp.com/robots.txtsitemap.xml通过访问https://my-site.firebaseapp.com/sitemap.xml

你应该像我一样关注并robots.txt通过yoursite.com/robots.txt

注意:如果您的build文件夹在执行之前被删除npm run build,那么您需要再次放入robots.txtbuild文件夹。

更新:

如果您将robots.txtsitemap.xml放入public文件夹中,则build每次执行时它们都会自动复制到文件夹中npm run build