React router v4 serve static file (robot.txt)

喜你入骨 提交于 2020-01-04 02:05:10

问题


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


回答1:


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

I did this and deployed 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: Everytime you need to put robots.txt in build folder if you delete build folder before performing npm run build



来源:https://stackoverflow.com/questions/55148238/react-router-v4-serve-static-file-robot-txt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!