Skip to content

Commit 1790854

Browse files
committed
Fix links in server-side rendering
1 parent c85bc41 commit 1790854

File tree

7 files changed

+142
-138
lines changed

7 files changed

+142
-138
lines changed

modules/BrowserHistory.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export class BrowserHistory extends DOMHistory {
3838
window.history.replaceState(state, '');
3939
}
4040

41-
this.location = this._createLocation(getWindowPath(), state, navigationType);
41+
this.location = this.createLocation(getWindowPath(), state, navigationType);
4242
}
4343

4444
setup() {
@@ -86,7 +86,7 @@ export class BrowserHistory extends DOMHistory {
8686
state = this._createState(state);
8787

8888
window.history.pushState(state, '', path);
89-
this.location = this._createLocation(path, state, NavigationTypes.PUSH);
89+
this.location = this.createLocation(path, state, NavigationTypes.PUSH);
9090
this._notifyChange();
9191
} else {
9292
window.location = path;
@@ -99,7 +99,7 @@ export class BrowserHistory extends DOMHistory {
9999
state = this._createState(state);
100100

101101
window.history.replaceState(state, '', path);
102-
this.location = this._createLocation(path, state, NavigationTypes.REPLACE);
102+
this.location = this.createLocation(path, state, NavigationTypes.REPLACE);
103103
this._notifyChange();
104104
} else {
105105
window.location.replace(path);

modules/ChangeEmitter.js

Lines changed: 0 additions & 24 deletions
This file was deleted.

modules/HashHistory.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export class HashHistory extends DOMHistory {
8282
_updateLocation(navigationType) {
8383
var path = getHashPath();
8484
var state = this.queryKey ? readState(path, this.queryKey) : null;
85-
this.location = this._createLocation(path, state, navigationType);
85+
this.location = this.createLocation(path, state, navigationType);
8686
}
8787

8888
setup() {
@@ -145,7 +145,7 @@ export class HashHistory extends DOMHistory {
145145
this._ignoreNextHashChange = true;
146146
window.location.hash = path;
147147

148-
this.location = this._createLocation(path, state, NavigationTypes.PUSH);
148+
this.location = this.createLocation(path, state, NavigationTypes.PUSH);
149149

150150
this._notifyChange();
151151
}
@@ -159,7 +159,7 @@ export class HashHistory extends DOMHistory {
159159
this._ignoreNextHashChange = true;
160160
replaceHashPath(path);
161161

162-
this.location = this._createLocation(path, state, NavigationTypes.REPLACE);
162+
this.location = this.createLocation(path, state, NavigationTypes.REPLACE);
163163

164164
this._notifyChange();
165165
}

modules/History.js

Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import invariant from 'invariant';
2-
import { getPathname, getQueryString, parseQueryString, stringifyQuery } from './URLUtils';
3-
import ChangeEmitter from './ChangeEmitter';
2+
import { getPathname, getQueryString, parseQueryString } from './URLUtils';
43
import Location from './Location';
54

6-
var RequiredSubclassMethods = [ 'pushState', 'replaceState', 'go' ];
5+
var RequiredHistorySubclassMethods = [ 'pushState', 'replaceState', 'go' ];
76

87
function createRandomKey() {
98
return Math.random().toString(36).substr(2);
@@ -18,22 +17,35 @@ function createRandomKey() {
1817
* - replaceState(state, path)
1918
* - go(n)
2019
*/
21-
class History extends ChangeEmitter {
20+
class History {
2221

2322
constructor(options={}) {
24-
super();
25-
26-
this.parseQueryString = options.parseQueryString || parseQueryString;
27-
this.stringifyQuery = options.stringifyQuery || stringifyQuery;
28-
this.location = null;
29-
30-
RequiredSubclassMethods.forEach(function (method) {
23+
RequiredHistorySubclassMethods.forEach(function (method) {
3124
invariant(
3225
typeof this[method] === 'function',
3326
'%s needs a "%s" method',
3427
this.constructor.name, method
3528
);
3629
}, this);
30+
31+
this.parseQueryString = options.parseQueryString || parseQueryString;
32+
this.changeListeners = [];
33+
this.location = null;
34+
}
35+
36+
_notifyChange() {
37+
for (var i = 0, len = this.changeListeners.length; i < len; ++i)
38+
this.changeListeners[i].call(this);
39+
}
40+
41+
addChangeListener(listener) {
42+
this.changeListeners.push(listener);
43+
}
44+
45+
removeChangeListener(listener) {
46+
this.changeListeners = this.changeListeners.filter(function (li) {
47+
return li !== listener;
48+
});
3749
}
3850

3951
back() {
@@ -53,36 +65,13 @@ class History extends ChangeEmitter {
5365
return state;
5466
}
5567

56-
_createLocation(path, state, navigationType) {
68+
createLocation(path, state, navigationType) {
5769
var pathname = getPathname(path);
5870
var queryString = getQueryString(path);
5971
var query = queryString ? this.parseQueryString(queryString) : null;
6072
return new Location(pathname, query, state, navigationType);
6173
}
6274

63-
/**
64-
* Returns a full URL path from the given pathname and query.
65-
*/
66-
makePath(pathname, query) {
67-
if (query) {
68-
if (typeof query !== 'string')
69-
query = this.stringifyQuery(query);
70-
71-
if (query !== '')
72-
return pathname + '?' + query;
73-
}
74-
75-
return pathname;
76-
}
77-
78-
/**
79-
* Returns a string that may safely be used to link to the given
80-
* pathname and query.
81-
*/
82-
makeHref(pathname, query) {
83-
return this.makePath(pathname, query);
84-
}
85-
8675
}
8776

8877
export default History;

modules/MemoryHistory.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function createEntry(object) {
1717
* for testing because it allows you to specify route history
1818
* entries in the constructor.
1919
*/
20-
export class MemoryHistory extends History {
20+
class MemoryHistory extends History {
2121

2222
constructor(entries, current) {
2323
super();
@@ -47,7 +47,7 @@ export class MemoryHistory extends History {
4747

4848
var currentEntry = entries[current];
4949

50-
this.location = this._createLocation(
50+
this.location = this.createLocation(
5151
currentEntry.path,
5252
currentEntry.state
5353
);
@@ -59,7 +59,7 @@ export class MemoryHistory extends History {
5959

6060
this.current += 1;
6161
this.entries = this.entries.slice(0, this.current).concat([{ state, path }]);
62-
this.location = this._createLocation(path, state, NavigationTypes.PUSH);
62+
this.location = this.createLocation(path, state, NavigationTypes.PUSH);
6363

6464
this._notifyChange();
6565
}
@@ -69,7 +69,7 @@ export class MemoryHistory extends History {
6969
state = this._createState(state);
7070

7171
this.entries[this.current] = { state, path };
72-
this.location = this._createLocation(path, state, NavigationTypes.REPLACE);
72+
this.location = this.createLocation(path, state, NavigationTypes.REPLACE);
7373

7474
this._notifyChange();
7575
}
@@ -87,7 +87,7 @@ export class MemoryHistory extends History {
8787
this.current += n;
8888
var currentEntry = this.entries[this.current];
8989

90-
this.location = this._createLocation(
90+
this.location = this.createLocation(
9191
currentEntry.path,
9292
currentEntry.state,
9393
NavigationTypes.POP

0 commit comments

Comments
 (0)