Skip to content

Ensure match creates consistent histories #3089

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
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
11 changes: 10 additions & 1 deletion modules/__tests__/serverRendering-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ describe('server rendering', function () {
})

it('accepts a custom history', function (done) {
const history = createMemoryHistory()
const history = createMemoryHistory('/dashboard')
const spy = spyOn(history, 'createLocation').andCallThrough()

match({ history, routes, location: '/dashboard' }, function (error, redirectLocation, renderProps) {
Expand Down Expand Up @@ -162,6 +162,15 @@ describe('server rendering', function () {
})
})

it('produces a consistent history', function () {
match({ routes, location: '/dashboard' }, function (error, redirectLocation, renderProps) {
renderProps.router.listen(function (location) {
expect(location).toExist()
expect(location.pathname).toEqual('/dashboard')
})
})
})

describe('server/client consistency', function () {
// Just render to static markup here to avoid having to normalize markup.

Expand Down
32 changes: 16 additions & 16 deletions modules/match.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,26 @@ function match({ history, routes, location, ...options }, callback) {
'match needs a history or a location'
)

history = history ? history : createMemoryHistory(options)
// Make our own history
if (!history) {
// Ensure our history is created with an initial location
options = {
entries: [ location ],
...options
}

history = createMemoryHistory(options)
}

const transitionManager = createTransitionManager(
history,
createRoutes(routes)
)

let unlisten

if (location) {
// Allow match({ location: '/the/path', ... })
location = history.createLocation(location)
} else {
// Pick up the location from the history via synchronous history.listen
// call if needed.
unlisten = history.listen(historyLocation => {
location = historyLocation
})
}
// Pick up the location from the history via synchronous history.listen call.
const unlisten = history.listen(historyLocation => {
location = historyLocation
})

const router = createRouterObject(history, transitionManager)
history = createRoutingHistory(history, transitionManager)
Expand All @@ -57,9 +59,7 @@ function match({ history, routes, location, ...options }, callback) {
// Defer removing the listener to here to prevent DOM histories from having
// to unwind DOM event listeners unnecessarily, in case callback renders a
// <Router> and attaches another history listener.
if (unlisten) {
unlisten()
}
unlisten()
})
}

Expand Down