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

feat($location): default hashPrefix to '!' #14202

Closed
wants to merge 2 commits into from

Conversation

petebacondarwin
Copy link
Member

The $location service is designed to support hash prefixed URLs
for cases where the browser does not support HTML5 push-state navigation.

The Google Ajax Crawling Scheme expects that local paths within a SPA start
with a hash-bang (e.g. somedomain.com/base/path/#!/client/side/path).

The $locationProvide allows the application developer to configure the
hashPrefix, and it is normal to set this to a bang '!', but the default
has always been the empty string ''.

This has caused some confusion where a user is not aware of this feature
and wonders why adding a hash value to the location (e.g. $location.hash('xxx'))
results in a double hash: ##xxx.

This commit changes the default value of the prefix to '!', which is more
natural and expected.

See https://developers.google.com/webmasters/ajax-crawling/docs/getting-started

Closes #13812

BREAKING CHANGE

The hash-prefix for $location hash-bang URLs has changed from the empty
string "" to the bang "!". If your application does not use HTML5 mode
or is being run on browsers that do not support HTML5 mode, and you have
not specified your own hash-prefix then client side URLs will now contain
a "!" prefix. For example, rather than mydomain.com/#/a/b/c will become
mydomain/#!/a/b/c.

If you actually wanted to have no hash-prefix then you should configure
this by adding a configuration block to you application:

appModule.config(['$locationProvider', function($locationProvider) {
  $locationProvider.hashPrefix("");
}]);

});
});

it('should not be hashbang mode', function() {
Copy link
Member

Choose a reason for hiding this comment

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

Isn't this test identical with the above ?

Copy link
Member Author

Choose a reason for hiding this comment

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

The body of the test may be identical but the sentiment is different.

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually the description of this test should be should not be htm5mode

@aciccarello
Copy link

Will old links get redirected to the new #! url?

somedomain.com/base/path/#/client/side/path -> somedomain.com/base/path/#!/client/side/path

@gkalpak
Copy link
Member

gkalpak commented Mar 10, 2016

@aciccarello, nope. That's part of the breaking change.
You should either migrate your URLs to including the ! or fallback to the old behavior by explicitly setting the hashPrefix to the empty string.

@gkalpak
Copy link
Member

gkalpak commented Mar 21, 2016

Implementation-wise it LGTM. I'm not yet convinced about this change (from a theoretical point of view), but it sure doesn't hurt (and it's easy to "work around" it for backward compatibility).

Assuming this won't change anything in terms of how search engines treat #!/path vs #/path URLs, I'm fine merging this 😃

The $location service is designed to support hash prefixed URLs
for cases where the browser does not support HTML5 push-state navigation.

The Google Ajax Crawling Scheme expects that local paths within a SPA start
with a hash-bang (e.g. `somedomain.com/base/path/#!/client/side/path`).

The `$locationProvide` allows the application developer to configure the
hashPrefix, and it is normal to set this to a bang '!', but the default
has always been the empty string ''.

This has caused some confusion where a user is not aware of this feature
and wonders why adding a hash value to the location (e.g. `$location.hash('xxx')`)
results in a double hash: `##xxx`.

This commit changes the default value of the prefix to '!', which is more
natural and expected.

See https://developers.google.com/webmasters/ajax-crawling/docs/getting-started

Closes angular#13812

BREAKING CHANGE

The hash-prefix for `$location` hash-bang URLs has changed from the empty
string "" to the bang "!". If your application does not use HTML5 mode
or is being run on browsers that do not support HTML5 mode, and you have
not specified your own hash-prefix then client side URLs will now contain
a "!" prefix. For example, rather than `mydomain.com/#/a/b/c` will become
`mydomain/#!/a/b/c`.

If you actually wanted to have no hash-prefix then you should configure
this by adding a configuration block to you application:

```
appModule.config(['$locationProvider', function($locationProvider) {
  $locationProvider.hashPrefix("");
}]);
```
EnricSala added a commit to EnricSala/publication-library that referenced this pull request May 3, 2017
The default navigation hash prefix has changed from '' to '!' in Angular 1.6:
angular/angular.js#14202
zowber added a commit to zowber/zowber-portfolio that referenced this pull request Apr 24, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

$location.hash inserts two hash signs
3 participants