I build a PWA
. On Server-Side-Rendering I want to wait for the responses to build and deliver the HTML-document
with data the API
returns. On client I do not have to wait for the responses, since the client is allowed to render a loading animation for example.
Therefore I want the following await to be conditional. On the Server is should like this:
await Promise.all([
store.dispatch(fetchUser()),
store.dispatch(fetchFriends()),
store.dispatch(fetchPosts())
])
On client the execution should look like this
Promise.all([
store.dispatch(fetchUser()),
store.dispatch(fetchFriends()),
store.dispatch(fetchPosts())
])
I am looking for a wrapper function that appends the await on the server. I do not want to have duplicated code at every occurrences like this:
if (isServer) {
await Promise.all([
store.dispatch(fetchUser()),
store.dispatch(fetchFriends()),
store.dispatch(fetchPosts())
]);
} else {
Promise.all([
store.dispatch(fetchUser()),
store.dispatch(fetchFriends()),
store.dispatch(fetchPosts())
])
}
Edit: I tried this but the await does not work on server side.
export default async function ssrAwait(promise): Promise<any> {
if (isServer) {
return await promise;
} else {
return promise;
}
}
ssrAwait(Promise.all([
store.dispatch(fetchUser()),
store.dispatch(fetchFriends()),
store.dispatch(fetchPosts())
]))
Any suggestions on this?