Async redux thunk in SERVER breaks
See original GitHub issueExamples 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:
- Created 4 years ago
- Comments:13 (11 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@MarchWorks yes, you’re right. I’ve found that
getInitialPropsinindex.jsis not async https://github.com/zeit/next.js/blob/0dbd3b98eca0bee01a05b8414a60c48abba76abd/examples/with-redux-thunk/pages/index.js#L7it has to be this way:
@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