【问题标题】:Netlify does not recognize the URL params when using react-router-dom使用 react-router-dom 时,Netlify 无法识别 URL 参数
【发布时间】:2019-06-05 21:19:34
【问题描述】:

我正在创建一个使用反应路由器的反应应用程序。我正在使用路由器来匹配:/bankName-:credit 之类的路径,它在本地开发中运行良好。我的应用程序唯一需要的路径是:/bankName-:credit,其他所有路径都将命中404。 但是,当我将此应用程序部署到 netlify 时,默认情况下它会转到 / 并显示自定义 404。这都很好。但是现在,如果我尝试转到 /hdfc-500,那么它会给出一条 netlify not found 消息,即 page not found

我尝试使用_redirects 中提到的netlify docs,但这不起作用。

这是我的路线:-

App.js

<Route path='/:bankCode-:credit' component={NestedRoutes} />
<Route component={NotFound} />

这是我的NestedRoutes 组件:-

const NestedRoutes = ({ match }) => (
  <Suspense fallback={<LinearProgress />}>
    <Switch>
      <Route exact path={`${match.path}/sc-generate`} component={SCGenerate} />
      <Route exact path='/:bankCode-:credit' component={Home} />
      <Route component={NotFound} />
    </Switch>
  </Suspense>
)

我在_redirects 文件中使用以下代码:-

/* /:bankCode-:credit

但它试图与/:bankCode-:credit完全匹配

我应该怎么做才能解决这个问题?

【问题讨论】:

    标签: reactjs deployment react-router netlify netlify-cli


    【解决方案1】:

    我在这里重现了你的问题https://codesandbox.io/s/trusting-frost-ls353

    解决方法很简单,将名为_redirects 的文件添加到您的公用文件夹中,其中包含此内容

    /* /index.html 200
    

    您可以在此链接上找到更多信息。 https://www.slightedgecoder.com/2018/12/18/page-not-found-on-netlify-with-react-router/

    【讨论】:

    • 谢谢,它解决了我的问题。我正在尝试不同的重定向,但这个解决了我的问题。
    【解决方案2】:

    我在 在 netlify 中传递 url 参数时遇到了这个问题,并通过删除“。”得到了解决。 在 build 文件夹内的 index.html 文件中的 href 和 src 链接之前。

    希望这会对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 2020-02-16
      • 2018-12-08
      • 2018-01-27
      • 1970-01-01
      • 2018-02-28
      • 1970-01-01
      • 1970-01-01
      • 2023-01-21
      • 1970-01-01
      相关资源
      最近更新 更多