@@ -2,8 +2,28 @@ var invariant = require('react/lib/invariant');
2
2
var ExecutionEnvironment = require ( 'react/lib/ExecutionEnvironment' ) ;
3
3
var getWindowPath = require ( '../helpers/getWindowPath' ) ;
4
4
5
+ function getHashPath ( ) {
6
+ return window . location . hash . substr ( 1 ) ;
7
+ }
8
+
9
+ function ensureSlash ( ) {
10
+ var path = getHashPath ( ) ;
11
+
12
+ if ( path . charAt ( 0 ) === '/' )
13
+ return true ;
14
+
15
+ HashLocation . replace ( '/' + path ) ;
16
+
17
+ return false ;
18
+ }
19
+
5
20
var _onChange ;
6
21
22
+ function handleHashChange ( ) {
23
+ if ( ensureSlash ( ) )
24
+ _onChange ( ) ;
25
+ }
26
+
7
27
/**
8
28
* A Location that uses `window.location.hash`.
9
29
*/
@@ -17,22 +37,20 @@ var HashLocation = {
17
37
18
38
_onChange = onChange ;
19
39
20
- // Make sure the hash is at least / to begin with.
21
- if ( window . location . hash === '' )
22
- window . location . replace ( getWindowPath ( ) + '#/' ) ;
40
+ ensureSlash ( ) ;
23
41
24
42
if ( window . addEventListener ) {
25
- window . addEventListener ( 'hashchange' , _onChange , false ) ;
43
+ window . addEventListener ( 'hashchange' , handleHashChange , false ) ;
26
44
} else {
27
- window . attachEvent ( 'onhashchange' , _onChange ) ;
45
+ window . attachEvent ( 'onhashchange' , handleHashChange ) ;
28
46
}
29
47
} ,
30
48
31
49
teardown : function ( ) {
32
50
if ( window . removeEventListener ) {
33
- window . removeEventListener ( 'hashchange' , _onChange , false ) ;
51
+ window . removeEventListener ( 'hashchange' , handleHashChange , false ) ;
34
52
} else {
35
- window . detachEvent ( 'onhashchange' , _onChange ) ;
53
+ window . detachEvent ( 'onhashchange' , handleHashChange ) ;
36
54
}
37
55
} ,
38
56
@@ -48,9 +66,7 @@ var HashLocation = {
48
66
window . history . back ( ) ;
49
67
} ,
50
68
51
- getCurrentPath : function ( ) {
52
- return window . location . hash . substr ( 1 ) ;
53
- } ,
69
+ getCurrentPath : getHashPath ,
54
70
55
71
toString : function ( ) {
56
72
return '<HashLocation>' ;
0 commit comments