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-ssr-starter-kit

v5.0.0

Published

Starter kit for server side rendering with React and Redux

Downloads

1

Readme

React-ssr-starter-kit

CYPRESS

This package is meant to help with the creation of an isomorphic React + Redux application.

Overview

The package includes two main parts: a Client and a SSRMiddleware classes. You instantiate a SSRMiddleware object on your Node.js server and a Client object on the client.

The SSRMiddleware is reponsible to render the app content by fetching any required data (see Component requirements section).

The Client is responsible to boostrap and render the React client application.

Rendering and routing are managed isomorphically in fact you need to pass a common routing configuration to both client and server, see SSRMiddleware configuration and Client configuration for more details.

Install and usage

yarn add react-ssr-starter-kit

On the server:

import express from 'express';
import SSRMiddleware from 'react-ssr-starter-kit/SSRMiddleware';

 // see the "SSRMiddleware configuration" section
const config = { ... };
const ssrMiddleware = new SSRMiddleware(config);
const server = express();
server.use(ssrMiddleware.middleware);

this.server.get('*', async (req, res) => {
    const template = path.join(__dirname, './views/main.handlebars');
    res.type('text/html; charset=UTF-8');
    res.render(template, {
        state: JSON.stringify(res.state),
        content: res.content
    });
});


server.listen(8080);

On the client:

import Client from 'react-ssr-starter-kit/Client';

// see the "Client configuration" section
const config = { ... };

const client = new Client(config);
client.render(document.getElementById('root'));

SSRMiddleware configuration

The SSRMiddleware class constructor accepts a single configuration object parameter with the following properties:

#### initialState A function taking the request as parameter that create the initial redux state.

template

A path to an handlebars template that will be used to render the app content.

rootReducer

The already combined Redux reducers object.

routes

An array of objects with path and component properties.

inject ( default: null )

This is a thunk specific argument. It allow to inject extra argument into every action creator along with the dispatch and the getState arguments.

Client configuration

The Client class contructor accept a single configuration object with the following properties:

routes ( default: null)

This has to be the same object passed to server (see the server config)

rootReducer ( default: null )

This has to be the same object passed to server (see the server config)

initialState ( default: null )

This field deserve special attention because it's the point of contact between client and server data. The client has to fill this field with the state coming from the server. See the example app to more details.

inject ( default: null )

The Redux-Thunk injections.

middlewares ( default: [] )

An array of Redux middleware to be registered.

Component requirements

Components that requires initial data to be rendered can speify a static property requirements like in the following example:

class Planets {
  ...
}

Planets.requirements = [
    fetchPlanets,
    ['/planets/:id', (params) => fetchPlanetDetails(params.id) ]
  ]

requirements is an array and each item inside it can be a plain Redux Action like fetchPlanets or a bit more complicated expression like the second one in the example above. This expression means that the component Planet requires also the execution of the action fetchPlanetDetails if the routes match the /planets/:id pattern. As you can see you are able to access to route parameters.

How to contribute

All the main code is inside the /src folder.

The /example folder contains an example application so that you can easly experiment or add new functionality.

To make development more confortable the example app is already linked to the react-ssr-starter-kit package by file system (look at its package.json) so if you make some change to the react_ssr_kit, simply build it to make changes available to the example app.

Build the react-ssr-starter-kit package locally

cd react-ssr-starter-kit
yarn
yarn build

Launch the example app using the local built package

cd example
yarn
yarn dev