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

universal-redux-thunk

v2.0.0

Published

A replacement for Redux's built-in middleware store enhancer to build universal apps.

Downloads

7

Readme

Universal Redux Thunk Build Status

A Redux store enhancer taking care of promise resolution for building universal apps.

Background

Rendering a Redux application on the server requires you to give up on certain programming patterns. For once you can't use singletons on the server as they would be shared between multiple users. Another issue we encountered is resolving Promises trigger from actions within components. While many Redux boilerplates use custom routers to handle data fetching on the client & server we decided to approach it differently. Our goal was to surface a minimal API that can be plugged into any existing Redux application without adding limitations in the way people built their applications.

Setup

To install the stable version run:

npm install --save univeral-redux-thunk

How it Works

Universal Redux Thunk will catch any Promise returned by redux-thunk middleware. Calling renderUniversal returns a Promise which is fulfilled once all Promises are resolved. It is important to note that mutiple rendering loops will be run on the server depending on how your components are nested. For example if ComponentA has ComponentB as a child and ComponentB has ComponentC as a child, several loops will be run resolving promises in sequence on the way to the leaf child. Try not to deeply nest components with multiple slow running asynchronous actions on the way down.

This library works with a specific set of action types. This allows the library to filter actions in an attempt to detect infinte loops while rendering components, preventing excessive asynchronous actions (API calls, database queries, etc.). Before dispatching an asynchronous action the action.type should contain the word "LOADING", on a succesful response your action.type should contain "LOAD_SUCCESS" and on failure "LOAD_FAILED". This allows coverage for the three possible states of an asynchronous action.

If the same action containing "LOADING", "LOAD_SUCCESS" or "LOAD_FAILED" is detected, the rendering cycle will be aborted and an error thrown. Other action types will be ignored by the the library so you can freely dispatch as many other actions as required. All promises are tracked (promises should have corresponding actions for each of its states which can be tracked).

Guide with Redux-thunk

A small example application is provided at: https://github.com/tom-drake/universal-redux-thunk-example

Why Universal rendering?

####Faster Perceived Load Time

The network roundtrips to load all the resources take time. By already pre-rendering the first page impression the user experience can be improved. This becomes even more important in places with high internet latency.

####Search Engine Indexability

Many search engines only rely on server side rendering for indexing. Google have improved their search crawler to index client side rendered content but by rendering the page on the server you simply remove a potential point of failure in indexing.

####Code Reusability & Maintainability

Libraries can be shared between the backend & front-end.

Client side vs Universal rendering

Use case with client side rendering

  • (Client) Request the website's HTML
  • (Server) Serve the page without content
  • (Client) Request JavaScript code based on sources in the HTML
  • (Server) Serve the JavaScript code
  • (Client) Load & execute JavaScript
  • (Client) -> Render a loading page
  • (Client) Request data based on the executed code
  • (Server) Collect and serve the data
  • (Client) -> Render the content

Caching definitely helps to reduce the loading times and can be done easily for the HTML as well as for the code.

Use case with universal rendering

JavaScript code is already loaded when starting the server. From experience I saw this can take a couple hundred milliseconds.

  • (Client) Request the website's HTML
  • (Server) Execute the JavaScript Code
  • (Server) Collect the data
  • (Server) Render the page in the backend
  • (Server) Serve the page with content
  • (Client) -> Render the content
  • (Client) Request JavaScript code based on sources in the HTML
  • (Server) Serve the JavaScript code
  • (Client) Load & execute JavaScript

Pros & Cons

While with the initial site can be serve faster with client-side rendering there is no relvant content for the user. The network roundtrips increase the time until the user actually sees relevant content. While with the universal approach it takes a bit longer until the user receives the first page it already comes with the content and the total loading time is faster.

Initial Technical Requirements for univeral-redux-thunk

Given the user of redux-thunk it should just work. You may have to do some work to remove the infinite loop issue such as checking that a promise action has not already been dispatched, but this should be no more than a simple if check to check the loading/loaded state of your reducer leaf.

All tools must be ready to work with server-side rendering. Luckily that's the case in the React/Redux eco-sytem:

  • React
  • React-Router
  • Redux
  • Webpack or Browserify
  • Superagent

License

MIT