-
Notifications
You must be signed in to change notification settings - Fork 322
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #284 from goatslacker/server-rendering
The holy-grail of server rendering
- Loading branch information
Showing
9 changed files
with
578 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
{ | ||
"env": { | ||
"node": true, | ||
"browser": true, | ||
"es6": true | ||
}, | ||
"ecmaFeatures": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import Iso from 'iso' | ||
import * as Render from './Render' | ||
|
||
export default { | ||
define: Render.withData, | ||
|
||
render(alt, Component, props) { | ||
// recycle state | ||
alt.recycle() | ||
|
||
if (typeof window === 'undefined') { | ||
return Render.toString(Component, props).then((markup) => { | ||
return Iso.render(markup, alt.takeSnapshot()) | ||
}) | ||
} else { | ||
return Promise.resolve( | ||
Iso.bootstrap((state, _, node) => { | ||
alt.bootstrap(state) | ||
Render.toDOM(Component, props, node) | ||
}) | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import React from 'react' | ||
|
||
export function withData(fetch, MaybeComponent) { | ||
function bind(Component) { | ||
return React.createClass({ | ||
contextTypes: { | ||
buffer: React.PropTypes.object.isRequired | ||
}, | ||
|
||
childContextTypes: { | ||
buffer: React.PropTypes.object.isRequired | ||
}, | ||
|
||
getChildContext() { | ||
return { buffer: this.context.buffer } | ||
}, | ||
|
||
componentWillMount() { | ||
if (!this.context.buffer.locked) { | ||
this.context.buffer.push( | ||
fetch(this.props) | ||
) | ||
} | ||
}, | ||
|
||
render() { | ||
return this.context.buffer.locked | ||
? React.createElement(Component, this.props) | ||
: null | ||
} | ||
}) | ||
} | ||
|
||
// works as a decorator or as a function | ||
return MaybeComponent ? bind(MaybeComponent) : Component => bind(Component) | ||
} | ||
|
||
function usingDispatchBuffer(buffer, Component) { | ||
return React.createClass({ | ||
childContextTypes: { | ||
buffer: React.PropTypes.object.isRequired | ||
}, | ||
|
||
getChildContext() { | ||
return { buffer } | ||
}, | ||
|
||
render() { | ||
return React.createElement(Component, this.props) | ||
} | ||
}) | ||
} | ||
|
||
class DispatchBuffer { | ||
constructor(renderStrategy) { | ||
this.promisesBuffer = [] | ||
this.locked = false | ||
this.renderStrategy = renderStrategy | ||
} | ||
|
||
push(v) { | ||
this.promisesBuffer.push(v) | ||
} | ||
|
||
fill(Element) { | ||
return this.renderStrategy(Element) | ||
} | ||
|
||
clear() { | ||
this.promisesBuffer = [] | ||
} | ||
|
||
flush(Element) { | ||
return Promise.all(this.promisesBuffer).then((data) => { | ||
// fire off all the actions synchronously | ||
data.forEach((f) => { | ||
if (Array.isArray(f)) { | ||
f.forEach(x => x()) | ||
} else { | ||
f() | ||
} | ||
}) | ||
this.locked = true | ||
|
||
return this.renderStrategy(Element) | ||
}).catch(() => { | ||
// if there's an error still render the markup with what we've got. | ||
return this.renderStrategy(Element) | ||
}) | ||
} | ||
} | ||
|
||
|
||
function renderWithStrategy(strategy) { | ||
return (Component, props) => { | ||
// create a buffer and use context to pass it through to the components | ||
const buffer = new DispatchBuffer((Node) => { | ||
return React[strategy](Node) | ||
}) | ||
const Container = usingDispatchBuffer(buffer, Component) | ||
|
||
// cache the element | ||
const Element = React.createElement(Container, props) | ||
|
||
// render so we kick things off and get the props | ||
buffer.fill(Element) | ||
|
||
// flush out the results in the buffer synchronously setting the store | ||
// state and returning the markup | ||
return buffer.flush(Element) | ||
} | ||
} | ||
|
||
export function toDOM(Component, props, documentNode) { | ||
const buffer = new DispatchBuffer() | ||
buffer.locked = true | ||
const Node = usingDispatchBuffer(buffer, Component) | ||
const Element = React.createElement(Node, props) | ||
buffer.clear() | ||
return React.render(Element, documentNode) | ||
} | ||
|
||
export const toStaticMarkup = renderWithStrategy('renderToStaticMarkup') | ||
export const toString = renderWithStrategy('renderToString') |
Oops, something went wrong.