What happens when you enable the html5 mode in mode in angularjs?

ε祈祈猫儿з 提交于 2019-12-04 05:57:20

问题


What actually happens when you enable the html5 mode ? This might go back to the point how routing takes place in single page applications

What I perceived before(it may be wrong) : Looking at the dirty url in the angularjs application I always assumed it being url fragment to which different views are bind for different fragments. So in short we already have all the pages and a particular fragment is being displayed for a particular url .

Now in order to remove the hash you have to set html5mode true and you have to tell the server to return the index page for every request other than your apis . Kinda like

app.get('/ap1',some);
//more apis 
*
*
*
//in the end
app.get('*' ,(req,res,next) => req.sendFile('index.html'));

Shouldn't the request go the server and the page be reloaded everytime the url changes ?

and what does html5mode does to the browser ? In the newer frameworks like react and angular(2 or greater) , you don't even have to enable html5mode(except in angular 2 where you have to tell what kind of url you want) .

What is this sorcery ?


回答1:


This uses the History API.

It is designed so that developers can:

  • Tell the browser things like "I have used JavaScript to change the contents of the page. It is now the same as the page you would get if you asked for /other/page"
  • Intercept clicks on Back and Forward so that they can use JS to change the contents of the page to the be the same as the URLs that would be navigated to if the click on Back/Forward wasn't intercepted.

This updates the browser history and the URL in the address bar without triggering a normal navigation to a fresh page. The idea is that you can provide a smoother, faster transition between pages while still having real URLs that work well if you link to them or come back to them later.

That way:

  • If the JavaScript fails for any reason, the real URLs can still deliver the page
  • If a search engine indexes the URL, it gets the real data from it
  • If a visitor lands on a URL that isn't the homepage, they get the content they asked for immediately without having to wait for JavaScript to run, making additional Ajax requests, and assemble all the pieces client side.

It was designed in response to people using hangbangs instead of real URLs.


Now in order to remove the hash you have to set html5mode true and you have to tell the server to return the index page for every request other than your apis

This is terrible advice from Angular.

It abandons all of the good stuff that the history API can provide in order to have all the drawbacks of hashbangs, but with nicer looking URLs.

The problem with doing it properly is that it requires duplicating the logic on the server and the client, which is a lot of work. Isomorphic JavaScript is an approach to reduce this workload.


and what does html5mode does to the browser ?

Nothing. The html5mode variable is read by Angular. The browser doesn't care about it directly. It tells Angular to use the History API instead of hangbang URLs.


In the newer frameworks like react and angular(2 or greater) , you don't even have to enable html5mode(except in angular 2 where you have to tell what kind of url you want) .

They just use the History API by default.



来源:https://stackoverflow.com/questions/48166249/what-happens-when-you-enable-the-html5-mode-in-mode-in-angularjs

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