Next.js Discord

Discord Forum

Caching with Firebase

Unanswered
Sun bear posted this in #help-forum
Open in Discord
Sun bearOP
Hey there! I have a general question about caching. I am trying to reduce my firestore reads and wanted to tackle it using the various caching options that NextJS offers.

Since I am using firestore and hosting API routes in my NextJS project, I cannot use the fetch( , {next: { revalidate }}) approach.
Instead I have opted for wrapping my firestore get functions in cache( async (id) => ... )

From what I gather, the way to invalidate these caches is via the export const revalidate = 60 // cache for 60 seconds declarations in e.g. my page.tsx files.
I have a couple problems here:
I am finding it hard to "see" if this is working or not. The logs show cache HIT/MISS when using the fetch approach, but the cache() approach does not seem to produce the same kinds of logs. Is this normal?
I would like to set revalidation time limits on each function separately. It would be great if cache took a second parameter along the lines of cache(fn, revalidateAfter) but I can't find good docs on how React's cache really works.
I would also like to force revalidation on POST/PUT/PATCH/DELETE activity. I understand that NextJS has the revalidateTag method but it doesn't seem to work for cache? Again, it would be so cool if cache could be extended to perhaps have something like cache(fn, { tags, revalidateAfter } ).

Lastly, I was wondering what the best practises are for caching for collaborative spaces, where POST/PUT activity can come from another user? For instance if you view a user's profile page and they may update it, is it best to set a long cache revalidation time or just in case, set it short?

1 Reply

Sun bearOP
Would love to get some guidance on this. I'd love a cache equivalent of fetch(, { next ... }