Response Caching
The below examples uses Vercel's edge caching to serve data to your users as fast as possible.
⚠️ A word of caution ⚠️
Always be careful with caching - especially if you handle personal information.
Since batching is enabled by default, it's recommended to set your cache headers in the responseMeta function and make sure that there are not any concurrent calls that may include personal data - or to omit cache headers completely if there is an auth headers or cookie.
You can also use a splitLink to split your requests that are public and those that should be private and uncached.
App Caching
If you turn on SSR in your app you might discover that your app loads slow on for instance Vercel, but you can actually statically render your whole app without using SSG; read this Twitter thread for more insights.
Example code
pages/_app.tsxtsx
pages/_app.tsxtsx
API Response caching
Since all queries are normal HTTP GETs we can use normal HTTP headers to cache responses, make the responses snappy, give your database a rest, and easier scale your API to gazillions of users.
Using responseMeta to cache responses
Assuming you're deploying your API somewhere that can handle stale-while-revalidate cache headers like Vercel.
server.tstsx
server.tstsx