Caching when using Firebase Admin SDK #52267
-
SummaryHey 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. (I did think about using internal API calls but this does not seem to be advised).
From what I gather, the way to invalidate these caches is via the I have a couple problems here:
Thanks. I've been Googling/ChatGPT'ing around with no luck. I also tried the NextJS Discord but did not get far. Additional informationNo response ExampleNo response |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 5 replies
-
Hey, did you ever get answers to this? I'm searching for the same thing. |
Beta Was this translation helpful? Give feedback.
-
having the same issue |
Beta Was this translation helpful? Give feedback.
-
I believe this is what you are looking for: https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#example Replace |
Beta Was this translation helpful? Give feedback.
-
@HStromfelt import { unstable_cache as cache } from "next/cache";
const getUserDetailsByUsernameImpl = async (username: string): Promise<{ userDetails: any; message?: string }> => {
const q = DB.collection('users').where('username', '==', username)
const querySnapshot = await q.get()
if (querySnapshot.empty) {
return { userDetails: null, message: 'User not found' }
}
const userDetails = querySnapshot.docs[0].data()
return { userDetails }
}
const getUserDetailsByUsername = cache(
/* fetch function */ getUserDetailsByUsernameImpl,
/* unique key */ ["getUserDetailsByUsername"],
/* options */ {
tags: ["getUserDetailsByUsername"],
revalidate: 60 * 60 * 24 /* same as fetch.revalidate */
}
) Functioning similar to caching in the fetch API, it allows setting a revalidate option to define when cached data should become stale or invoking revalidateTag("getUserDetailsByUsername") to instantly invalidate the cache. It accepts 3 parameters:
|
Beta Was this translation helpful? Give feedback.
@HStromfelt
Next.js offers an experiential cache function designed to write to the Next.js Data Cache, functioning similarly to the extended fetch API.