Unverified Commit eb756a63 authored by Janka Uryga's avatar Janka Uryga Committed by GitHub

[Cache Components] give the "seconds" profile a 30s staleTime (#82332)

Closes NAR-262
parent 2dc45422
...@@ -1355,7 +1355,7 @@ export const defaultConfig = Object.freeze({ ...@@ -1355,7 +1355,7 @@ export const defaultConfig = Object.freeze({
expire: INFINITE_CACHE, expire: INFINITE_CACHE,
}, },
seconds: { seconds: {
stale: undefined, // defaults to staleTimes.dynamic stale: 30, // 30 seconds
revalidate: 1, // 1 second revalidate: 1, // 1 second
expire: 60, // 1 minute expire: 60, // 1 minute
}, },
......
...@@ -942,18 +942,6 @@ function assignDefaults( ...@@ -942,18 +942,6 @@ function assignDefaults(
result.expireTime ?? defaultDefault.expire result.expireTime ?? defaultDefault.expire
} }
} }
// This is the most dynamic cache life profile.
const secondsCacheLifeProfile = result.experimental.cacheLife['seconds']
if (
secondsCacheLifeProfile &&
secondsCacheLifeProfile.stale === undefined
) {
// We default this to whatever stale time you had configured for dynamic content.
// Since this is basically a dynamic cache life profile.
const dynamicStaleTime = result.experimental.staleTimes?.dynamic
secondsCacheLifeProfile.stale =
dynamicStaleTime ?? defaultConfig.experimental?.staleTimes?.dynamic
}
} }
if (result.experimental?.cacheHandlers) { if (result.experimental?.cacheHandlers) {
......
import { Suspense } from 'react'
import { cachedDelay, DebugRenderKind } from '../../../shared'
import { unstable_cacheLife } from 'next/cache'
export default async function Page() {
return (
<main>
<DebugRenderKind />
<p id="intro">
This page uses a short-lived private cache (with cacheLife("seconds")),
which should not be included in a static prefetch, but should be
included in a runtime prefetch, because it has a long enough stale time
(&ge; RUNTIME_PREFETCH_DYNAMIC_STALE, 30s)
</p>
<Suspense fallback={<div style={{ color: 'grey' }}>Loading...</div>}>
<ShortLivedCache />
</Suspense>
</main>
)
}
async function ShortLivedCache() {
'use cache: private'
unstable_cacheLife('seconds')
await cachedDelay([__filename])
return (
<div style={{ border: '1px solid blue', padding: '1em' }}>
Short-lived cached content
<div id="cached-value">{Date.now()}</div>
</div>
)
}
import { Suspense } from 'react'
import { cachedDelay, DebugRenderKind } from '../../../shared'
import { unstable_cacheLife } from 'next/cache'
export default async function Page() {
return (
<main>
<DebugRenderKind />
<p id="intro">
This page uses a short-lived public cache (with cacheLife("seconds")),
which should not be included in a static prefetch, but should be
included in a runtime prefetch, because it has a long enough stale time
(&ge; RUNTIME_PREFETCH_DYNAMIC_STALE, 30s)
</p>
<Suspense fallback={<div style={{ color: 'grey' }}>Loading...</div>}>
<ShortLivedCache />
</Suspense>
</main>
)
}
async function ShortLivedCache() {
'use cache'
unstable_cacheLife('seconds')
await cachedDelay([__filename])
return (
<div style={{ border: '1px solid blue', padding: '1em' }}>
Short-lived cached content
<div id="cached-value">{Date.now()}</div>
</div>
)
}
...@@ -10,7 +10,7 @@ export default async function Page() { ...@@ -10,7 +10,7 @@ export default async function Page() {
This page uses a short-lived public cache (expire &lt; DYNAMIC_EXPIRE, This page uses a short-lived public cache (expire &lt; DYNAMIC_EXPIRE,
5min), which should not be included in a static prefetch, but should be 5min), which should not be included in a static prefetch, but should be
included in a runtime prefetch, because it has a long enough stale time included in a runtime prefetch, because it has a long enough stale time
(&gt; RUNTIME_PREFETCH_DYNAMIC_STALE, 30s) (&ge; RUNTIME_PREFETCH_DYNAMIC_STALE, 30s)
</p> </p>
<Suspense fallback={<div style={{ color: 'grey' }}>Loading...</div>}> <Suspense fallback={<div style={{ color: 'grey' }}>Loading...</div>}>
<ShortLivedCache /> <ShortLivedCache />
......
...@@ -189,6 +189,40 @@ export default async function Page() { ...@@ -189,6 +189,40 @@ export default async function Page() {
</li> </li>
</ul> </ul>
</li> </li>
<li>
public, cacheLife("seconds")
<ul>
<li>
<DebugLinkAccordion
href="/caches/public-seconds"
prefetch={'auto'}
/>
</li>
<li>
<DebugLinkAccordion
href="/caches/public-seconds"
prefetch={true}
/>
</li>
</ul>
</li>
<li>
private, cacheLife("seconds")
<ul>
<li>
<DebugLinkAccordion
href="/caches/private-seconds"
prefetch={'auto'}
/>
</li>
<li>
<DebugLinkAccordion
href="/caches/private-seconds"
prefetch={true}
/>
</li>
</ul>
</li>
</ul> </ul>
<h2>misc</h2> <h2>misc</h2>
......
...@@ -670,10 +670,32 @@ describe('<Link prefetch={true}> (runtime prefetch)', () => { ...@@ -670,10 +670,32 @@ describe('<Link prefetch={true}> (runtime prefetch)', () => {
}) })
describe('cache stale time handling', () => { describe('cache stale time handling', () => {
it('includes short-lived public caches with a long enough staleTime', async () => { it.each([
{
// If a cache has an expiration time under 5min (DYNAMIC_EXPIRE), we omit it from static prerenders. // If a cache has an expiration time under 5min (DYNAMIC_EXPIRE), we omit it from static prerenders.
// However, it should still be included in a runtime prefetch if it's stale time is above 30s. (RUNTIME_PREFETCH_DYNAMIC_STALE) // However, it should still be included in a runtime prefetch if its stale time is >=30s. (RUNTIME_PREFETCH_DYNAMIC_STALE)
description:
'includes short-lived public caches with a long enough staleTime',
staticContent: 'This page uses a short-lived public cache',
path: '/caches/public-short-expire-long-stale',
},
{
// If a cache has an expiration time under 5min (DYNAMIC_EXPIRE), we omit it from static prerenders.
// However, it should still be included in a runtime prefetch if its stale time is >=30s. (RUNTIME_PREFETCH_DYNAMIC_STALE)
// `cacheLife("seconds")` is deliberately set to have a stale time of 30s to stay above this treshold.
description: 'includes public caches with cacheLife("seconds")',
staticContent: 'This page uses a short-lived public cache',
path: '/caches/public-seconds',
},
{
// A Private cache will always be omitted from static prerenders.
// However, it should still be included in a runtime prefetch if its stale time is >=30s. (RUNTIME_PREFETCH_DYNAMIC_STALE)
// `cacheLife("seconds")` is deliberately set to have a stale time of 30s to stay above this treshold.
description: 'includes private caches with cacheLife("seconds")',
staticContent: 'This page uses a short-lived private cache',
path: '/caches/private-seconds',
},
])('$description', async ({ path, staticContent }) => {
let page: Playwright.Page let page: Playwright.Page
const browser = await next.browser('/', { const browser = await next.browser('/', {
beforePageLoad(p: Playwright.Page) { beforePageLoad(p: Playwright.Page) {
...@@ -682,22 +704,20 @@ describe('<Link prefetch={true}> (runtime prefetch)', () => { ...@@ -682,22 +704,20 @@ describe('<Link prefetch={true}> (runtime prefetch)', () => {
}) })
const act = createRouterAct(page) const act = createRouterAct(page)
const STATIC_CONTENT = 'This page uses a short-lived public cache'
const DYNAMICALLY_PREFETCHABLE_CONTENT = 'Short-lived cached content' const DYNAMICALLY_PREFETCHABLE_CONTENT = 'Short-lived cached content'
// Reveal the link to trigger a static prefetch // Reveal the link to trigger a static prefetch
await act(async () => { await act(async () => {
const linkToggle = await browser.elementByCss( const linkToggle = await browser.elementByCss(
`input[data-prefetch="auto"][data-link-accordion="/caches/public-short-expire-long-stale"]` `input[data-prefetch="auto"][data-link-accordion="${path}"]`
) )
await linkToggle.click() await linkToggle.click()
}, [ }, [
// Should include the static shell // Should include the static shell
{ {
includes: STATIC_CONTENT, includes: staticContent,
}, },
// Should not include the short-lived cache // Should not include the short-lived cache
// (We set the `expire` value to be under 5min, so it will be excluded from prerenders)
{ {
includes: DYNAMICALLY_PREFETCHABLE_CONTENT, includes: DYNAMICALLY_PREFETCHABLE_CONTENT,
block: 'reject', block: 'reject',
...@@ -707,12 +727,11 @@ describe('<Link prefetch={true}> (runtime prefetch)', () => { ...@@ -707,12 +727,11 @@ describe('<Link prefetch={true}> (runtime prefetch)', () => {
// Reveal the link to trigger a runtime prefetch // Reveal the link to trigger a runtime prefetch
await act(async () => { await act(async () => {
const linkToggle = await browser.elementByCss( const linkToggle = await browser.elementByCss(
`input[data-prefetch="runtime"][data-link-accordion="/caches/public-short-expire-long-stale"]` `input[data-prefetch="runtime"][data-link-accordion="${path}"]`
) )
await linkToggle.click() await linkToggle.click()
}, [ }, [
// Should include the short-lived cache // Should include the short-lived cache
// (We set `stale` to be above 30s, which means it shouldn't be omitted)
{ {
includes: DYNAMICALLY_PREFETCHABLE_CONTENT, includes: DYNAMICALLY_PREFETCHABLE_CONTENT,
}, },
...@@ -721,9 +740,7 @@ describe('<Link prefetch={true}> (runtime prefetch)', () => { ...@@ -721,9 +740,7 @@ describe('<Link prefetch={true}> (runtime prefetch)', () => {
// Navigate to the page. We didn't include any uncached IO, so the page is fully prefetched, // Navigate to the page. We didn't include any uncached IO, so the page is fully prefetched,
// and this shouldn't issue any more requests // and this shouldn't issue any more requests
await act(async () => { await act(async () => {
await browser await browser.elementByCss(`a[href="${path}"]`).click()
.elementByCss(`a[href="/caches/public-short-expire-long-stale"]`)
.click()
}, 'no-requests') }, 'no-requests')
expect(await browser.elementByCss('main').text()).toInclude( expect(await browser.elementByCss('main').text()).toInclude(
......
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