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

API for returning extra items returned by render server #87

Merged
merged 3 commits into from
Jul 17, 2018
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ The object returned has two properties:

- `markup` - the rendered markup
- `props` - the JSON-serialized props
- `data` - extra data returned by the render server

If the object is coerced to a string, it will emit the value of the `markup` attribute.

Expand All @@ -82,6 +83,8 @@ for a simple server that will cover most cases. The key files for the render ser
- [render_server.js](examples/basic_rendering/render_server.js) - the server's source code
- [package.json](examples/basic_rendering/package.json) - the server's dependencies, installable with
[npm](http://npmjs.com)

You can also return extra data from your render server and these data will be stored in `data` attribute of the response object.


Using React on the front-end
Expand Down
10 changes: 6 additions & 4 deletions react/render_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@


class RenderedComponent(object):
def __init__(self, markup, props):
def __init__(self, markup, props, data):
self.markup = markup
self.props = props
self.data = data

def __str__(self):
return self.markup
Expand Down Expand Up @@ -67,8 +68,9 @@ def render(self, path, props=None, to_static_markup=False, request_headers=None,

obj = res.json()

markup = obj.get('markup', None)
err = obj.get('error', None)
markup = obj.pop('markup', None)
err = obj.pop('error', None)
data = obj

if err:
if 'message' in err and 'stack' in err:
Expand All @@ -80,7 +82,7 @@ def render(self, path, props=None, to_static_markup=False, request_headers=None,
if markup is None:
raise ReactRenderingError('Render server failed to return markup. Returned: {}'.format(obj))

return RenderedComponent(markup, serialized_props)
return RenderedComponent(markup, serialized_props, data)


render_server = RenderServer()