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

Commit 5b0a007

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 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 efd448d commit 5b0a007

File tree

6 files changed

+105
-77
lines changed

6 files changed

+105
-77
lines changed

.vscode/settings.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Place your settings in this file to overwrite default and user settings.
2+
{
3+
// When enabled, will trim trailing whitespace when you save a file.
4+
"files.trimTrailingWhitespace": true
5+
}

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
@@ -747,9 +747,9 @@ describe('browser', function() {
747747
$rootScope.$apply(function() {
748748
$location.path('/initialPath');
749749
});
750-
expect(fakeWindow.location.href).toBe('http://server/#/initialPath');
750+
expect(fakeWindow.location.href).toBe('http://server/#!/initialPath');
751751

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

754754
$rootScope.$digest();
755755

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

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

773773
$rootScope.$digest();
774774

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

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

792792
$rootScope.$digest();
793793

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

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

0 commit comments

Comments
 (0)