Skip to content

Commit 60421d2

Browse files
authored
Merge pull request #3858 from ReactTraining/you-so-basic
Fix basename support
2 parents ab57fa8 + c8a7dbb commit 60421d2

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

modules/BrowserRouter.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const BrowserRouter = ({ basename, keyLength, ...rest }) => (
1111
<StaticRouter
1212
action={action}
1313
location={location}
14+
basename={basename}
1415
onPush={history.push}
1516
onReplace={history.replace}
1617
blockTransitions={history.block}

modules/HashRouter.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const HashRouter = ({ basename, hashType, ...rest }) => (
1111
<StaticRouter
1212
action={action}
1313
location={location}
14+
basename={basename}
1415
onPush={history.push}
1516
onReplace={history.replace}
1617
onGo={history.go}

modules/StaticRouter.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class StaticRouter extends React.Component {
2020
children: PropTypes.oneOfType([ PropTypes.node, PropTypes.func ]),
2121
createHref: PropTypes.func.isRequired,
2222
location: PropTypes.oneOfType([ PropTypes.object, PropTypes.string ]).isRequired,
23+
basename: PropTypes.string,
2324
onPush: PropTypes.func.isRequired,
2425
onReplace: PropTypes.func.isRequired,
2526
stringifyQuery: PropTypes.func.isRequired,
@@ -45,7 +46,8 @@ class StaticRouter extends React.Component {
4546

4647
getChildContext() {
4748
const createHref = (to) => {
48-
const path = createRouterPath(to, this.props.stringifyQuery)
49+
let path = createRouterPath(to, this.props.stringifyQuery)
50+
if (this.props.basename) path = this.props.basename + path
4951
return this.props.createHref(path)
5052
}
5153

modules/__tests__/StaticRouter-test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import expect from 'expect'
22
import React from 'react'
33
import StaticRouter from '../StaticRouter'
4+
import { router as routerType } from '../PropTypes'
45
import { renderToString } from 'react-dom/server'
56

67
//console.error = () => {}
@@ -187,4 +188,32 @@ describe('StaticRouter', () => {
187188

188189
})
189190
})
191+
192+
describe('basename support', () => {
193+
class Test extends React.Component {
194+
static contextTypes = {
195+
router: routerType
196+
}
197+
198+
render() {
199+
return <div>{this.context.router.createHref('/bar')}</div>
200+
}
201+
}
202+
203+
const BASENAME = "/foo"
204+
const routerProps = {
205+
location: '/',
206+
action: 'POP',
207+
onPush: () => {},
208+
onReplace: () => {}
209+
}
210+
211+
it('uses the basename when creating hrefs', () => {
212+
expect(renderToString(
213+
<StaticRouter {...routerProps} basename={BASENAME}>
214+
<Test />
215+
</StaticRouter>
216+
)).toContain(BASENAME)
217+
})
218+
})
190219
})

0 commit comments

Comments
 (0)