By default set HashLocationStrategy for Angular Project

HashLocationStrategy should be set by default in the angular project.

Suppose you have deployed Angular code in webserver(in PRD environment). When you refresh the page after navigating to some pages(for eg., http://xyz.com/foo/bar) you will receive 404 error. The reason is Browser tries to get new content from the server, but the server does not understand this request from where to bring the content.

As Angular code gets downloaded to the browser, it should not ask the server to send new content on refreshing the page

This can be avoided by setting HashLocationStrategy in AppModule

Due to this parameter, URL will have a hash fragment(for eg., http://xyz.com#/foo/bar). This says browser to check in his local files to get new URL details instead of sending the request to the server.

Another 404 issue you can avoid using this feature is if the user tries to open an angular page in another tab/window by using right-click option.

HashLocationStrategy in AppModule

HashLocationStrategy in AppModule

Reference – https://angular.io/api/common/HashLocationStrategy

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s