Skip to content

Commit

Permalink
fix: don’t cache new result in draft mode
Browse files Browse the repository at this point in the history
  • Loading branch information
timfuhrmann committed Jul 15, 2024
1 parent 060d9a1 commit eea2a95
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
22 changes: 13 additions & 9 deletions packages/next/src/server/web/spec-extension/unstable-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,15 +267,19 @@ export function unstable_cache<T extends Callback>(
cb,
...args
)
cacheNewResult(
result,
incrementalCache,
cacheKey,
tags,
options.revalidate,
fetchIdx,
fetchUrl
)

if (!staticGenerationStore.isDraftMode) {
cacheNewResult(
result,
incrementalCache,
cacheKey,
tags,
options.revalidate,
fetchIdx,
fetchUrl
)
}

return result
} else {
noStoreFetchIdx += 1
Expand Down
20 changes: 20 additions & 0 deletions test/e2e/app-dir/app-static/app-static.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3489,6 +3489,26 @@ describe('app-dir static/dynamic handling', () => {
expect(data).not.toEqual(data2)
})

it('should not cache new result in draft mode', async () => {
const draftRes = await next.fetch('/api/draft-mode?status=enable')
const setCookie = draftRes.headers.get('set-cookie')
const cookieHeader = { Cookie: setCookie?.split(';', 1)[0] }

expect(cookieHeader.Cookie).toBeTruthy()

const res = await next.fetch('/unstable-cache/dynamic', {
headers: cookieHeader,
})
const html = await res.text()
const data = cheerio.load(html)('#cached-data').text()

const res2 = await next.fetch('/unstable-cache/dynamic')
const html2 = await res2.text()
const data2 = cheerio.load(html2)('#cached-data').text()

expect(data).not.toEqual(data2)
})

it('should not error when retrieving the value undefined', async () => {
const res = await next.fetch('/unstable-cache/dynamic-undefined')
const html = await res.text()
Expand Down

0 comments on commit eea2a95

Please sign in to comment.