简体   繁体   English

Netlify React 中的高级路由

[英]Advanced Routing in Netlify React

I had problems with the routing after deploying my react app on netlify.在 netlify 上部署我的 React 应用程序后,我遇到了路由问题。 I inserted a _redirects file to my public folder with this code /* /index.html 200 as recommended.我按照建议使用此代码/* /index.html 200将 _redirects 文件插入到我的公共文件夹中。

Yet, when I refresh the page from an advanced route eg '/sceneries/beach', it always pushes me back to the index page.然而,当我从高级路线(例如“/sceneries/beach”)刷新页面时,它总是将我推回到索引页面。 Locally it has been working perfectly before.在本地,它之前一直运行良好。 Do you know how to solve this?你知道如何解决这个问题吗? My routes currently look like this:我的路线目前是这样的:

in index.js在 index.js 中

ReactDOM.render((
<Router history={history}>
    <App />
</Router>
), document.getElementById('root'));

in App.js在 App.js 中

<BrowserRouter>
          <Route path='/' component ={Home} />
          <Route path='/sceneries/:id' component ={Comparison} />
          <Redirect from="*" to="/" />
      </BrowserRouter>

Thanks a lot in advance!非常感谢! I really appreciate some help!我真的很感谢一些帮助!

I had the same problem as you when I created a React app and deployed it on Netlify.当我创建一个 React 应用程序并将其部署在 Netlify 上时,我遇到了与您相同的问题。 This is what worked for me, based on this solution :基于此解决方案对我有用

  1. Create a file named netlify.toml in the root/main folder of your project.在项目的 root/main 文件夹中创建一个名为netlify.toml的文件。
  2. Inside of this file, add this code:在此文件中,添加以下代码:

[[redirects]] from = "/*" to = "/index.html" status = 200

  1. Save and deploy your website with git add commit push.使用 git add commit push 保存和部署您的网站。
  2. Wait for your site to re-deploy on Netlify.等待您的站点在 Netlify 上重新部署。
  3. Test your website with refresh.刷新测试您的网站。

Let me know if you have any additional questions, I'm happy to help :)如果您有任何其他问题,请告诉我,我很乐意为您提供帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM