首页 > 解决方案 > 如何在带有 React 应用程序的 Firebase 托管中使用站点地图

问题描述

我已经在我的 firebase 主机上部署了一个 reactjs 网络应用程序。我将 sitemap.xml 文件添加到公用文件夹中。我编辑firebase.json将源路由到 sitemap.xml:

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

但是当我去的时候https://blah blah.com/sitemap它总是返回index.html我试图将它添加到重写中,但它似乎总是返回index.html

React Router 4也在使用路由,也许这会影响这个。

<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>

如何将站点地图添加到托管域?如果我在其中添加一个Redirect组件,Switch它可以工作,但是由于重定向,我无法设置 Google 搜索控制台站点地图。

标签: reactjsfirebasesitemapfirebase-hostinggoogle-search-api

解决方案


这对我有用:

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

推荐阅读