forked from remix-run/react-router
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatch.js
More file actions
66 lines (57 loc) · 1.9 KB
/
match.js
File metadata and controls
66 lines (57 loc) · 1.9 KB
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
import invariant from 'invariant'
import createMemoryHistory from './createMemoryHistory'
import createTransitionManager from './createTransitionManager'
import { createRoutes } from './RouteUtils'
import { createRouterObject, createRoutingHistory } from './RouterUtils'
/**
* A high-level API to be used for server-side rendering.
*
* This function matches a location to a set of routes and calls
* callback(error, redirectLocation, renderProps) when finished.
*
* Note: You probably don't want to use this in a browser unless you're using
* server-side rendering with async routes.
*/
function match({ history, routes, location, ...options }, callback) {
invariant(
history || location,
'match needs a history or a location'
)
history = history ? history : createMemoryHistory(options)
const transitionManager = createTransitionManager(
history,
createRoutes(routes)
)
let unlisten
if (location) {
// Allow match({ location: '/the/path', ... })
location = history.createLocation(location)
} else {
// Pick up the location from the history via synchronous history.listen
// call if needed.
unlisten = history.listen(historyLocation => {
location = historyLocation
})
}
const router = createRouterObject(history, transitionManager)
history = createRoutingHistory(history, transitionManager)
transitionManager.match(location, function (error, redirectLocation, nextState) {
callback(
error,
redirectLocation,
nextState && {
...nextState,
history,
router,
matchContext: { history, transitionManager, router }
}
)
// Defer removing the listener to here to prevent DOM histories from having
// to unwind DOM event listeners unnecessarily, in case callback renders a
// <Router> and attaches another history listener.
if (unlisten) {
unlisten()
}
})
}
export default match