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

前端 未结 1 1806
萌比男神i
萌比男神i 2021-01-24 08:04

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

1条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-24 08:27

    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.

    0 讨论(0)
提交回复
热议问题