npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-render-service

v1.3.1

Published

HTTP microservice for rendering React to HTML on the server side

Downloads

648

Readme

React Render

Render React components on the server side in Django, also called "isomorphic React". You would do this for faster page loads, to make it friendlier to web crawlers and for SEO.

Example

from react_render.django.render import render_component

props = {
    'foo': 'bar',
    'woz': [1,2,3],
}

rendered = render_component('path/to/component.js', props=props)

print(rendered)

How it works

It works by having a NodeJS service on the same server that can render the React components. The Python just uses a simple HTTP API to send the context and the file path over to the service, and it responds with the rendered HTML.

Documentation

Installation

npm install react-render-service --save
pip install react-render-client

Optional: Point it to the service in your settings.py

REACT_SERVICE_URL = 'http://localhost:63578/render'

Start the node server which hosts the renderer.

react-service --debug

render_component()

Renders a component to its initial HTML.

Returns a RenderedComponent instance, which can be passed directly into templates to output the component's HTML.

Arguments:

  • path_to_source — a path to a JS file which exports the component. If the path is relative, django's static file finders will be used to find the file. NOTE Must be compiled JS, not JSX.
  • props optional — a dictonary that will be serialised to JSON and passed to the component during the renderering process.
  • to_static_markup optional — a boolean indicating that React's renderToStaticMarkup method should be used for the rendering. Defaults to False, which causes React's renderToString method to be used.
  • json_encoder optional — a class which is used to encode the JSON which is sent to the renderer. Defaults to django.core.serializers.json.DjangoJSONEncoder.

RenderedComponent

The result of rendering a component to its initial HTML. RenderedComponents can be passed directly into templates where they output the generated HTML.

# Render the component
my_component = render_component(...)

# Print the generated HTML
print(my_component)
<!-- Insert the generated HTML into your template -->
{{ my_component }}

RenderedComponents have a helper method, render_props, which outputs your JSON-serialized props. This allows you to reuse the encoded form of your props on the client-side.

<script>
    var myProps = {{ my_component.render_props }};
</script>

react-service settings

  • --host Bind to a different interface, by default this is localhost.
  • --port Bind to a different port. By default it is 63578
  • --watch Enable developer mode where it will automatically re-load the bundles if they change.
  • --whitelist Restrict the directory that the service can access. By default, the service will attempt to load any arbitrary path it is given. Can also be set via the REACT_WHITELIST environment variable.

Running the tests

mkvirtualenv react-render
pip install -e .
cd tests
npm install
npm run build
cd ..
python runtests.py