Cache tag invalidation behavior is inconsistent and affects unrelated tags.
The issue involves inconsistent behavior of `updateTag`, which sometimes updates unrelated cache tags along with the specified one. This requires understanding the cache mechanism and potentially modifying how tags are invalidated. The scope is unclear due to unpredictable behavior and potential cross-cutting changes.
https://github.com/prananta/next-use-cache-private-bug
According to the doc in https://nextjs.org/docs/app/api-reference/functions/updateTag, updateTag is supposed to update cached data for a specific cache tag. But in my repo, updating certain tag updates other cached data with different tags too. In the repo example, i have three functions that use use cache, use cache: remote and use cache: private respectively. They also have different tags.
export const timeInAsia = async () => {
"use cache: remote";
cacheTag("time-in-asia");
cacheLife("hours");
console.log(`Fetching Asia time`);
await new Promise((resolve) => setTimeout(resolve, 1000));
const now = getDateInTimezone("Asia/Tokyo");
return now;
};
export const timeInUTC = async () => {
"use cache";
cacheTag("time-in-utc");
cacheLife("hours");
console.log(`Fetching UTC time`);
await new Promise((resolve) => setTimeout(resolve, 1000));
const now = getDateInTimezone("UTC");
return now;
};
export const timeInAmerica = async () => {
"use cache: private";
cacheTag("time-in-america");
cacheLife("hours");
console.log(`Fetching America time`);
await new Promise((resolve) => setTimeout(resolve, 1000));
const cookieStore = await cookies();
console.log(cookieStore.getAll());
const now = getDateInTimezone("America/New_York");
return now;
};
When I called updateTag('time-in-utc') from a server action for example, not only timeInUtc is re-executed, but also timeInAmerica. The updateTag's behavior is unpredictable right now. Sometimes it only updates the cached data for given tag, sometimes it affects other tags too. This happens in both dev and production.
Operating System:
Platform: darwin
Arch:
Claim this issue to let others know you're working on it. You'll earn 30 points when you complete it!