@isogon/prefetch
v0.1.1
Published
It allows you to do async actions before rendering the view.
Downloads
3
Readme
Prefetch for React Router and Redux
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.
- Use the
prefetchData
helper method in your server code, It expects an object containing allrenderProps
and yourredux
store. - Use Prefetcher instead of
RoutingContext
and pass itrenderProps
- 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!