Created
March 28, 2019 16:42
-
-
Save gregberge/e4b79c04a80a4a056f24488095b53b94 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useContext } from 'react' | |
import ReactDOM from 'react-dom/server' | |
import ssrPrepass from 'react-ssr-prepass' | |
const Context = React.createContext() | |
const asyncFoo = async () => 'foo' | |
function MyComponent() { | |
const tracker = useContext(Context) | |
if (!tracker.resolved) { | |
throw asyncFoo().then(result => { | |
tracker.resolved = result | |
}) | |
} | |
return tracker.resolved | |
} | |
async function main() { | |
const tracker = {} | |
const app = ( | |
<Context.Provider value={tracker}> | |
<MyComponent /> | |
</Context.Provider> | |
) | |
await ssrPrepass(app) | |
console.log(ReactDOM.renderToString(app)) | |
} | |
main().catch(error => | |
setTimeout(() => { | |
throw error | |
}), | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment