Skip to content
Open
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
25 changes: 20 additions & 5 deletions modules/AsyncProps.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ function last(arr) {
return arr[arr.length - 1]
}

/**
* We need to iterate over all components for specified routes.
* Components array can include objects if named components are used:
* https://github.com/rackt/react-router/blob/latest/docs/API.md#named-components
*
* @param components
* @param iterator
*/
function eachComponents(components, iterator) {
for (var i = 0, l = components.length; i < l; i++) {
if (typeof components[i] === 'object') {
Expand All @@ -29,7 +37,11 @@ function filterAndFlattenComponents(components) {
return flattened
}

function loadAsyncProps(components, params, cb) {
function defaultResolver(Component, params, cb) {
Component.loadProps(params, cb)
}

function loadAsyncProps(components, params, cb, resolver = defaultResolver) {
// flatten the multi-component routes
let componentsArray = []
let propsArray = []
Expand All @@ -46,7 +58,7 @@ function loadAsyncProps(components, params, cb) {
}

components.forEach((Component, index) => {
Component.loadProps(params, (error, props) => {
resolver(Component, params, (error, props) => {
needToLoadCounter--
propsArray[index] = props
componentsArray[index] = Component
Expand Down Expand Up @@ -112,7 +124,7 @@ function createElement(Component, props) {
return <Component {...props}/>
}

export function loadPropsOnServer({ components, params }, cb) {
export function loadPropsOnServer({ components, params, resolver }, cb) {
loadAsyncProps(
filterAndFlattenComponents(components),
params,
Expand All @@ -125,7 +137,8 @@ export function loadPropsOnServer({ components, params }, cb) {
const scriptString = `<script>__ASYNC_PROPS__ = ${json}</script>`
cb(null, propsAndComponents, scriptString)
}
}
},
resolver
)
}

Expand Down Expand Up @@ -189,6 +202,7 @@ class AsyncProps extends React.Component {
location: object.isRequired,
onError: func.isRequired,
renderLoading: func.isRequired,
resolver: func,

// server rendering
propsArray: array,
Expand Down Expand Up @@ -303,7 +317,8 @@ class AsyncProps extends React.Component {
prevProps: null
})
}
})
}),
this.props.resolver
)
}

Expand Down
45 changes: 45 additions & 0 deletions modules/__tests__/AsyncProps-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,33 @@ describe('AsyncProps', () => {
/>
), div, next)
})

it('allows to override resolver function', (done) => {
const appSpy = spyOn(App, 'loadProps').andCallThrough()

const next = execNext([
() => {},
() => {
expect(appSpy.calls.length).toEqual(1)
expect(appSpy.calls[0].arguments[2]).toEqual('check')
},
done
])

function resolver(Component, params, cb) {
Component.loadProps(params, cb, 'check')
Copy link
Owner

Choose a reason for hiding this comment

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

this would pass even w/o your changes, right? Maybe switch the name from loadProps to resolveProps or something.

Copy link
Author

Choose a reason for hiding this comment

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

It's impossible. there a lot of places where we are checking that loadProps method exists
We need to use this method now...

Copy link
Author

Choose a reason for hiding this comment

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

#42 (diff)

this will work only with resolver feature

}

App.setAssertions(next)

render((
<Router
history={createHistory('/')}
routes={routes}
render={(props) => <AsyncProps {...props} resolver={resolver} />}
/>
), div, next)
})
})

describe('server rendering', () => {
Expand All @@ -420,6 +447,24 @@ describe('AsyncProps', () => {
})
})

it('allows to override resolver function', (done) => {
const appSpy = spyOn(App, 'loadProps').andCallThrough()
function resolver(Component, params, cb) {
Component.loadProps(params, cb, 'check')
}
const loadPropsRoutes = {
path: '/',
component: App
}
match({ routes: loadPropsRoutes, location: '/' }, (err, redirect, renderProps) => {
loadPropsOnServer({ ...renderProps, resolver }, () => {
expect(appSpy.calls.length).toEqual(1)
expect(appSpy.calls[0].arguments[2]).toEqual('check')
done()
})
})
})

it('renders synchronously with props from hydration', () => {
const html = renderToString(
<Router
Expand Down