Skip to content

Instantly share code, notes, and snippets.

@jlei523
Last active January 31, 2020 12:00
Show Gist options
  • Save jlei523/206770fbc0178fbb75e2b2cae6668fa2 to your computer and use it in GitHub Desktop.
Save jlei523/206770fbc0178fbb75e2b2cae6668fa2 to your computer and use it in GitHub Desktop.
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