Page-level vs function-level vs component-level caching, how does Next.js handle rendering? #92166
-
|
Hi! I have a page where all parts are static except one data-fetching function, Current setup:
export default async function Home() {
const projects = await getProjects();
return (...)
}
export function Projects({ projects }: { projects: ProjectsQueryResult }) {
return (...)
}
export async function getProjects() {
"use cache";
cacheLife("hours");
// fetch from Sanity
}My questions:
Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
|
In Next.js with the App Router, the "level" of caching determines what work is saved. Since you are using the 1. Function-level Caching vs. Page RenderingIf you use
2. Moving the call to the Component levelMoving the call into the
3. Achieving a fully Static PageTo make the entire page static and regenerate every hour, you should apply the caching at the entry point or ensure the route satisfies the requirements for Static Site Generation (SSG).
Tip For the best performance, apply If you want the whole page to be a static shell that updates every hour, use the segment config at the top of // page.tsx
export const revalidate = 3600; // Revalidate the whole page every hour
export default async function Home() {
const projects = await getProjects();
return <Projects projects={projects} />
}Does your page rely on any dynamic functions like |
Beta Was this translation helpful? Give feedback.
-
|
The way to think about this with Cache Components, is that pages are no longer either static or dynamic, they can be both. If a page is not accessing dynamic data (fetch, remote db queries, async file system reads), runtime data (cookies, searchParams, etc), or non-deterministic operations (Math.random or Date access), then it will complete its rendering, and be a fully static page. You wouldn't need to add a caching directive at all. This is the static shell, the static output (contribution) for this page, and in the case above, it is the entire page. This would page have zero fallbacks for dynamic content, (it contributes with zero dynamic content). The so called dynamic data, may be cached, given some conditions, the framework shows helpful errors if you are not meeting these conditions but essentially, don't access runtime data, under a cached scope, read it outside and pass it as a function input. Don't read cookies as part of a cached function, directly or indirectly. When dynamic data is cached, it can be included into the static shell/output. And it is given a cacheLife (implicitly 'default'). This sets the conditions for when the cached function will be executed again, in this case, that'll also update the static output. A very sort cacheLife, zero revalidate or expire under 5 minutes, is excluded from the static shell. Anyway, to keep things short, in your case, an otherwise static page, with a fetch |
Beta Was this translation helpful? Give feedback.
The way to think about this with Cache Components, is that pages are no longer either static or dynamic, they can be both.
If a page is not accessing dynamic data (fetch, remote db queries, async file system reads), runtime data (cookies, searchParams, etc), or non-deterministic operations (Math.random or Date access), then it will complete its rendering, and be a fully static page. You wouldn't need to add a caching directive at all.
This is the static shell, the static output (contribution) for this page, and in the case above, it is the entire page. This would page have zero fallbacks for dynamic content, (it contributes with zero dynamic content).
The so called dynamic data, may be ca…