It allows you to do async actions before rendering the view.
Last updated 3 years ago by isogon .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @isogon/prefetch 
SYNC missed versions from official npm registry.

Prefetch for React Router and Redux

npm version Build Status

Decorator and provider for handling async actions before route rendering in React.

yarn install prefetch


Prefetch is inspired by redux-connect


Prefetch is designed to work in combination with redux and react-router


import { Prefetcher }  from 'redux-prefetcher';

All you have to do is render Router with Prefetcher middleware

  <Provider store={store}>
      render={(props) => <Prefetcher {...props}/>}
      <Route path="/" component={App}/>
), el)

Prefetcher will watch for route changes, when they happen it will look for components used in that route that are wrapped with prefetch. If it finds any, it will block the display of those components until their prefetch actions are complete.


Using redux-promise

Create an action and prefetch will dispatch them for you. Prefetch will dispatch whatever you return. Either your promise middleware or your Thunk needs to finally return a promise.

redux-promise returns promises by default so it works well with prefetch.

import { connect } from 'react-redux';
import { prefetch } from 'redux-prefetcher';

@prefetch(() => ({
  type: 'GET_LUNCH',
  payload: Promise.resolve({
    name: 'Fish Tacos',
    from: 'The Fish Taco Store'
@connect((state) => ({
  lunch: state.lunch
function App({ lunch }) {
  return (
    <h4>Lunch Time!</h4>
    <div>Looks like you are having {lunch.name} from {lunch.from}</div>

If you want to use redux-thunk, make sure to return a reference to your promise so that prefetch knows when your thunk is complete. That could look something like this.


@prefetch(() => (dispatch) => Promise.resolve(dispatch({
  type: 'GET_LUNCH',
  payload: {
    name: 'Fish Tacos',
    from: 'The Fish Taco Store'


Server Side rendering

Ideally you want to do all this asynchronous stuff on the server and preload all that data into state. There are only three things you need to do to get this to work.

  1. Use the prefetchData helper method in your server code, It expects an object containing all renderProps and your redux store.
  2. Use Prefetcher instead of RoutingContext and pass it renderProps
  3. Set the prefetchedOnServer flag on both the client and server.
import { renderToString } from 'react-dom/server'
import { match, RoutingContext } from 'react-router'
import { Prefetcher, prefetchData } from 'redux-prefetcher';
import { Provider } from 'react-redux';
import { createStore, combineReducers } from 'redux';
import serialize from 'serialize-javascript';

import lunchReducer from './lunchReducer';

app.get('*', (req, res) => {
  const store = createStore(combineReducers({ lunch: lunchReducer }));

  match({ routes, location: req.url }, (err, redirect, renderProps) => {

    // 1. load data
    prefetchData({ ...renderProps, store }).then(() => {

      // 2. use `Prefetcher` instead of `RoutingContext` and pass it `renderProps`
      const appHTML = renderToString(
        <Provider store={store} key="provider">
          <Prefetcher {...renderProps} prefetchedOnServer />

      // 3. render the Redux initial data into the server markup
      const html = createPage(appHTML, store)

function createPage(html, store) {
  return `
    <!doctype html>
        <div id="app">${html}</div>

        <!-- its a Redux initial data -->
        <script dangerouslySetInnerHTML={{__html: `window.__data=${serialize(store.getState())};`}} charSet="UTF-8"/>

// on the client
const component = (
  <Provider store={store}>
      render={(props) => <Prefetcher {...props} prefetchedOnServer />}



Usage with applyRouterMiddleware

// on the client
const render = applyRouterMiddleware(useScroll());
  render={(props) => <Prefetcher {...props} {render} />}

Basically what you do is instead of using render method like:

const render = props => <RouterContext {...props} />;

you use

const render = applyRouterMiddleware(...middleware);


You're welcome to PR, and we appreciate any questions or issues, please open an issue!

Current Tags

  • 0.1.1                                ...           latest (3 years ago)

2 Versions

  • 0.1.1                                ...           3 years ago
  • 0.1.0                                ...           3 years ago
Today 0
This Week 2
This Month 2
Last Day 0
Last Week 0
Last Month 1
Dependencies (12)
Dependents (0)

Copyright 2014 - 2017 © taobao.org |