Skip to content

Commit

Permalink
feat: move logic of "ensure async resolution inside of QueryRenderer"…
Browse files Browse the repository at this point in the history
… to server.getCache() method
  • Loading branch information
nodkz committed Jun 18, 2018
1 parent 30abe88 commit 3c17880
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
16 changes: 6 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

**For a full examples, see**:

* https://github.com/damassi/react-relay-network-modern-ssr-example
* https://github.com/damassi/react-relay-network-modern-ssr-todomvc
- https://github.com/damassi/react-relay-network-modern-ssr-example
- https://github.com/damassi/react-relay-network-modern-ssr-todomvc

# Server

Expand Down Expand Up @@ -51,17 +51,13 @@ const network = new RelayNetworkLayer([
// First, kick off Relay requests with an initial render
ReactDOMServer.renderToString(<App />);

// Second, collect the data
// Second, await while all data were recieved from graphql server
const relayData = await relayServerSSR.getCache();

// Third, render the app a second time now that the Relay store has been primed
// and send HTML and bootstrap data to the client for rehydration. (setTimeout is
// used to ensure that async resolution inside of QueryRenderer completes once
// data is provided.
setTimeout(() => {
const appHtml = ReactDOMServer.renderToString(<App />);
sendHtml(appHtml, relayData);
}, 0)
// and send HTML and bootstrap data to the client for rehydration.
const appHtml = ReactDOMServer.renderToString(<App />);
sendHtml(appHtml, relayData);
```

# Client
Expand Down
13 changes: 13 additions & 0 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,26 @@ export default class RelayServerSSR {
}

async getCache(): Promise<SSRCache> {
this.log('Call getCache() method');

// ensure that async resolution in Routes completes
await new Promise(resolve => {
setTimeout(resolve, 0);
});

const arr = [];
const keys = Array.from(this.cache.keys());
for (let i = 0; i < keys.length; i++) {
// eslint-disable-next-line no-await-in-loop
const payload: any = await this.cache.get(keys[i]);
arr.push([keys[i], payload]);
}

// ensure that async resolution inside of QueryRenderer completes
await new Promise(resolve => {
setTimeout(resolve, 0);
});

this.log('Recieved all payloads', arr.length);
return arr;
}
Expand Down

0 comments on commit 3c17880

Please sign in to comment.