How to use sitemap in firebase hosting with react app

无人久伴 提交于 2020-04-11 06:23:25

问题


I have deployed a reactjs web app on my firebase hosting. I added a sitemap.xml file to the public folder. I edited firebase.json to route the source to sitemap.xml:

"redirects": [
      {
        "source": "/sitemap",
        "destination": "/sitemap.xml",
        "type": 302
      }
    ],
"rewrites": [
  {
    "source": "/*",
    "destination": "/index.html"
  }
]

But when ever I go https://blah blah.com/sitemap it always returns index.html I tried to add it to rewrites as well but it seems it always returns index.html.

I am using React Router 4 as well for routing, maybe that effects this.

<Switch>
        <Route component={Login} exact path="/login" />
        <Route component={Home} path="/home" />
        <Route component={About} path="/about" />
        <Route component={Carousel} path="/carousel/:galleryRef/:ref" />
        <Route component={RelayEditPage} path="/edit/:galleryRef" />
        <Route path="/" render={() => <Redirect to="/home" />} />
      </Switch>

How can I add the sitemap to hosting domain? If I add a Redirect component inside the Switch it works but then I am unable to set Google search console sitemap because of the redirect.


回答1:


This worked for me:

"redirects": [
  {
    "source": "/sitemap",
    "destination": "/sitemap.xml",
    "type": 301
  },
  {
    "source": "/sitemap/**",
    "destination": "/sitemap.xml",
    "type": 302
  }
] 



回答2:


Try deleting index.html file. I have created a website in that I deleted the index.html from public folder and instead used index.js in functions folder.




回答3:


I had the same problem as you. Using the following configuration made the files in my public folder (including the sitemap) accessible online after deployment:

"rewrites": [
  {
    "source": "/",
    "destination": "/index.html"
  },
  {
    "source": "/sitemap",
    "destination": "/sitemap.xml"
  }
]


来源:https://stackoverflow.com/questions/50872098/how-to-use-sitemap-in-firebase-hosting-with-react-app

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