avyvar's picture
Add files using upload-large-folder tool
94786da verified
Raw
History Blame Contribute Delete
11 kB
diff --git a/test/development/app-dir/cache-components-dev-streaming/app/page.tsx b/test/development/app-dir/cache-components-dev-streaming/app/page.tsx
index a5d1524c73..4f3c15dfb9 100644
--- a/test/development/app-dir/cache-components-dev-streaming/app/page.tsx
+++ b/test/development/app-dir/cache-components-dev-streaming/app/page.tsx
@@ -14,6 +14,11 @@ export default function Page() {
/use-cache-private-runtime-prefetch
</Link>
</li>
+ <li>
+ <Link href="/use-cache-expire-zero/nav">
+ /use-cache-expire-zero/nav
+ </Link>
+ </li>
<li>
<Link href="/partial-prefetching/session-data">
/partial-prefetching/session-data
diff --git a/test/development/app-dir/cache-components-dev-streaming/app/use-cache-expire-zero/[slug]/page.tsx b/test/development/app-dir/cache-components-dev-streaming/app/use-cache-expire-zero/[slug]/page.tsx
new file mode 100644
index 0000000000..cdf45502d4
--- /dev/null
+++ b/test/development/app-dir/cache-components-dev-streaming/app/use-cache-expire-zero/[slug]/page.tsx
@@ -0,0 +1,43 @@
+import { Suspense } from 'react'
+import { setTimeout } from 'timers/promises'
+import { cacheLife } from 'next/cache'
+
+export const prefetch = 'allow-runtime'
+
+// A distinct slug per test keys a separate cache entry (so the first request
+// for each slug is a genuine cold miss), while both tests share this one
+// runtime-prefetchable page. Declaring the slugs also keeps `params` statically
+// known, so the page shell doesn't depend on dynamic params. In development
+// this does not pre-fill the cache.
+export function generateStaticParams() {
+ return [{ slug: 'nav' }, { slug: 'reload' }]
+}
+
+async function getExpireZeroValue(slug: string) {
+ 'use cache'
+ // An explicit short `expire` opts this public cache into a dynamic,
+ // client-only life: excluded from the static shell, but included in the
+ // runtime prefetch. The slug keys the entry; the value itself is just a
+ // timestamp.
+ cacheLife({ expire: 0 })
+ await setTimeout(1500)
+ return new Date().toISOString()
+}
+
+async function ExpireZeroCached({ slug }: Promise<{ slug: string }>) {
+ const value = await getExpireZeroValue(slug)
+
+ return <p id="expire-zero">{value}</p>
+}
+
+export default function Page({
+ params,
+}: {
+ params: Promise<{ slug: string }>
+}) {
+ return (
+ <Suspense fallback={<p id="expire-zero-fallback">Loading...</p>}>
+ <ExpireZeroCached slug={params.then(({ slug }) => slug)} />
+ </Suspense>
+ )
+}
diff --git a/test/development/app-dir/cache-components-dev-streaming/cache-components-dev-streaming.test.ts b/test/development/app-dir/cache-components-dev-streaming/cache-components-dev-streaming.test.ts
index 2f649898b1..b8c3b0eb1a 100644
--- a/test/development/app-dir/cache-components-dev-streaming/cache-components-dev-streaming.test.ts
+++ b/test/development/app-dir/cache-components-dev-streaming/cache-components-dev-streaming.test.ts
@@ -189,6 +189,91 @@ describe('cache-components-dev-streaming', () => {
})
})
+ it('shows the short-expire-cache fallback on a cold client navigation but not on a warm one', async () => {
+ // A public `'use cache'` with an explicit short `expire` (`cacheLife({
+ // expire: 0 })`) is a runtime-prefetch route here, so its cached content
+ // belongs to the runtime shell stage. On a warm navigation the dev minimum
+ // retention keeps the entry available, and the client defers revealing the
+ // response until the shell has flushed, so the content arrives with the
+ // shell and the fallback isn't shown - just like a private cache.
+ const browser = await next.browser('/')
+
+ // Cold navigation: the cache misses and fills in the background, so the
+ // fallback is shown until the content streams in.
+ await browser.elementByCss('a[href="/use-cache-expire-zero/nav"]').click()
+ expect(await browser.elementByCss('#expire-zero-fallback').text()).toBe(
+ 'Loading...'
+ )
+ expect(await browser.elementByCss('#expire-zero').text()).toBeDateString()
+
+ // Wait for the background write to settle so the next navigation hits the
+ // warm entry instead of racing a pending write.
+ await waitFor(2000)
+
+ // Hard-reload home so the warm navigation below starts from a fresh page.
+ await browser.loadPage(new URL('/', next.url).href)
+
+ // Warm navigation: record whether the fallback ever enters the DOM. It
+ // shouldn't, since the retained entry is delivered with the shell. (The
+ // client-side reveal race that this delivery relies on is covered by the
+ // private-cache test above, so we don't repeat its stress loop here.)
+ const fallbackObserver = observeNodeAppearances(browser, [
+ 'expire-zero-fallback',
+ ])
+
+ await fallbackObserver.observe()
+
+ await browser.elementByCss('a[href="/use-cache-expire-zero/nav"]').click()
+ expect(await browser.elementByCss('#expire-zero').text()).toBeDateString()
+
+ const appearanceCounts = await fallbackObserver.getResult()
+ expect(appearanceCounts).toEqual({
+ 'expire-zero-fallback': 0,
+ })
+ })
+
+ it('serves a short-expire cache warm on reload and converges to a fresh value', async () => {
+ const browser = await next.browser('/use-cache-expire-zero/reload', {
+ waitHydration: false,
+ // Do not wait for "load"; inspect the page as it streams in.
+ waitUntil: 'commit',
+ })
+
+ // Cold load: the cache misses, so the fallback streams first, and the
+ // generated value streams in once generation completes. The value is a
+ // dynamic hole (real `expire: 0`), so it streams in after the shell.
+ expect(
+ await browser
+ .elementByCss('#expire-zero-fallback', { waitUntil: false })
+ .text()
+ ).toBe('Loading...')
+ const coldValue = await browser
+ .elementByCss('#expire-zero', { waitUntil: false })
+ .text()
+ expect(coldValue).toBeDateString()
+
+ // Warm reload: the dev minimum retention keeps the short-expire entry, so
+ // the reload serves the previously cached value fast instead of
+ // regenerating it. A background revalidation regenerates a fresh entry for
+ // the next reload (asserted below). We wait for the streamed-in element
+ // without waiting for "load", so no retry is needed.
+ await browser.refresh({ waitUntil: 'commit' })
+ expect(
+ await browser.elementByCss('#expire-zero', { waitUntil: false }).text()
+ ).toBe(coldValue)
+
+ // That warm reload re-warmed a fresh entry in the background, so a later
+ // reload converges to the new value. Read after "load" here (a plain
+ // refresh) since we want the settled value, not the streaming inspection
+ // above.
+ await retry(async () => {
+ await browser.refresh()
+ expect(await browser.elementById('expire-zero').text()).not.toBe(
+ coldValue
+ )
+ })
+ })
+
// The following are smoke tests that Cache Components validation still
// surfaces errors for both cold-cache renders (validated via a separate
// warm-cache render) and warm-cache renders (validated via the streamed
diff --git a/test/development/app-dir/use-cache-custom-handler-dev/app/expire-zero/page.tsx b/test/development/app-dir/use-cache-custom-handler-dev/app/expire-zero/page.tsx
new file mode 100644
index 0000000000..e7d5b06820
--- /dev/null
+++ b/test/development/app-dir/use-cache-custom-handler-dev/app/expire-zero/page.tsx
@@ -0,0 +1,36 @@
+import { Suspense } from 'react'
+import { setTimeout } from 'timers/promises'
+import { cacheLife } from 'next/cache'
+
+// A public `'use cache'` routed through the custom (slow) handler, with an
+// explicit short `expire`. In dev the built-in front handler applies a minimum
+// retention, so a cache hit still resolves from the front in a microtask
+// instead of paying the backing's latency on every read.
+async function getCachedValue() {
+ 'use cache'
+ // `expire: 0` gives a short, dynamic (client-only) cache life, excluded from
+ // static prerenders. Reusing it across client navigations would require
+ // opting the route into runtime prefetching (`prefetch = 'allow-runtime'`) so
+ // Cached Navigations embeds it into the client router cache; this fixture
+ // doesn't, since the test only exercises the dev front handler serving it
+ // warm on reloads.
+ cacheLife({ expire: 0 })
+ await setTimeout(1000)
+ return new Date().toISOString()
+}
+
+async function CachedValue() {
+ const value = await getCachedValue()
+
+ return <p id="value">{value}</p>
+}
+
+export default function Page() {
+ return (
+ <main>
+ <Suspense fallback={<p id="loading">Loading...</p>}>
+ <CachedValue />
+ </Suspense>
+ </main>
+ )
+}
diff --git a/test/development/app-dir/use-cache-custom-handler-dev/use-cache-custom-handler-dev.test.ts b/test/development/app-dir/use-cache-custom-handler-dev/use-cache-custom-handler-dev.test.ts
index f7a8508129..197060fa4a 100644
--- a/test/development/app-dir/use-cache-custom-handler-dev/use-cache-custom-handler-dev.test.ts
+++ b/test/development/app-dir/use-cache-custom-handler-dev/use-cache-custom-handler-dev.test.ts
@@ -40,6 +40,38 @@ describe('use-cache-custom-handler-dev', () => {
expect(await browser.hasElementByCss('[data-cold-cache-badge]')).toBe(false)
})
+ it('serves a short-expire value warm through a custom handler and re-warms it on each reload', async () => {
+ const browser = await next.browser('/expire-zero', {
+ waitHydration: false,
+ // Do not wait for "load"; inspect the page as it streams in.
+ waitUntil: 'commit',
+ })
+
+ // Cold load: the custom handler misses, the value generates and is written
+ // through to both the backing handler and the dev-only in-memory front. We
+ // wait for the streamed-in element without waiting for "load".
+ const coldValue = await browser
+ .elementByCss('#value', { waitUntil: false })
+ .text()
+ expect(coldValue).toBeDateString()
+
+ // Warm reload: served fast from the front, whose minimum retention keeps
+ // the short-`expire` entry. The custom handler's slow `get` isn't on the
+ // critical path, and the short `expire` no longer evicts the front entry on
+ // every read, so the same cached value shows.
+ await browser.refresh({ waitUntil: 'commit' })
+ expect(
+ await browser.elementByCss('#value', { waitUntil: false }).text()
+ ).toBe(coldValue)
+
+ // Each warm reload re-executes the cache function and writes through to the
+ // backing, so reloads converge to a fresh value.
+ await retry(async () => {
+ await browser.refresh()
+ expect(await browser.elementById('value').text()).not.toBe(coldValue)
+ })
+ })
+
it('stops serving a front-cached entry after the backing cache is purged out-of-band', async () => {
const browser = await next.browser('/purged')