question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Code splitting / dynamically loading sagas

See original GitHub issue

I just discovered redux-saga and I am looking to start integrating this into my app. I am currently code splitting my reducers and using replaceReducer when they hit the correct route. From the saga examples it looks like they all use a rootSaga. The closest I found to an answer is https://github.com/yelouafi/redux-saga/issues/23#issuecomment-168762879 but I am not certain if this is the official API and usage.

My current setup looks something like this: http://stackoverflow.com/a/33045558 which I pasted a snippet below:

function createRoutes(reducerRegistry) {
  return <Route path="/" component={App}>
    <Route path="async" getComponent={(location, cb) => {
      require.ensure([], require => {
        reducerRegistry.register({async: require('./reducers/async')})
        cb(null, require('./screens/Async'))
      })
    }}/>
  </Route>
}

function createStore(reducerRegistry) {
  var rootReducer = createReducer(reducerRegistry.getReducers())
  var store = createStore(rootReducer)

  reducerRegistry.setChangeListener((reducers) => {
    store.replaceReducer(createReducer(reducers))
  })

  return store
}

From the other comment, it sounds like this should be possible, but it looks like sagas need access to the store?

    <Route path="async" getComponent={(location, cb) => {
      require.ensure([], require => {
        reducerRegistry.register({async: require('./reducers/async').default})
        const sagas = require('./sagas').default
        // store?
        runSaga(sagas) 
        // right api usage? where to get the store? does any array work?
        sagas.forEach(saga => runSaga( saga(store.getState), store )) 
        // only supports one rootSaga, not an array?
        runSaga( sagas(store.getState), store )
        cb(null, require('./screens/Async'))
      })
    }}/>

It looks like a middleware approach was discussed in https://github.com/yelouafi/redux-saga/issues/13#issuecomment-168884656 that would have solved this, but it was closed. This could have potentially worked:

  var store = createStore(rootReducer)

  reducerRegistry.setChangeListener(reducers => {
    store.replaceReducer(createReducer(reducers))
  })

  sagaRegistry.setChangeListener(iterator => {
    store.runSaga(iterator)
  })

Would this be the official usage? What about “stopping/removing/disconnecting” the sagas? Will this work with server side rendering?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:25 (11 by maintainers)

github_iconTop GitHub Comments

8reactions
GuillaumeCiscocommented, Feb 15, 2019

Hello there,

I had the same issue and found redux-injector for injecting reducers asynchronously via routing. I decided to create the same philosophy for redux-sagas : https://github.com/GuillaumeCisco/redux-sagas-injector

You can find an exemple in the README. Using injectReducer and injectSaga, you can easily load your js code asynchronously via routing and deciding what route need only what it needs.

Hope you’ll enjoy it 🎉

PS: It also works with SSR Stream Caching and with react-redux >= 6.0.0

2reactions
mjrussellcommented, Dec 2, 2016

@johanneslumpe AFAIK you cannot hot-reload a saga. Changes to sagas during hot reloading should force an entire app refresh. What I do is pass in a “key” of the saga to a registry which calls sagaMiddleware.run. And I only run a saga when I register a saga with a key that is different from any key I’ve registered before. This is because the registration exists inside the getComponent call in a React Router <Route> which is invoked on every route enter/change

Read more comments on GitHub >

github_iconTop Results From Across the Web

Code Splitting for redux and redux-saga
The most common strategy for choosing what to load dynamically (via import() ) and what statically is route-based code splitting.
Read more >
Code Splitting
This strategy, called 'code splitting', helps to increase performance of your application by reducing the size of the initial JS payload that ...
Read more >
Redux store code splitting with sagas
First off we need to make some changes to store creation so that: We can specify async sagas/reducers to load at initialization if...
Read more >
Redux/Saga Code Splitting-[ Part 2 ]
Route based code splitting means creating different chunk for each page. e.g. If user is visiting home page web browser will only request ......
Read more >
Dynamically load sagas for code splitting in redux application
I am trying to integrate redux-saga into my react app. I am currently code splitting reducers by dynamically injecting it using ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found