Skip to content

Commit f4bb7ea

Browse files
committed
Add basename support to match
1 parent 36c5222 commit f4bb7ea

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

modules/match.js

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,43 @@
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'
24
import { createRoutes } from './RouteUtils'
35
import useRoutes from './useRoutes'
46

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+
*/
518
function match({
619
routes,
720
location,
821
parseQueryString,
9-
stringifyQuery
22+
stringifyQuery,
23+
basename
1024
}, callback) {
11-
let history = useRoutes(createHistory)({
25+
invariant(
26+
location,
27+
'match needs a location'
28+
)
29+
30+
const history = createHistory({
1231
routes: createRoutes(routes),
1332
parseQueryString,
14-
stringifyQuery
33+
stringifyQuery,
34+
basename
1535
})
1636

37+
// Allow match({ location: '/the/path', ... })
38+
if (typeof location === 'string')
39+
location = history.createLocation(location)
40+
1741
history.match(location, function (error, redirectLocation, nextState) {
1842
callback(error, redirectLocation, nextState && { ...nextState, history })
1943
})

modules/useRoutes.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,6 @@ function useRoutes(createHistory) {
4949
// Continue from where we left off.
5050
finishMatch(partialNextState, callback)
5151
} else {
52-
// Allow match(path)
53-
if (typeof location === 'string')
54-
location = history.createLocation(location)
55-
5652
matchRoutes(routes, location, function (error, nextState) {
5753
if (error) {
5854
callback(error)

0 commit comments

Comments
 (0)