Files
Wyatt JohnsonandGitHub dc6d612529 fix: ensure dynamic fetches are tracked correctly (#66349)
This modifies the patched fetch implementation to better handle when
called within an `unstable_cache` callback. In these callbacks, it
should not throw an error related to dynamic access.

This replaces the verbose `trackDynamicFetch` instead with
`markCurrentScopeAsDynamic` which already has support for checking if
inside an unstable cache context. It also has been adjusted to be a
no-op when `export const dynamic = "force-static"`, further simplifying
the code within the patch fetch implementation.
2024-05-30 16:54:03 +00:00

30 lines
592 B
React

import { unstable_cache } from 'next/cache'
import { Suspense } from 'react'
const getData = unstable_cache(
async (cache) => {
const res = await fetch(
'https://next-data-api-endpoint.vercel.app/api/random',
{ cache }
)
return res.text()
},
undefined,
{}
)
async function Data({ cache = undefined }) {
const data = await getData(cache)
return <div id="data">{data}</div>
}
export default function Page({ cache = undefined }) {
return (
<Suspense fallback={<div id="loading">Loading...</div>}>
<Data cache={cache} />
</Suspense>
)
}