Unverified Commit 1558dbef authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

[test] Add test cases for dynamic caches without suspense boundaries (#85500)

When defining a `cacheLife` with an expire time below 5 minutes or a
revalidate time of 0 we consider the `'use cache'` function dynamic
during prerendering, i.e. it will be excluded from the static shell.
Similarly, accessing runtime `params` (not defined in
`generateStaticParams`) also makes the function dynamic. Those cases
require adding a parent suspense boundary. If there's no suspense
boundary, we should show an error during prerendering at build time, as
well as in development as part of the prerender validation. The latter
is currently not working as expected when the `'use cache'` directive is
used in a page (or layout) directly. As the added snapshot tests show,
no redbox is shown in those cases. This is a known issue that we plan to
address separately.

We will also follow up with improving the error messages and docs for
those dynamic cache cases. This detail got lost in #85087.
parent d27f18f3
export default function Root({ children }: { children: React.ReactNode }) {
return (
<html>
<body>
<main>{children}</main>
</body>
</html>
)
}
import { cacheLife } from 'next/cache'
export default async function Page() {
'use cache: remote'
cacheLife({ expire: 299 }) // 1 second below the threshold of 5 minutes
return (
<>
<p>
This page is cached with a low expire time. Such a short-lived cache is
excluded from prerenders, and creates a dynamic hole. Without a parent
suspense boundary, this will cause an error during prerendering.
</p>
</>
)
}
export default async function Page({
params,
}: {
params: Promise<{ slug: string }>
}) {
'use cache'
const { slug } = await params
return (
<>
<p>
This page accesses params without defining any static params. This
excludes the page from prerenders, and creates a dynamic hole. Without a
parent suspense boundary, this will cause an error during prerendering.
</p>
<p>Slug: {slug}</p>
</>
)
}
export default function Root({ children }: { children: React.ReactNode }) {
return (
<html>
<body>
<main>{children}</main>
</body>
</html>
)
}
export default function Root({ children }: { children: React.ReactNode }) {
return (
<html>
<body>
<main>{children}</main>
</body>
</html>
)
}
import { cacheLife } from 'next/cache'
export default async function Page() {
'use cache: remote'
cacheLife({ revalidate: 0 })
return (
<>
<p>
This page is cached with a zero revalidate time. Such a short-lived
cache is excluded from prerenders, and creates a dynamic hole. Without a
parent suspense boundary, this will cause an error during prerendering.
</p>
</>
)
}
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