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
57 lines (49 loc) · 1.56 KB
/
match.js
File metadata and controls
57 lines (49 loc) · 1.56 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
import { REPLACE } from 'history/lib/Actions'
import invariant from 'invariant'
import createMemoryHistory from './createMemoryHistory'
import createTransitionManager from './createTransitionManager'
import { createRoutes } from './RouteUtils'
import { createRouterObject } 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)
)
if (location) {
// Allow match({ location: '/the/path', ... })
location = history.createLocation(location)
} else {
location = history.getCurrentLocation()
}
transitionManager.match(location, (error, redirectLocation, nextState) => {
let renderProps
if (nextState) {
const router = createRouterObject(history, transitionManager, nextState)
renderProps = {
...nextState,
router,
matchContext: { transitionManager, router }
}
}
callback(
error,
redirectLocation && history.createLocation(redirectLocation, REPLACE),
renderProps
)
})
}
export default match