Question presented to candidate: "With Cache Components on, how do you control how long a cached function lives and how do you invalidate it? Show me a custom profile."
What a strong answer should cover:
cacheLife(profile)inside a 'use cache' function or component sets three times:stale(client),revalidate(background refresh) andexpire(hard limit).- Built-in profiles:
default,seconds,minutes,hours,days,weeks,max; custom ones go incacheLifein next.config.ts. cacheTag('name')labels the entry;revalidateTag('name', 'max')orupdateTag('name')invalidates it.- The build table shows each route revalidate and expire times, and responses get a matching
s-maxageandstale-while-revalidate. - Short lifetimes are excluded from prerendering and must sit inside Suspense:
revalidateof 0,expireunder 5 minutes orstaleunder 30 s. Of the built-in profiles onlysecondsqualifies, because of its 1-minute expire.
Clarifying questions expected:
- "How stale may this data be, and for whom?" — decides the profile; per-user data should not be cached this way at all.
- "What event should refresh it?" — decides the tag.
Code / implementation expected: Yes — a built-in profile, a custom profile, and a tag with invalidation.