I have a Next.js app deployed to GCP Cloud Run as a serverless website.
Its home page is a server component of a list of "Cards". It fetches data of the "Cards" from Firestore and renders it to the page like this:
// src/app/page.tsx
export const revalidate = 900;
const getRecentCards = cache(async (): Promise<Card[]> => {
try {
// fetch cards from Firestore
// return a list of cards
} catch (error) {
return [];
}
});
export default async function HomePage() {
const cards = await getRecentCards();
// display the cards
return (...{cards.map(card => {<div>...</div>} )}...)
Since the cards are generated by infrequent schduled cronjob so I set the revaldidate
to 900 seconds (15 mins).
And I deploy the app to Cloud Run by running gcloud run deploy --source .
.
I am expecting the behavior to be something like this:
However I am observing different behavior for user#4:
This will happen all the time in future, let's say now we have Cards 1-10 in the Firestore, whenever there is a cold start, user will receive (1, 2, 3) then wait for a while then refresh to get the whole list of cards.
It feels like there is a "build-time cache" freezed in the "cold start image". My question is how do I disable this "build-time cache". Or if I misunderstood anything. Thnk you.
During a cold start, Cloud Run loads the last built image which seems to include the built time cache. You can disable caching in your Next.js app so that it always fetches the latest data. For example, you can set the cache
option to no-store
in a fetch
request. However, this might lead to higher costs since Firestore charges for every read operation.