PathLocationStrategy vs HashLocationStrategy in web apps

随声附和 提交于 2019-11-26 13:59:23

问题


What are the pros and cons of using:

  1. PathLocationStrategy - the default "HTML 5 pushState" style.
  2. HashLocationStrategy - the "hash URL" style.

for instance, using HashLocationStrategy will prevent the feature of scrolling to an element by its #ID, but some 3rd party plugins require the HashLocationStrategy or the Hashbang #! in order to work in ajax websites.

I would like to know which one offers more for a webapp.


回答1:


For me the main difference is that the PathLocationStrategy requires a configuration on the server side to all the paths configured in @RouteConfig to be redirected to the main HTML page of your Angular2 application. Otherwise you will have some 404 errors when trying to reload your application in the browser or try to access it using a particular URL.

Here is a question that could give you some hints about this:

  • When I refresh my website I get a 404. This is with Angular2 and firebase.

Hope it helps you, Thierry




回答2:


# can only be processed on the client, the servers just ignore them. This can cause problems with search engines (SEO), redirects can cause redundant page reloads. This page https://github.com/browserstate/history.js/wiki/Intelligent-State-Handling has some detailed explanation, while some of the arguments don't apply for Angular applications (for example - doesn't work with JS disabled).

The "disadvantage" of HTML5 pushstate is that is requires server support like explained by Thierry.




回答3:


According to official docs:

When the router navigates to a new component view, it updates the browser's location and history with a URL for that view. This is a strictly local URL. The browser shouldn't send this URL to the server and should not reload the page.

PathLocationStrategy

Modern HTML5 browsers support history.pushState, a technique that changes a browser's location and history without triggering a server page request. The router can compose a "natural" URL that is indistinguishable from one that would otherwise require a page load.

Here's the HTML5 pushState style URL that routes to the xyz component: localhost:4200/xyz/

HashLocationStrategy

Older browsers send page requests to the server when the location URL changes unless the change occurs after a # (called the hash). Routers can take advantage of this exception by composing in-application route URLs with hashes.

Here's a hash style URL that routes to the xyz component: localhost:4200/src/#/xyz/

I would like to know which one offers more for a webapp.

Almost all Angular projects should use the default HTML5 style as:

  1. It produces URLs that are easier for users to understand.
  2. It preserves the option to do server-side rendering later.

Rendering critical pages on the server is a technique that can greatly improve perceived responsiveness when the app first loads. An app that would otherwise take ten or more seconds to start could be rendered on the server and delivered to the user's device in less than a second.

This option is only available if application URLs look like normal web URLs without hashes (#) in the middle.

Stick with the default unless you have a compelling reason to resort to hash routes.



来源:https://stackoverflow.com/questions/34703343/pathlocationstrategy-vs-hashlocationstrategy-in-web-apps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!