Skip to content
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

Improve support for server rendering async routes #2883

Merged
merged 1 commit into from
Jan 13, 2016
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
10 changes: 4 additions & 6 deletions modules/Router.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,10 @@ const Router = React.createClass({
},

getInitialState() {
return {
location: null,
routes: null,
params: null,
components: null
}
// Use initial state from renderProps when available, to allow using match
// on client side when doing server-side rendering.
const { location, routes, params, components } = this.props

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this! I just got async routes working with RC4, had some issues, but you fixed it for me :)

👍 @taion

return { location, routes, params, components }
},

handleError(error) {
Expand Down
9 changes: 5 additions & 4 deletions modules/__tests__/_bc-serverRendering-test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import expect from 'expect'
import React, { Component } from 'react'
import { renderToString } from 'react-dom/server'
import match from '../match'
import RouterContext from '../RouterContext'

import Link from '../Link'
import match from '../match'
import RoutingContext from '../RoutingContext'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This wasn't actually testing the v1 API. Now it is.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch!


describe('v1 server rendering', function () {

Expand Down Expand Up @@ -68,7 +69,7 @@ describe('v1 server rendering', function () {
it('works', function (done) {
match({ routes, location: '/dashboard' }, function (error, redirectLocation, renderProps) {
const string = renderToString(
<RouterContext {...renderProps} />
<RoutingContext {...renderProps} />
)
expect(string).toMatch(/The Dashboard/)
done()
Expand All @@ -78,7 +79,7 @@ describe('v1 server rendering', function () {
it('renders active Links as active', function (done) {
match({ routes, location: '/about' }, function (error, redirectLocation, renderProps) {
const string = renderToString(
<RouterContext {...renderProps} />
<RoutingContext {...renderProps} />
)
expect(string).toMatch(/about-is-active/)
expect(string).toNotMatch(/dashboard-is-active/)
Expand Down
70 changes: 65 additions & 5 deletions modules/__tests__/serverRendering-test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import expect, { spyOn } from 'expect'
import React, { Component } from 'react'
import { renderToString } from 'react-dom/server'
import match from '../match'
import { renderToStaticMarkup, renderToString } from 'react-dom/server'

import createMemoryHistory from '../createMemoryHistory'
import RouterContext from '../RouterContext'
import Link from '../Link'
import match from '../match'
import Router from '../Router'
import RouterContext from '../RouterContext'

describe('server rendering', function () {

Expand Down Expand Up @@ -43,6 +45,16 @@ describe('server rendering', function () {
}
}

class Async extends Component {
render() {
return (
<div className="Async">
<h1>Async</h1>
</div>
)
}
}

const DashboardRoute = {
path: '/dashboard',
component: Dashboard
Expand All @@ -60,13 +72,20 @@ describe('server rendering', function () {
}
}

const AsyncRoute = {
path: '/async',
getComponent(location, cb) {
setTimeout(cb(null, Async))
}
}

const routes = {
path: '/',
component: App,
childRoutes: [ DashboardRoute, AboutRoute, RedirectRoute ]
childRoutes: [ DashboardRoute, AboutRoute, RedirectRoute, AsyncRoute ]
}

it('works', function (done) {
it('works for synchronous route', function (done) {
match({ routes, location: '/dashboard' }, function (error, redirectLocation, renderProps) {
const string = renderToString(
<RouterContext {...renderProps} />
Expand All @@ -76,6 +95,16 @@ describe('server rendering', function () {
})
})

it('works for asynchronous route', function (done) {
match({ routes, location: '/async' }, function (error, redirectLocation, renderProps) {
const string = renderToString(
<RouterContext {...renderProps} />
)
expect(string).toMatch(/Async/)
done()
})
})

it('accepts a custom history', function (done) {
const history = createMemoryHistory()
const spy = spyOn(history, 'createLocation').andCallThrough()
Expand Down Expand Up @@ -122,4 +151,35 @@ describe('server rendering', function () {
})
})

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

it('should match for synchronous route', function (done) {
match({ routes, location: '/dashboard' }, function (error, redirectLocation, renderProps) {
const serverString = renderToStaticMarkup(
<RouterContext {...renderProps} />
)
const browserString = renderToStaticMarkup(
<Router {...renderProps} history={createMemoryHistory('/dashboard')} />
)

expect(browserString).toEqual(serverString)
done()
})
})

it('should match for asynchronous route', function (done) {
match({ routes, location: '/async' }, function (error, redirectLocation, renderProps) {
const serverString = renderToStaticMarkup(
<RouterContext {...renderProps} />
)
const browserString = renderToStaticMarkup(
<Router {...renderProps} history={createMemoryHistory('/async')} />
)

expect(browserString).toEqual(serverString)
done()
})
})
})
})