Unverified Commit 6da6388b authored by mattddean's avatar mattddean Committed by GitHub

Correctly deserialize `undefined` unstable_cache data (#59126)

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->

The value `undefined` can be saved to the incremental cache as
`undefined` (`JSON.stringify(undefined)`) with no errors, but when
retrieving it, we attempt to parse it as JSON using
`JSON.parse(undefined)`. This throws an error. We should instead
deserialize `undefined` as `undefined` when retrieving.

relevant discussion: #59087

---------
Co-authored-by: 's avatarSam Ko <sam@vercel.com>
parent b8d63702
......@@ -186,7 +186,10 @@ export function unstable_cache<T extends Callback>(
} else {
// We have a valid cache entry so we will be returning it. We also check to see if we need
// to background revalidate it by checking if it is stale.
const cachedResponse = JSON.parse(cacheEntry.value.data.body)
const cachedResponse =
cacheEntry.value.data.body !== undefined
? JSON.parse(cacheEntry.value.data.body)
: undefined
if (cacheEntry.isStale) {
// In App Router we return the stale result and revalidate in the background
if (!store.pendingRevalidates) {
......@@ -281,7 +284,9 @@ export function unstable_cache<T extends Callback>(
// will fall through to generating a new cache entry below
} else if (!cacheEntry.isStale) {
// We have a valid cache entry and it is fresh so we return it
return JSON.parse(cacheEntry.value.data.body)
return cacheEntry.value.data.body !== undefined
? JSON.parse(cacheEntry.value.data.body)
: undefined
}
}
}
......
......@@ -667,6 +667,10 @@ createNextDescribe(
"static-to-dynamic-error-forced/[id]/page_client-reference-manifest.js",
"static-to-dynamic-error/[id]/page.js",
"static-to-dynamic-error/[id]/page_client-reference-manifest.js",
"unstable-cache/dynamic-undefined/page.js",
"unstable-cache/dynamic-undefined/page_client-reference-manifest.js",
"unstable-cache/dynamic/page.js",
"unstable-cache/dynamic/page_client-reference-manifest.js",
"variable-config-revalidate/revalidate-3.html",
"variable-config-revalidate/revalidate-3.rsc",
"variable-config-revalidate/revalidate-3/page.js",
......@@ -3012,6 +3016,31 @@ createNextDescribe(
})
})
describe('unstable_cache', () => {
it('should retrieve the same value on second request', async () => {
const res = await next.fetch('/unstable-cache/dynamic')
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).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()
const data = cheerio.load(html)('#cached-data').text()
const res2 = await next.fetch('/unstable-cache/dynamic-undefined')
const html2 = await res2.text()
const data2 = cheerio.load(html2)('#cached-data').text()
expect(data).toEqual(data2)
expect(data).toEqual('typeof cachedData: undefined')
})
})
it('should keep querystring on static page', async () => {
const browser = await next.browser('/blog/tim?message=hello-world')
const checkUrl = async () =>
......
import { revalidateTag, unstable_cache } from 'next/cache'
import { RevalidateButton } from '../revalidate-button'
export const dynamic = 'force-dynamic'
export default async function Page() {
async function revalidate() {
'use server'
await revalidateTag('undefined-value-data')
}
const cachedData = await unstable_cache(
async () => {
return undefined as undefined
},
['undefined-value'],
{
tags: ['undefined-value-data'],
}
)()
return (
<div>
<p>random: {Math.random()}</p>
<p id="cached-data">typeof cachedData: {typeof cachedData}</p>
<RevalidateButton onClick={revalidate} />
</div>
)
}
import { revalidateTag, unstable_cache } from 'next/cache'
import { RevalidateButton } from '../revalidate-button'
export const dynamic = 'force-dynamic'
export default async function Page() {
async function revalidate() {
'use server'
await revalidateTag('random-value-data')
}
const cachedData = await unstable_cache(
async () => {
return {
random: Math.random(),
}
},
['random-value'],
{
tags: ['random-value-data'],
}
)()
return (
<div>
<p>random: {Math.random()}</p>
<p id="cached-data">cachedData: {cachedData.random}</p>
<RevalidateButton onClick={revalidate} />
</div>
)
}
'use client'
export function RevalidateButton({ onClick }) {
return (
<form action={onClick}>
<button type="submit">revalidate</button>
</form>
)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment