React router v4 服务静态文件 (robot.txt) [英] React router v4 serve static file (robot.txt)

查看:38
本文介绍了React router v4 服务静态文件 (robot.txt)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

(...)导出类 App 扩展 React.Component {使成为() {返回 (<div><开关><路由精确路径='/stuff' component={Stuff}/><路由精确路径='/' component={HomePage}/></开关>

)}}

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

解决方案

只需将 robots.txt(和 sitemap.xml 如果有)放在 build 文件夹.

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

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

访问 robots.txt

注意:如果你的build文件夹在执行npm run build之前被删除了,那么你需要再次把robots.txtbuild 文件夹中.

更新:

如果你把 robots.txtsitemap.xml 放在 public 文件夹中,那么它们会自动复制到 build 文件夹每次执行 npm run build

How can I put my robots.txt file to the path www.domain.com/robots.txt? No server is used, its only frontend with react router.

robots.txt --> in root folder ./

app.js --> in src folder ./src/

(...)

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

If I test it locally, it works OK, localhost:4000/robots.txt opens the file properly in the browser. However I am afraid it is not case in production environment? Any ideas? Thank you in advance

解决方案

Just put robots.txt (and sitemap.xml if any) in build folder.

I did this and deployed it to firebase and I can access robots.txt through https://my-site.firebaseapp.com/robots.txt and sitemap.xml through https://my-site.firebaseapp.com/sitemap.xml

You should follow like mine and access robots.txt through yoursite.com/robots.txt

Note: If your build folder gets deleted before performing npm run build, then you need to again put robots.txt in the build folder.

UPDATE:

if you put robots.txt and sitemap.xml in the public folder, then they will auto get copied to the build folder every time you perform npm run build

这篇关于React router v4 服务静态文件 (robot.txt)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆