How can I serve robots.txt on an SPA using React with Firebase hosting?

后端 未结 2 538
谎友^
谎友^ 2021-02-07 23:33

I have an SPA built using create-react-app and wish to have a robots.txt like this:

http://example.com/robots.txt

I see on this pa

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-07 23:51

    In my /public directory, I created a robots.txt.

    In my /src directory, I did the following:

    I created /src/index.js:

    import React from 'react'
    import ReactDOM from 'react-dom'
    import {TopApp} from './TopApp'
    import registerServiceWorker from './registerServiceWorker'
    
    import {BrowserRouter} from 'react-router-dom'
    
    ReactDOM.render(
      
        
      ,
      document.getElementById('react-render-root')
    )
    
    registerServiceWorker()
    

    I created /src/TopApp.js:

    import React from 'react'
    
    import {
      Switch,
      Route
    } from 'react-router-dom'
    
    import {ComingSoon} from './ComingSoon'
    import {App} from './App'
    
    export class TopApp extends React.Component {
      render() {
        return (
          
    ) } }

    Because path /robots.txt is not covered by the router paths provided, it took it from my public directory and robots file was published as desired.

    The same could be done for sitemap.xml.

提交回复
热议问题