javascript - How does the BrowserRouter relate to the server? - Stack Overflow

I read this statement:The BrowserRouter should be used when you have a server that will handle dynamic

I read this statement:

The BrowserRouter should be used when you have a server that will handle dynamic requests (knows how to respond to any possible URI), while the HashRouter should be used for static websites (can only respond to requests for files that it knows about).

I got it from this.

I don't understand what exactly it means when it says BrowserRouter should be used when you have a server that will handle dynamic requests. I thought when you click on a specific Link (in the case of React), the router will load the specific ponent and that is all done on the client side in Javascript (basically replacing elements in the DOM with the specific ponent code).

Where does the server e into play with the BrowserRouter as mentioned in the above statement?

I read this statement:

The BrowserRouter should be used when you have a server that will handle dynamic requests (knows how to respond to any possible URI), while the HashRouter should be used for static websites (can only respond to requests for files that it knows about).

I got it from this.

I don't understand what exactly it means when it says BrowserRouter should be used when you have a server that will handle dynamic requests. I thought when you click on a specific Link (in the case of React), the router will load the specific ponent and that is all done on the client side in Javascript (basically replacing elements in the DOM with the specific ponent code).

Where does the server e into play with the BrowserRouter as mentioned in the above statement?

Share Improve this question edited Sep 29, 2017 at 4:14 Priyantha 5,0916 gold badges30 silver badges46 bronze badges asked Sep 29, 2017 at 4:09 user5228393user5228393
Add a ment  | 

1 Answer 1

Reset to default 8

Updated for HashRouter:

In case of hash router, your client routes look like this - ://www.mysite./#login. As the # is a client-only fragment and is never sent to the server, even if the user bookmarked it, the browser will still request ://www.mysite./ only, removing the #login part. So the server never needs to be bothered with any dynamic paths. Although this may look simpler, on the negative side, you end up losing the traditional (and actual) purpose of #, which is to have local anchors. Hence this approach should be avoided unless you are targeting older browsers which don't have history API.

Earlier:

Suppose your web app is deployed on ://www.mysite./. And you have a client route at say /login on which you render a Login ponent which handles auth. So, when the user first visits your page, the server will typically send index.html to the browser and it will bootstrap your React app.

Then per the logic in your default ponent say App, you will redirect the user to the /login route and the Login ponent is mounted by react-router. The url now is ://www.mysite./login and so far all is good!

Now say the user bookmarked the page at this point and visited it later, or simply clicked on refresh. The browser will now ask the server for a page at ://www.mysite./login and this is where the server must send back index.html.

In-fact, for a single page app (aka SPA), for all the client-routes, the server must send back your app's bootstrap file, i.e. index.html.

This is what is meant by that statement in quotes. You must configure your server to send back index.html for any dynamic paths.

Hope this helps :).

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745638948a4637568.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信