-
Notifications
You must be signed in to change notification settings - Fork 41
Cache support #105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
If you plan on reusing the aws cache handler as it is i can give some advice. S3 storageFor the S3 storage ( the incremental cache and the fetch cache ) it should be the easiest and the obvious choice seems to be KV. QueueThe role of the queue is for ISR revalidation, when a page is marked as stale, the queue component will be called. For the easy one, you don't need any other component you just do exactly the same thing as in the run locally example for the For the more complex solution, i'm not sure, but it seems that durable object could be a way to go Tag cache (DynamoDB)It is only used for Really not sure about this one, it is heavily geared toward DynamoDB to take full advantage of it. I also have plan to offer an alternative way to query for the tag cache (that is closer to the next implementation in the default handler), but that means having to query for a bunch of tags for every get request These 3 are also used by the experimental cache interceptor that avoid having to reach the server to serve ISR/SSG route. |
Thank you so much for the super valuable info on those github issues/tasks. |
I suspect that the easiest way to go about this is to use KV for storage and serving. In terms of latency, it's on par with the Workers Cache API, but unlike the Cache API, KV is globally distributed. Queue can be used to dedupe revalidation requests so that we don't overwhelm the DB. DO is likely not a great fit here as it could get overwhelmed by the thundering herd when the cache is empty. One more thing to figure out is the tag based invalidation. We might need some kind of tag to paths mapping to support that. There is one more thing that I don't see mentioned here at all. This issue is about serving cache, but Next also has a separate concept of a data cache, which is used to cache data requests. @dario-piotrowicz and @james-elicx spent built out a solution for that in next-on-pages: https://github.com/cloudflare/next-on-pages/blob/main/packages/next-on-pages/docs/caching.md Is this cache already supported or do we need to create a separate tracking issue for it? |
The Next fetch cache shares the same infra - there are different value types in a common store. |
Can the worker cache api be also supported too? KV is great but I would use worker cache if I don't need global distribution since it costs less. |
yes, ideally the implementation is replaceable. we could even follow the precedent in next-on-pages and provide workers cache implementation by default, and offer kv+queue as a more scalable alternative as an opt-in. |
About the cache api, there might be some issues with
OpenNext already provides that, but for it to work properly, the tag cache needs to be initialized at build time. |
The seed files don't necessarily have to be put into the cache, they could be stored as part of the deployment instead and be served from there internally - similar to what is currently done in the main branch (it was removed from experimental branch). Then it would be a bit more friendly for cache stores that aren't as easy to seed. Unsure how that would work with what the aws adaptor has as I haven't really looked at it that much since Victor is leading that. |
So if the file are not there in the cache, it relies on the seed files ? I guess what would make more sense would be to separate the routing/middleware in a different worker and rely on the cache control that is set by next server for the cache api (effectively shielding most of the request with the cache api) |
It tries to serve from the cache, but if there is no file in the cache, it would serve the seed file from the deployment (which would be correct if a page hasn't been revalidated before). Works in the same way, just falls back to a different location if you're not able to seed the cache. |
Note: But I think that it just after caching is supported (based on a quick glance of its source code) |
Big thanks to conico's comment above - super helpful in figuring out where to get started with this. I've put up two PRs to enable ISR and revalidation: And a docs PR - opennextjs/docs#66 We'll probably need additional tickets for the following as future pieces of work as I'm not considering them as part of this one:
|
The cache api alone cannot work for the incremental cache or the tag cache. At least not without a big warning that it may break or not work as expected. And On Demand revalidation would require purging the cache for these values. This could help, but i'm not even sure if it's doable globally while using the cache api from workers |
There's nothing necessarily wrong with it being regional - Vercel's data cache for data fetching is regional for what it's worth. Just as long as it's communicated to users. From what I recall, I don't think anyone ever ran into issues using the cache api in next-on-pages. |
The issue is not really with it being regional, but rather that the same call from different location may return different data. Correct me if i'm wrong, but next-on-pages doesn't use Next incremental cache for SSG/ISR route, only for the fetch cache right ? With a big warning explaining this i think it's fine, but it should be made clear that this is a possibility and that it may not work as expected |
You're right, Vercel cache is regional, BUT revalidation is global. We already discussed this same topic for next-on-pages here. The important part is in the Vercel docs (emphasis by me):
Having regional cache AND regional revalidation makes it impossible to forcibly reset a cache. |
Yeah it's a fair point and a big warning explaining it like conico mentioned makes sense. It's better giving people the option instead of having no free option IMO, but if others disagree we don't have to do it. |
Technically it would be possible to make it work like on vercel with everything we got in place in OpenNext, but it kinds of defeat the purpose of a free/cheap option :
|
Was the option to do fetch or ISR revalidation using Just a thought, feel free to toss it out if it doesn't match the vision here. |
Cache support.
This is a big chunk of work that should be split into multiple sub-tasks when we start the work.
References:
AWS uses multiple components for the cache:
This task is about providing a cache (incremental + tag) on the cloudflare infrastructure.
We have started to explore multiple possibles solutions using:
TODO:
Support for Cache Invalidation by Tag (#91)
--
Second thing we should tackle with this issue is errors when running i.e. the middleware example on the experimental branch with browser cache enabled.fixed in opennextjs/opennextjs-aws#683--
Edit the main branch has basic cache support (kv + assets).
It's missing revalidation
The text was updated successfully, but these errors were encountered: