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

Commit aa077e8

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(""); }]); ```
1 parent 6a56461 commit aa077e8

File tree

5 files changed

+100
-77
lines changed

5 files changed

+100
-77
lines changed

docs/content/guide/$location.ngdoc

+2-2
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,11 @@ To configure the `$location` service, retrieve the
9999

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

104104
### Example configuration
105105
```js
106-
$locationProvider.html5Mode(true).hashPrefix('!');
106+
$locationProvider.html5Mode(true).hashPrefix('*');
107107
```
108108

109109
## Getter and setter methods

src/ng/location.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ function locationGetterSetter(property, preprocess) {
701701
* Use the `$locationProvider` to configure how the application deep linking paths are stored.
702702
*/
703703
function $LocationProvider() {
704-
var hashPrefix = '',
704+
var hashPrefix = '!',
705705
html5Mode = {
706706
enabled: false,
707707
requireBase: true,
@@ -712,6 +712,7 @@ function $LocationProvider() {
712712
* @ngdoc method
713713
* @name $locationProvider#hashPrefix
714714
* @description
715+
* The default value for the prefix is `'!'`.
715716
* @param {string=} prefix Prefix for hash part (containing path and search)
716717
* @returns {*} current value if used as getter or itself (chaining) if used as setter
717718
*/

test/ng/browserSpecs.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -755,9 +755,9 @@ describe('browser', function() {
755755
$rootScope.$apply(function() {
756756
$location.path('/initialPath');
757757
});
758-
expect(fakeWindow.location.href).toBe('http://server/#/initialPath');
758+
expect(fakeWindow.location.href).toBe('http://server/#!/initialPath');
759759

760-
fakeWindow.location.href = 'http://server/#/someTestHash';
760+
fakeWindow.location.href = 'http://server/#!/someTestHash';
761761

762762
$rootScope.$digest();
763763

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

779-
fakeWindow.location.href = 'http://server/#/someTestHash';
779+
fakeWindow.location.href = 'http://server/#!/someTestHash';
780780

781781
$rootScope.$digest();
782782

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

798-
fakeWindow.location.href = 'http://server/#/someTestHash';
798+
fakeWindow.location.href = 'http://server/#!/someTestHash';
799799

800800
$rootScope.$digest();
801801

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

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

0 commit comments

Comments
 (0)