forked from remix-run/history
-
Notifications
You must be signed in to change notification settings - Fork 0
/
createBrowserHistory.js
163 lines (130 loc) · 3.92 KB
/
createBrowserHistory.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import invariant from 'invariant'
import { PUSH, POP } from './Actions'
import { parsePath } from './PathUtils'
import { canUseDOM } from './ExecutionEnvironment'
import { addEventListener, removeEventListener, getWindowPath, supportsHistory } from './DOMUtils'
import { saveState, readState } from './DOMStateStorage'
import createDOMHistory from './createDOMHistory'
/**
* Creates and returns a history object that uses HTML5's history API
* (pushState, replaceState, and the popstate event) to manage history.
* This is the recommended method of managing history in browsers because
* it provides the cleanest URLs.
*
* Note: In browsers that do not support the HTML5 history API full
* page reloads will be used to preserve URLs.
*/
function createBrowserHistory(options={}) {
invariant(
canUseDOM,
'Browser history needs a DOM'
)
const { forceRefresh } = options
const isSupported = supportsHistory()
const useRefresh = !isSupported || forceRefresh
function getCurrentLocation(historyState) {
try {
historyState = historyState || window.history.state || {}
} catch (e) {
historyState = {}
}
const path = getWindowPath()
let { key } = historyState
let state
if (key) {
state = readState(key)
} else {
state = null
key = history.createKey()
if (isSupported)
window.history.replaceState({ ...historyState, key }, null, path)
}
const location = parsePath(path)
return history.createLocation({ ...location, state }, undefined, key)
}
function startPopStateListener({ transitionTo }) {
function popStateListener(event) {
if (event.state === undefined)
return // Ignore extraneous popstate events in WebKit.
transitionTo(
getCurrentLocation(event.state)
)
}
addEventListener(window, 'popstate', popStateListener)
return function () {
removeEventListener(window, 'popstate', popStateListener)
}
}
function finishTransition(location) {
const { basename, pathname, search, hash, state, action, key } = location
if (action === POP)
return // Nothing to do.
saveState(key, state)
const path = (basename || '') + pathname + search + hash
const historyState = {
key
}
if (action === PUSH) {
if (useRefresh) {
window.location.href = path
return false // Prevent location update.
} else {
window.history.pushState(historyState, null, path)
}
} else { // REPLACE
if (useRefresh) {
window.location.replace(path)
return false // Prevent location update.
} else {
window.history.replaceState(historyState, null, path)
}
}
}
const history = createDOMHistory({
...options,
getCurrentLocation,
finishTransition,
saveState
})
let listenerCount = 0, stopPopStateListener
function listenBefore(listener) {
if (++listenerCount === 1)
stopPopStateListener = startPopStateListener(history)
const unlisten = history.listenBefore(listener)
return function () {
unlisten()
if (--listenerCount === 0)
stopPopStateListener()
}
}
function listen(listener) {
if (++listenerCount === 1)
stopPopStateListener = startPopStateListener(history)
const unlisten = history.listen(listener)
return function () {
unlisten()
if (--listenerCount === 0)
stopPopStateListener()
}
}
// deprecated
function registerTransitionHook(hook) {
if (++listenerCount === 1)
stopPopStateListener = startPopStateListener(history)
history.registerTransitionHook(hook)
}
// deprecated
function unregisterTransitionHook(hook) {
history.unregisterTransitionHook(hook)
if (--listenerCount === 0)
stopPopStateListener()
}
return {
...history,
listenBefore,
listen,
registerTransitionHook,
unregisterTransitionHook
}
}
export default createBrowserHistory