File size: 11,000 Bytes
94786da | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 | 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')
|