pages/stuff.ts
export function getServerSideProps(context) {
const userId = getUserIdOrNull(context);
return {props: { somethingComplicated: getSomethingComplicated(!!userId) }};
}
export default Home({somethingComplicated}) {
return <div>{somethingComplicated}</div>
}
How do you cache this page?!
Ideally I'd like to cache the page <Home {...{somethingComplicated}}/> based on the different contexts.
Possible but unsatisfying solutions:
Cache the calculation of
somethingComplicated. Unsatisfying because Home would need to be recalculated every time and cannot be cached by cloudflare.Load somethingComplicated dynamically on the client from an api. Unsatisfying because the lack of server side rendering is a worse user experience.
Route for logged in users and logged out users to different routes. Unsatisfying because user visible urls will be different and the redirects get complicated.
I can't seem to find a satisfying solution. Maybe it's not possible? Anyways, thanks in advance for your time.
you can read more
If you are using
getServerSidePropsthat means every time a user visits that page, you want to fetch fresh data and serve it to the user. If your serverside is doing heavy calculation or hitting database disk you could implement caching layer between application server and db or other servers. You could use redis