Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: unstable_cache should not cache new result in draft mode #67772

Merged
merged 2 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading