Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

fix($location): always resolve relative links in html5mode to <base> url #8851

Merged
merged 2 commits into from
Aug 29, 2014

Conversation

tbosch
Copy link
Contributor

@tbosch tbosch commented Aug 29, 2014

BREAKING CHANGE (since 1.2.0 and 1.3.0-beta.1):

Angular now requires a <base> tag when html5 mode of $location is enabled. Reasoning:
Using html5 mode without a <base href="..."> tag makes relative links for images, links, ...
relative to the current url if the browser supports
the history API. However, if the browser does not support the history API Angular falls back to using the #,
and then all links would be broken.

BREAKING CHANGE (since 1.2.17 and 1.3.0-beta.10):

In html5 mode without a <base> tag on older browser that don't support the history API
relative paths were adding up. E.g. clicking on <a href="page1"> and then on <a href="page2">
would produce $location.path()==='/page1/page2'. The code that introduced this behavior was removed and Angular now also requires a` tag to be present when using html5 mode.

Closes #8172

@@ -127,21 +127,32 @@ function LocationHtml5Url(appBase, basePrefix) {
this.$$absUrl = appBaseNoFile + this.$$url.substr(1); // first char is always '/'
};

this.$$rewrite = function(url) {
this.$$parseLinkUrl = function(url, relHref) {
if (relHref && relHref[0] === '#') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<a href="#!/path/foo">foo</a> would be broken by this (more specifically, the LocationHashbangInHtml5Url case would break this --- but it's the same code in both)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is a breaking change. But in html5 mode you should not use those kinds of urls, right? They are used in plain hashbang mode (without html5Mode turned on)

@caitp
Copy link
Contributor

caitp commented Aug 29, 2014

It looks like this fails to deal with the ../relative and ./relative cases in hashbang mode, but in modern browsers it will work (urlResolve() won't fix this because stuff in the hash fragment is not understood by the browser)

…` url

BREAKING CHANGE (since 1.2.0 and 1.3.0-beta.1):

Angular now requires a `<base>` tag when html5 mode of `$location` is enabled. Reasoning:
Using html5 mode without a `<base href="...">` tag makes relative links for images, links, ...
relative to the current url if the browser supports
the history API. However, if the browser does not support the history API Angular falls back to using the `#`,
and then all those relative links would be broken.

The `<base>` tag is also needed when a deep url is loaded from the server, e.g. `http://server/some/page/url`.
In that case, Angular needs to decide which part of the url is the base of the application, and which part
is path inside of the application.

To summarize: Now all relative links are always relative to the `<base>` tag.

Exception (also a breaking change):
Link tags whose `href` attribute starts with a `#` will only change the hash of the url, but nothing else
(e.g. `<a href="#someAnchor">`). This is to make it easy to scroll to anchors inside a document.

Related to angular#6162
Closes angular#8492

BREAKING CHANGE (since 1.2.17 and 1.3.0-beta.10):

In html5 mode without a `<base>` tag on older browser that don't support the history API
relative paths were adding up. E.g. clicking on `<a href="page1">` and then on `<a href="page2">`
would produce `$location.path()==='/page1/page2'. The code that introduced this behavior was removed
and Angular now also requires a `<base>` tag to be present when using html5 mode.

Closes angular#8172, angular#8233
@tbosch
Copy link
Contributor Author

tbosch commented Aug 29, 2014

@caitp this is correct, but this also does not need to deal with those cases as:

  1. the <base> tag is required in html5 mode
  2. all relative urls are then relative to the <base> tag
  3. Angular ignores all links that go somewhere outside of the <base> tag.

I.e. a link of ../relative will go to a page below the <base> tag and would not be handled by Angular. On old and new browsers.

@tbosch
Copy link
Contributor Author

tbosch commented Aug 29, 2014

Landed as 2294880

@tbosch tbosch closed this Aug 29, 2014
@tbosch tbosch merged commit 2294880 into angular:master Aug 29, 2014
@tbosch tbosch deleted the location branch September 2, 2014 17:48
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Need to check "base" element in Html5 Hashbang mode
7 participants