Skip to content
Permalink
Browse files

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

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
Closes #14202

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("");
}]);
```
  • Loading branch information...
petebacondarwin committed Mar 9, 2016
1 parent 6a56461 commit aa077e81129c740041438688dff2e8d20c3d7b52
Showing with 100 additions and 77 deletions.
  1. +2 −2 docs/content/guide/$location.ngdoc
  2. +2 −1 src/ng/location.js
  3. +7 −7 test/ng/browserSpecs.js
  4. +88 −66 test/ng/locationSpec.js
  5. +1 −1 test/ngRoute/routeSpec.js
@@ -99,11 +99,11 @@ To configure the `$location` service, retrieve the

- **hashPrefix(prefix)**: {string}<br />
prefix used for Hashbang URLs (used in Hashbang mode or in legacy browser in Html5 mode)<br />
default: `""`
default: `"!"`

### Example configuration
```js
$locationProvider.html5Mode(true).hashPrefix('!');
$locationProvider.html5Mode(true).hashPrefix('*');
```

## Getter and setter methods
@@ -701,7 +701,7 @@ function locationGetterSetter(property, preprocess) {
* Use the `$locationProvider` to configure how the application deep linking paths are stored.
*/
function $LocationProvider() {
var hashPrefix = '',
var hashPrefix = '!',
html5Mode = {
enabled: false,
requireBase: true,
@@ -712,6 +712,7 @@ function $LocationProvider() {
* @ngdoc method
* @name $locationProvider#hashPrefix
* @description
* The default value for the prefix is `'!'`.
* @param {string=} prefix Prefix for hash part (containing path and search)
* @returns {*} current value if used as getter or itself (chaining) if used as setter
*/
@@ -755,9 +755,9 @@ describe('browser', function() {
$rootScope.$apply(function() {
$location.path('/initialPath');
});
expect(fakeWindow.location.href).toBe('http://server/#/initialPath');
expect(fakeWindow.location.href).toBe('http://server/#!/initialPath');

fakeWindow.location.href = 'http://server/#/someTestHash';
fakeWindow.location.href = 'http://server/#!/someTestHash';

$rootScope.$digest();

@@ -774,9 +774,9 @@ describe('browser', function() {
$rootScope.$apply(function() {
$location.path('/initialPath');
});
expect(fakeWindow.location.href).toBe('http://server/#/initialPath');
expect(fakeWindow.location.href).toBe('http://server/#!/initialPath');

fakeWindow.location.href = 'http://server/#/someTestHash';
fakeWindow.location.href = 'http://server/#!/someTestHash';

$rootScope.$digest();

@@ -793,9 +793,9 @@ describe('browser', function() {
$rootScope.$apply(function() {
$location.path('/initialPath');
});
expect(fakeWindow.location.href).toBe('http://server/#/initialPath');
expect(fakeWindow.location.href).toBe('http://server/#!/initialPath');

fakeWindow.location.href = 'http://server/#/someTestHash';
fakeWindow.location.href = 'http://server/#!/someTestHash';

$rootScope.$digest();

@@ -846,7 +846,7 @@ describe('browser', function() {
$rootScope.$digest();

// from $location for rewriting the initial url into a hash url
expect(browser.url).toHaveBeenCalledWith('http://server/#/some/deep/path', true);
expect(browser.url).toHaveBeenCalledWith('http://server/#!/some/deep/path', true);
expect(changeUrlCount).toBe(1);
});

Oops, something went wrong.

0 comments on commit aa077e8

Please sign in to comment.
You can’t perform that action at this time.