react-router:为什么比 hashHistory 更喜欢 browserHistory? [英] react-router: Why the preference for browserHistory over hashHistory?

查看:53
本文介绍了react-router:为什么比 hashHistory 更喜欢 browserHistory?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 React 比较陌生;如果这是一个非常幼稚的问题,我们深表歉意.

I'm relatively new to React; apologies if this is a really naive question.

browserHistory 有哪些技术优势使其优于hashHistory?例如,使用 History API 是否有重大的性能/效率提升?

What are the technical advantages to browserHistory that make it preferable over hashHistory? For example, is there a major performance/efficiency boost from it using the History API?

文档声明 browserHistory推荐,即使这是以附加服务器配置 并且需要通过 basename.

The docs state that browserHistory is recommended, even though this comes at the cost of the additional server config and needing to hard-code or configure your base URL for different servers via basename.

hashHistory 正常工作",但是,不管提供文件的基本 URL 是什么.不需要服务器配置.捆绑您的应用,从服务器上的任何网址/路径托管它,一切顺利.

hashHistory "just works", however, regardless of the base URL from which the files are served. No server config needed. Bundle your app, host it from any URL/path on a server, good to go.

如果文档能更进一步解释为什么推荐使用 browserHistory 可能会更好,即使它涉及更多复杂性.

It might be good if the docs went a bit further in explaining why it browserHistory is recommended even though it involves more complexity.

推荐答案

在某些情况下 hashHistory 很好 - 除非您开始处理需要知道完整 URL 的服务器端逻辑原始请求.

In some cases hashHistory is fine - except when you start dealing with server-side logic that needs to know a full URL of the original request.

浏览器不会在任何 HTTP 请求中发送 URL 的 #hash 部分.

Browsers do not send the #hash part of URL in any of HTTP requests.

因此,当用户请求页面时,服务器端(即 NodeJS)不会知道 URL 中的 #hash 是什么.

Therefore server-side (i.e. NodeJS) would not know what #hash was in the URL when user requested a page.

一个很好的例子是用户尝试加载需要登录的页面(通过 oAuth 等).在用户被带到单独的网站进行身份验证之前,您的应用程序的服务器端会告诉身份验证供应商在成功登录后将用户重定向到哪个 URL(通常是请求的原始 URL,因为大多数网站都是这样做的).如果您要使用 hashHistory - 服务器端将只知道 # 符号之前的位,并将用户重定向到您的应用程序的主页,而不是一个子页面用户想要加载.

A good example is user trying to load a page that requires a login (via oAuth etc.). Before user is taken to a separate website for authentication, you app's server-side would tell the authentication vendor what URL redirect user to after a successful login (usually it is to the original URL requested as most websites do this). If you were to use hashHistory - server-side would know only the bits before # symbol and would redirect user to the main page of your app and not a sub-page that user wanted to load.

我希望这是有道理的.

这篇关于react-router:为什么比 hashHistory 更喜欢 browserHistory?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆