What happens if you have nested "use cache"? #84519
-
I am testing the new "use cache" way, but I have some serious questions about it. Imagine I want to add a granular cache in each "get" query: getTodo query, getTodoStats query, etc Then I am using a function getTodos, that calls all of them in user/[userId]/todo/page.tsx. I have then "use cache" cacheTag("todos-${userId}") and nested on it, all the use cache of each getTodo cacheTag("todo-${todoId}"). What is happenning on this case if I have only revalidatePath on the createTodo or updateTodo) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
When you have nested In your example:
How it works:
Your scenario with
This granular approach is actually powerful for performance - you can update just one todo without invalidating the entire list! |
Beta Was this translation helpful? Give feedback.
-
thanks for the answer, then, if I understood well: |
Beta Was this translation helpful? Give feedback.
When you have nested
"use cache"
directives in Next.js 15, each cache layer works independently with its own tags and scope.In your example:
getTodos
withcacheTag("todos-${userId}")
) - caches the entire resultgetTodo
,getTodoStats
withcacheTag("todo-${todoId}")
) - cache individual queriesHow it works:
revalidatePath()
orrevalidateTag()
, only the matching cache layers are invalidatedYour scenario with
revalidatePath
on createTodo/updateTodo: