Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Saga Support #43

Closed
emilyIser opened this issue May 11, 2017 · 9 comments
Closed

Saga Support #43

emilyIser opened this issue May 11, 2017 · 9 comments

Comments

@emilyIser
Copy link

Your platform has been working very nicely! I appreciate all the thought put into it.

One thing I'd love to see is Saga support. Sagas tend to be a lot easier to manage then the more 'thunk' like pattern (https://github.com/redux-saga/redux-saga), especially when you want to chain events.

I got very close to getting them working with the platform (thanks to all the middleware hooks etc). But there is a missing piece that revolves around saga's working server side: redux-saga/redux-saga#13.
Saga's Resolution: redux-saga/redux-saga#255

Essentially server side rendering and Sagas have some challenges, but they have a special 'END' event that needs to be triggered at specific point in the code.

More specifically

  1. Run Saga Middleware (Already a hook for this)
  2. renderToString happens
  3. Call dispatch(end)

Example code from the saga issue 255 linked above:

       const rootTask = sagaMiddleware.run(rootSaga)

      // this will cause the universal Saga tasks to trigger
      renderToString(
         <Root store={store} renderProps={renderProps} type="server"/>
      )
      // notify Saga that there will be no more dispatches
      // this will break the while loop of watchers
      store.dispatch(END)

Another challenge that I had mostly worked around was the fact that Saga's don't return promises by default which are required by the Preload decorators. But I created a helper file that can take care of that because when you call this.sagaMiddleware.run(common) it returns the associated promise, which I then save and can be returned in the 'preload' methods. (Tho there might be some trickery I hadn't gotten to around preload running client side?)

In summary: Saga support would be awesome :)

@catamphetamine
Copy link
Owner

I can expose any extra hooks this redux-saga library needs.
For that you'll need to tell me exactly which hooks are missing for you and at what point in time should they be exposed.
I'm not familiar in any way with that library.

@emilyIser
Copy link
Author

I'm trying to figure out exactly where in the react-isormorphic-render code this should live.

I think here?
https://github.com/halt-hammerzeit/react-isomorphic-render/blob/master/source/page-server/render.js#L194

It is essentially after all the preload methods have been triggered you have to run the store.dispatch(end) so that the saga promises will close out and allow the preloads to wrap up and the content to render.

@catamphetamine
Copy link
Owner

Is the only missing thing is running store.dispatch(end) in the end of server-side rendering?
If store.dispatch(end) is run on client side won't it break anything.

@catamphetamine
Copy link
Owner

As for the place I'd suggest adding a hook here:
https://github.com/halt-hammerzeit/react-isomorphic-render/blob/master/source/redux/server/render.js#L62

It's after all preloads but before ReactDOM.renderToString().
Should it await store.dispatch(end)?

@emilyIser
Copy link
Author

That should work!

  • We want to only END them server side
  • We want to END after all the preloads are executed

The best would be to have a hook there that provided the store.

Thanks :)

@catamphetamine
Copy link
Owner

catamphetamine commented May 15, 2017

Try the newest version and beforeRender({ dispatch }) hook in rendering server settings

beforeRender({ dispatch }) { return dispatch(END) }

(I didn't test it)

@emilyIser
Copy link
Author

Awesome thank you! I will give it a go :)

@chris-allen
Copy link

How is saga integration possible now that beforeRender has been removed? I can't seem to find a place step in after reduxMiddleware to run sagas.

@catamphetamine
Copy link
Owner

@chris-allen
The old beforeRender function was called only when rendering server-side after preloads have finished.
https://github.com/catamphetamine/react-website/blob/0e11dbaac18a177bfbd39b3541d5c4ab42922914/source/redux/server/render.js#L42-L50
What is your feature request?
I don't know what "saga"s are and I didn't work with them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants