Last active
January 31, 2020 12:00
-
-
Save jlei523/206770fbc0178fbb75e2b2cae6668fa2 to your computer and use it in GitHub Desktop.
This file contains 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
let cache = {}; | |
class App extends React.Component { | |
static async getInitialProps({ req, query, asPath, pathname }) { | |
let data; | |
//if data is in cache then use the cache | |
if (cache[someID]) { | |
data = cache[someID] | |
} else { | |
//if not, then fetch from server | |
data = await fetch(`someAPI`); | |
} | |
return { data } | |
} | |
render() { | |
//check if client, if so store the data in the cache. | |
//If you don't do this check, there will be a separate cache stored on the server since Next.js does server side rendering as well. | |
if (process.browser) { | |
cache[someID] = this.props.data; | |
} | |
return ( | |
<div></div> | |
) | |
} | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment