Skip to content

Commit 8b01e25

Browse files
authored
Fix nested unstable_cache revalidating (vercel#57316)
This ensures when we call `revalidateTag` and there are nested `unstable_cache` entries we properly revalidate.
1 parent a929bf7 commit 8b01e25

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

packages/next/src/server/web/spec-extension/unstable-cache.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ export function unstable_cache<T extends Callback>(
8282
const cacheKey = await incrementalCache?.fetchCacheKey(joinedKey)
8383
const cacheEntry =
8484
cacheKey &&
85+
// when we are nested inside of other unstable_cache's
86+
// we should bypass cache similar to fetches
87+
store?.fetchCache !== 'force-no-store' &&
8588
!(
8689
store?.isOnDemandRevalidate || incrementalCache.isOnDemandRevalidate
8790
) &&

test/e2e/app-dir/app-static/app-static.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ createNextDescribe(
222222
const $ = cheerio.load(html)
223223
const initLayoutData = $('#layout-data').text()
224224
const initPageData = $('#page-data').text()
225+
const initNestedCacheData = $('#nested-cache').text()
225226

226227
const routeHandlerRes = await next.fetch(
227228
'/route-handler/revalidate-360'
@@ -254,6 +255,7 @@ createNextDescribe(
254255
const new$ = cheerio.load(newHtml)
255256
const newLayoutData = new$('#layout-data').text()
256257
const newPageData = new$('#page-data').text()
258+
const newNestedCacheData = new$('#nested-cache').text()
257259

258260
const newRouteHandlerRes = await next.fetch(
259261
'/route-handler/revalidate-360'
@@ -271,6 +273,7 @@ createNextDescribe(
271273
expect(newEdgeRouteHandlerData).toBeTruthy()
272274
expect(newLayoutData).not.toBe(initLayoutData)
273275
expect(newPageData).not.toBe(initPageData)
276+
expect(newNestedCacheData).not.toBe(initNestedCacheData)
274277
expect(newRouteHandlerData).not.toEqual(initRouteHandlerData)
275278
expect(newEdgeRouteHandlerData).not.toEqual(initEdgeRouteHandlerRes)
276279
return 'success'

test/e2e/app-dir/app-static/app/variable-revalidate/revalidate-360/page.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,33 @@ export default async function Page() {
5454
}
5555
)()
5656

57+
const cacheInner = unstable_cache(
58+
async () => {
59+
console.log('calling cacheInner')
60+
const data = await fetch(
61+
'https://next-data-api-endpoint.vercel.app/api/random?something',
62+
{
63+
next: { revalidate: 15, tags: ['thankyounext'] },
64+
}
65+
).then((res) => res.text())
66+
return data
67+
},
68+
[],
69+
{ revalidate: 360 }
70+
)
71+
72+
const cacheOuter = unstable_cache(
73+
() => {
74+
console.log('cacheOuter')
75+
return cacheInner()
76+
},
77+
[],
78+
{
79+
revalidate: 1000,
80+
tags: ['thankyounext'],
81+
}
82+
)()
83+
5784
return (
5885
<>
5986
<p id="page">/variable-revalidate/revalidate-360</p>
@@ -65,6 +92,7 @@ export default async function Page() {
6592
revalidate 10 (tags: thankyounext): {JSON.stringify(cachedData)}
6693
</p>
6794
<p id="now">{Date.now()}</p>
95+
<p id="nested-cache">nested cache: {cacheOuter}</p>
6896
</>
6997
)
7098
}

0 commit comments

Comments
 (0)