简体   繁体   English

如何使用react-router处理生产版本中的刷新

[英]How to handle the refresh in production build using react-router

I have an app that's using react router. 我有一个使用React Router的应用程序。 I have already set up my dev environment to use it and I don't have the problem when I want to refresh the page, or the server do it itself, but when i make a production build. 我已经设置好了我的开发环境以使用它,并且当我要刷新页面或服务器自己执行它时,但是当我进行生产构建时,我没有问题。 If any user use the refresh button in the app, it will have a 404 error. 如果任何用户使用应用程序中的刷新按钮,将出现404错误。

I know in the server (my prod build will be in an ISS srv), the route made by react router what the app is trying to access doesn't exist, so how can I fix that problem? 我知道在服务器中(我的产品构建将在ISS srv中),由React Router进行的应用尝试访问的路由不存在,因此如何解决该问题?
what do I need to do and how to do it? 我需要做什么以及怎么做?
I'm pretty new on this. 我对此很陌生。

Thanks for your time and help! 感谢您的时间和帮助!

I will try to explain how react router manages your state of you Application. 我将尝试解释React Router如何管理您的应用程序状态。

Say u have following router 说您有以下路由器

<Router>
   <Route to="/" component={Home}>
     <Route to ="/about" component={About} />
    </Route>
<Router>

Say you are on home page of your application, which would be / . 假设您位于应用程序的主页上,该主页为/ Now say yoy navigate to /about url using Link on your home page. 现在说您可以使用主页上的Link导航到/about url。 This would be served by your react router. 这将由您的反应路由器提供。 So things work fine. 所以一切正常。

Now say at this point of time when you are at /about route you refresh the page. 现在说,在/about路由时,您刷新页面。 Now the web-browser like any page request(irrespective of whether its a single page application or not) would send a request to your backend server to get page. 现在,Web浏览器就像任何页面请求一样(无论是否是单个页面应用程序)都会向您的后端服务器发送请求以获取页面。 So you need to have routes configured in backend for the first page load. 因此,您需要在后端为第一页加载配置路由。

Now you must be compiling your react components maybe using webpack and getting a bundle output right? 现在,您必须使用Webpack编译React组件,并获得正确的bundle输出? This you put in your html file ? 你把这个放在你的html文件中吗? Now for all routes serve this html file only. 现在,对于所有路由,仅提供此html文件。

As you have your compiled JS file, when page renders on react-router renders the page automatically. 当您具有已编译的JS文件时,页面在react-router呈现时将自动呈现该页面。

Now for configuring routes there are multiple ways to do it. 现在,可以使用多种方法来配置路由。 One of them an be add all routes in the backend which render same html file with compiled javascript containing your react code. 其中之一是在后端添加所有路由,这些路由使用包含您的反应代码的已编译javascript渲染同一html文件。 Another can be use something like nginx and return the html from there only. 另一个可以使用类似nginx东西,并仅从那里返回html。

For your production setup, you'll need your ISS server set up so all incoming URL requests will return the index.html file. 对于生产设置,您需要设置ISS服务器,以便所有传入的URL请求都将返回index.html文件。 I'm not sure how this would be done in ISS, but in something like Apache, you'd use URL rewrites. 我不确定在ISS中如何做到这一点,但在类似Apache的应用中,您将使用URL重写。

The reason why you want to always return your index.html is because index.html is essentially your react app and the react-router component will handle the routing and render what is necessary based on the URL. 您希望始终返回index.html的原因是因为index.html本质上是您的react应用,而react-router组件将处理路由并根据URL呈现所需内容。

You would only not rewrite URLs that point to your static assets (css/js/pngs/etc. files). 您不会只重写指向静态资产的URL(css / js / pngs / etc。文件)。 For convenience, you would probably want to make all your static files available under a, for example, /static/ URL of your site. 为了方便起见,您可能希望使所有静态文件在站点的/static/ URL下可用。

So, you'd want your server configured so that ( * being a wildcard): 因此,您希望将服务器配置为( *为通配符):

/static/* returns whatever static resource is requested in the URL /static/*返回URL中请求的任何静态资源

/* returns your index.html page /*返回您的index.html页面

The reason why you don't worry about this when running in your dev environment is because webpack-dev-server is configured to do this all for you. 在开发环境中运行时,您不必为此担心的原因是,webpack-dev-server配置为可以为您完成所有这些操作。

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

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