A Cache API that allows to cache anything, not just Fetch results #65889
mckravchyk
started this conversation in
Ideas
Replies: 1 comment
-
I realise it may pose a problem for the client-side. The client obviously must not be able to set cache objects. I am not familiar with Next.js internals though so I just wanted to suggest it. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Goals
Provide a basic mechanism to cache any data and invalidate it. Currently only data fetched over the network can be cached.
Non-Goals
No synchronisation for cache invalidation between fetch and custom data - the developer must invalidate those caches separately even if they are used together.
Background
The App router has a powerful caching mechanism, but it is only limited to the fetch API. Oftentimes there's a need to fetch data from third-party sources, which may require further local processing. This means that the data fetched over the network is cached, but processing needlessly happens with every request.
This is something that the Pages router does better, as it just caches the output of getStaticProps() (I know, this has been a limitation by itself).
I guess the only workaround would be to create a route that does the processing and fetch it. It is some extra overhead, not just runtime but a maintenance one.
Proposal
API to cache data
Cache
cache.get(key)
key
string - Unique cache keyReturns
Promise
- data or nullcache.set(key, data)
key
string - Unique cache keydata
unknown - data to cachecache.invalidate(key)
key
string - Unique cache keycache.invalidatePrefix(prefix)
prefix
string - Invalidate all cache keys that start with the prefixExample
Minimal (not viable)
This is something I have considered as a minimal fix, something that perhaps could even be achieved via a simple hack. However, it has the downside that there could be multiple requests with the same url and different
postProcess
.Beta Was this translation helpful? Give feedback.
All reactions