Skip to content
This repository has been archived by the owner on Sep 12, 2020. It is now read-only.

Commit

Permalink
document rootSaga run condition
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoluc authored and bmealhouse committed Nov 13, 2019
1 parent 7784a7e commit 23ee4e9
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,10 @@ Check out the official [Next.js example](https://github.com/zeit/next.js/tree/ca
```js
import {createStore, applyMiddleware} from 'redux'
import createSagaMiddleware from 'redux-saga'
import {sagaStarted} from './actions'
import rootReducer from './root-reducer'
import rootSaga from './root-saga'

function configureStore(preloadedState) {
function configureStore(preloadedState, {isServer, req = null}) {

/**
* Recreate the stdChannel (saga middleware) with every context.
Expand All @@ -61,12 +60,14 @@ function configureStore(preloadedState) {
)

/**
* next-redux-saga depends on `sagaTask` being attached to the store.
* next-redux-saga depends on `sagaTask` being attached to the store during `getInitialProps`.
* It is used to await the rootSaga task before sending results to the client.
* However it should run only once - which must be regarded when using `next-redux-wrapper:^2.1.0`
* However, next-redux-wrapper creates two server-side stores per request:
* One before `getInitialProps` and one before SSR (see issue #62 for details).
* On the server side, we run rootSaga during `getInitialProps` only:
*/
if (!store.getState().saga.ran) {
store.dispatch(sagaStarted())

if (req || !isServer) {
store.sagaTask = sagaMiddleware.run(rootSaga)
}

Expand Down

0 comments on commit 23ee4e9

Please sign in to comment.