Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React router v4 serve static file (robot.txt)

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

like image 653
Jeremie_Highway Avatar asked Mar 13 '19 17:03

Jeremie_Highway


1 Answers

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

see image

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

like image 109
blueseal Avatar answered Oct 17 '22 04:10

blueseal