Note: This question could also read:
How to support bookmarking of hashbang-less client side mvc frameworks
in Java.
I am transitioning an angular app that uses hashtags
to one that is html5mode
. I have successfully set
$locationProvider.html5Mode(true);
And all links from the landing page (index.html) work fine.
The problem is, if partial urls are referenced directly, I get a 404, naturally, since the server endpoint definitions aren't coupled to client side-defined routes.
So without HTML5 we get non-SEO friendly hashbangs, but with it we cannot bookmark anything other than the landing page (the page that bootstraps angular).
Why it works if requesting default landing page (index.html) first, ie htpp://mydomain.com/ :
- Browser requests index.html from server
- Server returns index.html, and browser loads angular framework
- URL changes are sent to the client side router and the proper partial/s are loaded.
Why it doesn't work if (ie) http://mydomain.com/foo is requested directly from the browser:
- Browser requests mydomain/foo from server.
- Resource doesn't exist
- Server returns 404
Something is missing from this story, I just don't know what. Here are the only two answers I can see...
- It's by design. Is this how it is supposed to work? That users must always land on the client MVC framework's bootstrap page (usually index.html), and then navigate from there. This is not ideal because state cannot be saved and there is no way to bookmark... not to mention crawling.
- Server solution. Is this worked around with a server side trick? For example, on all requests, return index.html and immediately call router with additional context. If so, this is against the objective that AngularJS is completely client-side and seems like a hack.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…