Mark a route segment as cacheable by adding the use cache
directive to the top of the layout.tsx
or page.tsx
file.
1'use cache';23export default async function Page() {4// ...5}
layout.tsx
and page.tsx
are independently cacheable. This means a layout.tsx
can be prerendered, while its page.tsx
can be dynamically rendered at request time.An artificial one second delay is added to the page.tsx
to make the difference more obvious.
1'use cache';23export default async function Page() {4await new Promise((resolve) => setTimeout(resolve, 1000));56const products = await getProducts();7// ...8}
Since the whole route is cacheable, this delay only happens the first time the function runs, during prerendering.
use cache
directive and describes caching behavior once stable.