|
1 |
| -import createHistory from 'history/lib/createMemoryHistory' |
| 1 | +import invariant from 'invariant' |
| 2 | +import createMemoryHistory from 'history/lib/createMemoryHistory' |
| 3 | +import useBasename from 'history/lib/useBasename' |
2 | 4 | import { createRoutes } from './RouteUtils'
|
3 | 5 | import useRoutes from './useRoutes'
|
4 | 6 |
|
| 7 | +const createHistory = useRoutes(useBasename(createMemoryHistory)) |
| 8 | + |
| 9 | +/** |
| 10 | + * A high-level API to be used for server-side rendering. |
| 11 | + * |
| 12 | + * This function matches a location to a set of routes and calls |
| 13 | + * callback(error, redirectLocation, renderProps) when finished. |
| 14 | + * |
| 15 | + * Note: You probably don't want to use this in a browser. Use |
| 16 | + * the history.listen API instead. |
| 17 | + */ |
5 | 18 | function match({
|
6 | 19 | routes,
|
7 | 20 | location,
|
8 | 21 | parseQueryString,
|
9 |
| - stringifyQuery |
| 22 | + stringifyQuery, |
| 23 | + basename |
10 | 24 | }, callback) {
|
11 |
| - let history = useRoutes(createHistory)({ |
| 25 | + invariant( |
| 26 | + location, |
| 27 | + 'match needs a location' |
| 28 | + ) |
| 29 | + |
| 30 | + const history = createHistory({ |
12 | 31 | routes: createRoutes(routes),
|
13 | 32 | parseQueryString,
|
14 |
| - stringifyQuery |
| 33 | + stringifyQuery, |
| 34 | + basename |
15 | 35 | })
|
16 | 36 |
|
| 37 | + // Allow match({ location: '/the/path', ... }) |
| 38 | + if (typeof location === 'string') |
| 39 | + location = history.createLocation(location) |
| 40 | + |
17 | 41 | history.match(location, function (error, redirectLocation, nextState) {
|
18 | 42 | callback(error, redirectLocation, nextState && { ...nextState, history })
|
19 | 43 | })
|
|
0 commit comments