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

redux-universal-render

v3.0.4

Published

Redux module for server side async render

Downloads

3

Readme

Redux Universal render

Build Status Coverage Status

Dependencies

redux-thunk

Why you need?

It is necessary because you must wait data is loaded before render page on server side.

Installation

To install the stable version with npm:

npm install --save redux-universal-render

Usage

This module supply for you reducer and render function to apply on server and client.

Example store config:

import { createStore, compose, applyMiddleware, combineReducers } from 'redux';
import { reducer as reduxUniversalRenderReducer } from 'redux-universal-render';
import thunk from 'redux-thunk';
import * as reducers from './reducers';

const initialState = {};

const store = createStore(
  combineReducers({ ...reducers, reduxUniversalRenderReducer }),
  initialState,
  compose(applyMiddleware(thunk))
);

Example server side rendering:

import { awaitRender } from 'redux-universal-render';

match({ routes, location: req.originalUrl }, (error, redirectLocation, renderProps) => {
    const render = <Provider store={store}>
      <RouterContext {...renderProps} />
    </Provider>;
    awaitRender({
      store,
      render,
      cb: () => {
        const content = renderToString(render);
        const page = renderToStaticMarkup(<HTML content={content} />);

        res.send(`<!doctype html>\n${page}`);
      }
    }
    );
  });
});

Example actions creators:

import { isParsed, addAction, parsedAction } from 'redux-universal-render';

export const syncActionCreator = () => ({ type: 'ACTION' });

const asyncName = 'asyncName';
export const asyncAction = () => (dispatch, getState) => {
  // not refetch data if it is already parsed for ssr and client render
  if (isParsed(asyncName)(getState())) return;

  dispatch(addAction(asyncName));
  setTimeout(() => {
    dispatch(syncActionCreator());
    dispatch(parsedAction(asyncName));
  }, 200);
};

actions

  • addAction: This action add to pending async actions with unique name.

Example:

import { addAction } from 'redux-universal-render';

const name = 'action';
export const action = () => (dispatch) => {
  dispatch(addAction(name));
}
  • parsedAction: This action set ended a specific async action by same name adedd with addAction.

Example:

import { addAction, parsedAction } from 'redux-universal-render';

const name = 'action';
export const action = () => async (dispatch) => {
  dispatch(addAction(name));
  // your async logic
  dispatch(parsedAction(name));
}
  • errorAction: This action set in error a specific async action by same name adedd with addAction.

Example:

import { addAction, errorAction } from 'redux-universal-render';

const name = 'action';
export const action = () => async (dispatch) => {
  dispatch(addAction(name));
  try {
    // your async logic
  } catch(e) {
    dispatch(errorAction(name, e));
  }
}

selectors

  • inExecution: return true if there is async actions in queue

  • isEnded: return true if there isn't async actions in queue

  • actions: return array of actions in queue

  • parsed: return array of actions already parsed

  • errors: return object with action name: error

  • isPending: return true if action name is added in queue

  • isParsed: return true if action name is already parsed

  • isError: return true if action name is in error

API

  • awaitRender({

    • store (required): Redux store
    • render component (required): React component to render
    • cb (required): Function, it is called when all async actions dispatched are ended
    • ownerRender(walk) (optional): Function, receive function that render component you must call it can you wrap with your own business logic

})

  • createActions(

    • name: string or function that receive getState and return unique name for actions
    • action: async action to await

)

Example:

import { createActions } from 'redux-universal-render';

createActions((getState) => `name_${getState().myVar}`, async (dispatch, getState) => {
  // ... my logic
});