facebook/react-page-middleware — pull request #15 by tlack (2013-12-24 22:14:44+00:00)
Title: first draft of support for 'exposeInEnvironment'

I needed a way to get data exchanged between my node.js application logic and the React server-rendered components. As you may recall, server-rendered components are rendered inside an isolated VM context for security reasons:

https://github.com/facebook/react-page-middleware/blob/master/src/renderReactPage.js#L88

But that leaves me stuck when I need to find a way to communicate data into the component view - where will it get data from? Server-side rendered components don't even have access to any way to make http calls!

I inquired with petehunt and he told me I was on my own, so I just hacked something out that I think makes sense for React.

A more general improvement here would be to have the concept of React.sharedState.set() where we could put helper libraries, data, and constants that would cross the server/client boundary. But that's a bigger task than I am comfortable with.

Here's my commit with more details:

- passed an array of objects in buildConfig like so:

// initialize buildOptions as usual
// stuff to expose to the server renderer
var exposeInServerContext = {
request: require('request')
};
buildOptions.exposeInEnvironment = exposeInServerContext;
app.use(reactMiddleware.provide(buildOptions))

- will be available in a global context inside server-rendered
components

- maintains 'console' and 'renderResult' members as before

- seems to work for my exactly one use case