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.

Async redux thunk in SERVER breaks

See original GitHub issue

Examples bug report

Example name

with-redux-thunk

Describe the bug

Async thunk does not work in SSR

To Reproduce

The example repo: with-redux-thunk

In the above example, serverRenderClock is synchronous

export const serverRenderClock = isServer => dispatch => {
  return dispatch({ type: actionTypes.TICK, light: !isServer, ts: Date.now() })
}

everything work expected, on SSR, the example will render the time on the server

However, if we try to make serverRenderClock async, it breaks using below async serverRenderClock

export const serverRenderClock= usServer = dispatch => {
  return setInterval(() => {
    dispatch({ type: actionTypes.TICK, light: !isServer, ts: Date.now() })
  }, 1000)
}

The new getInitialProps

static async getInitialProps ({ reduxStore, req }) {
    const isServer = !!req
    await reduxStore.dispatch(serverRenderClock(isServer))
    return {}
  }

Expected behavior

With the new async dispatch, it should also render the server time as synchronous

Additional context

Above is an generalized example. However, let’s say if we want to dispatch an async thunk with API call in server, the scenario is similar For example, making async api call in thunk in the server side

getResultFromApi = () => async dispatch => {
   const response = await fetch('http://www.test-api.com')
   const data = await response.json()
   dispatch(setSuccessResponseAction(data)
}

How can I use above async thunk in my getInitialProps so SSR works properly?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:13 (11 by maintainers)

github_iconTop GitHub Comments

4reactions
nataliyakaratcommented, Jun 7, 2019

@MarchWorks yes, you’re right. I’ve found that getInitialProps in index.js is not async https://github.com/zeit/next.js/blob/0dbd3b98eca0bee01a05b8414a60c48abba76abd/examples/with-redux-thunk/pages/index.js#L7

it has to be this way:

static async getInitialProps({ reduxStore, req }) {
    const isServer = !!req;
    await reduxStore.dispatch(serverRenderClock(isServer));

    return {};
  }
1reaction
nblthreecommented, Jun 6, 2019

@meuwka check it here https://codesandbox.io/s/helloworld-37ycq click on Open in new window (not necessary just to see the delay more clearly.) it will take 10 seconds before the page render

Read more comments on GitHub >

github_iconTop Results From Across the Web

Async redux thunk in SERVER breaks · Issue #7503 - GitHub
Examples bug report Example name with-redux-thunk Describe the bug Async thunk does not work in SSR To Reproduce The example repo: ...
Read more >
Async actions in bare Redux with Thunk or custom middleware
This new action is inserted in the middleware pipeline via next() , sends the HTTP request to the server, and waits for a...
Read more >
How to handle server/connection exceptions with React and ...
I'm using Redux thunk to dispatch asynchronous actions like fetching and posting to server. Each action creator call returns a promise, ...
Read more >
Redux Fundamentals, Part 6: Async Logic and Data Fetching
Writing async logic as thunk functions allows us to reuse that logic without knowing what Redux store we're using ahead of time.
Read more >
Using redux-observable to handle asynchronous logic in Redux
These functions are called thunks, and they will be called by the middleware, which will pass dispatch on the 1st parameter and getState...
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