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

@isogon/prefetch

v0.1.1

Published

It allows you to do async actions before rendering the view.

Downloads

2

Readme

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

Notice

Prefetch is inspired by redux-connect

Usage

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

Setup

import { Prefetcher }  from 'redux-prefetcher';

All you have to do is render Router with Prefetcher middleware

render((
  <Provider store={store}>
    <Router
      render={(props) => <Prefetcher {...props}/>}
      history={browserHistory}
    >
      <Route path="/" component={App}/>
    </Router>
  </Provider>
), 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.

Example

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 />
        </Provider>
      )

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

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

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

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

API

Notes

Usage with applyRouterMiddleware

// on the client
const render = applyRouterMiddleware(useScroll());
<Router
  render={(props) => <Prefetcher {...props} {render} />}
  history={history}
  routes={getRoutes(store)}
/>

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

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

you use

const render = applyRouterMiddleware(...middleware);

Collaboration

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