Files
next.js/test/e2e/app-dir/use-cache-dev/app/page.tsx
Hendrik LiebauandGitHub 71f323d07b [Cache Components] Fix HMR for nested pages (#82776)
To bypass existing caches when editing server components, we store an
HMR hash in a session cookie. The cookie is sent to the server when
refetching the RSC, and included in the cache key of all `'use cache'`
functions (see #75474).

Before this fix, we weren't setting the cookie path properly, which led
to multiple cookies for different paths being written and sent to the
server. This caused stale values to be used for nested pages, which
resulted in stale components being displayed after edits.

Now, we set the cookie path to `/`, ensuring that the same cookie is
used for all nested pages.

closes NAR-204
resolves #81538
2025-08-19 16:13:54 +02:00

46 lines
1001 B
TypeScript

import { revalidatePath } from 'next/cache'
import Link from 'next/link'
async function getRandomValue() {
'use cache'
return Math.random()
}
async function getData() {
'use cache'
return fetch('https://next-data-api-endpoint.vercel.app/api/random').then(
(res) =>
res
.text()
.then(async (text) => [text, 'foo', await getRandomValue()] as const)
)
}
export default async function Page() {
const [fetchedRandom, text, mathRandom] = await getData()
return (
<>
<div id="container">
<p id="fetchedRandom">{fetchedRandom}</p>
<p id="text">{text}</p>
<p id="mathRandom">{mathRandom}</p>
</div>
<p id="uncached">{new Date().toISOString()}</p>
<form
action={async () => {
'use server'
revalidatePath('/')
}}
>
<button id="revalidate">Revalidate</button>
</form>
<p>
<Link href="/some/path">Go to /some/path</Link>
</p>
</>
)
}