Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions modules/BrowserRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const BrowserRouter = ({ basename, keyLength, ...rest }) => (
<StaticRouter
action={action}
location={location}
basename={basename}
onPush={history.push}
onReplace={history.replace}
blockTransitions={history.block}
Expand Down
1 change: 1 addition & 0 deletions modules/HashRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const HashRouter = ({ basename, hashType, ...rest }) => (
<StaticRouter
action={action}
location={location}
basename={basename}
onPush={history.push}
onReplace={history.replace}
onGo={history.go}
Expand Down
4 changes: 3 additions & 1 deletion modules/StaticRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class StaticRouter extends React.Component {
children: PropTypes.oneOfType([ PropTypes.node, PropTypes.func ]),
createHref: PropTypes.func.isRequired,
location: PropTypes.oneOfType([ PropTypes.object, PropTypes.string ]).isRequired,
basename: PropTypes.string,
onPush: PropTypes.func.isRequired,
onReplace: PropTypes.func.isRequired,
stringifyQuery: PropTypes.func.isRequired,
Expand All @@ -45,7 +46,8 @@ class StaticRouter extends React.Component {

getChildContext() {
const createHref = (to) => {
const path = createRouterPath(to, this.props.stringifyQuery)
let path = createRouterPath(to, this.props.stringifyQuery)
if (this.props.basename) path = this.props.basename + path
return this.props.createHref(path)
}

Expand Down
29 changes: 29 additions & 0 deletions modules/__tests__/StaticRouter-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import expect from 'expect'
import React from 'react'
import StaticRouter from '../StaticRouter'
import { router as routerType } from '../PropTypes'
import { renderToString } from 'react-dom/server'

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

})
})

describe('basename support', () => {
class Test extends React.Component {
static contextTypes = {
router: routerType
}

render() {
return <div>{this.context.router.createHref('/bar')}</div>
}
}

const BASENAME = "/foo"
const routerProps = {
location: '/',
action: 'POP',
onPush: () => {},
onReplace: () => {}
}

it('uses the basename when creating hrefs', () => {
expect(renderToString(
<StaticRouter {...routerProps} basename={BASENAME}>
<Test />
</StaticRouter>
)).toContain(BASENAME)
})
})
})