Unverified Commit 41c4ca5b authored by Arya's avatar Arya Committed by Hendrik Liebau

fix(use-cache): track root params read in "use cache" as vary params

parent 52788bdf
......@@ -28,6 +28,7 @@ import type {
import {
getHmrRefreshHash,
getResumeDataCache,
getVaryParamsAccumulator,
workUnitAsyncStorage,
getDraftModeProviderForCacheScope,
getCacheSignal,
......@@ -35,6 +36,7 @@ import {
getServerComponentsHmrCache,
willConsumerServerCache,
} from '../app-render/work-unit-async-storage.external'
import { accumulateVaryParam } from '../app-render/vary-params'
import {
applyOwnerStack,
......@@ -1029,6 +1031,17 @@ function propagateCacheEntryMetadata(
cacheContext.outerWorkUnitStore satisfies never
}
}
if (metadata.readRootParamNames) {
const varyParamsAccumulator = getVaryParamsAccumulator(
cacheContext.outerWorkUnitStore
)
if (varyParamsAccumulator) {
for (const paramName of metadata.readRootParamNames) {
accumulateVaryParam(varyParamsAccumulator.rootParams, paramName)
}
}
}
}
/**
......@@ -1041,10 +1054,12 @@ function propagateCacheEntryMetadata(
* `propagateCacheEntryMetadata` is called unconditionally (after the omission
* checks have already filtered out short-lived entries).
*
* Note: Root param names are only propagated when the outer context is a
* `cache` store (i.e. an enclosing `"use cache"` function), which is never
* deferred. For prerender contexts, root param names are tracked separately
* via `addKnownRootParamNames` in the resume data cache read path.
* Note: Root param names are only propagated to `readRootParamNames` when the
* outer context is a `cache` store (i.e. an enclosing `"use cache"` function),
* which is never deferred. For prerender contexts, root param names are
* tracked separately via `addKnownRootParamNames` in the resume data cache
* read path. They're also recorded as root vary params (if tracked), so that
* the client doesn't reuse the segment across different values.
*/
function maybePropagateCacheEntryMetadata(
cacheContext: CacheContext,
......
'use cache'
import Link from 'next/link'
import { locale } from 'next/root-params'
export async function generateStaticParams() {
return [{ locale: 'en' }, { locale: 'de' }]
}
export default async function Layout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang={await locale()}>
<body>
<Link href="/cached-root-params/en" prefetch={false}>
en
</Link>
<Link href="/cached-root-params/de" prefetch={false}>
de
</Link>
{children}
</body>
</html>
)
}
'use cache'
import { locale } from 'next/root-params'
export default async function Page() {
return <div id="cached-root-param">{`Locale: ${await locale()}`}</div>
}
......@@ -822,4 +822,30 @@ describe('segment cache - vary params', () => {
{ includes: 'Root param page content - param: bbb' }
)
})
it('does not reuse a "use cache" segment across root param values', async () => {
let act: ReturnType<typeof createRouterAct>
const browser = await next.browser('/cached-root-params/en', {
beforePageLoad(p: Playwright.Page) {
act = createRouterAct(p)
},
})
expect(await browser.elementById('cached-root-param').text()).toBe(
'Locale: en'
)
// Not prefetched, so the navigation fetches /de and must not reuse the
// page segment cached for /en.
await act(async () => {
const link = await browser.elementByCss(
'a[href="/cached-root-params/de"]'
)
await link.click()
})
expect(await browser.elementById('cached-root-param').text()).toBe(
'Locale: de'
)
})
})
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