Frontend performance can be hard to get right. Even in highly optimized apps, the most common culprit by far is client-server waterfalls. When introducing Next.js App Router, we knew we wanted to solve this issue. To do that, we needed to move client-server REST fetches to the server using React Server Components in a single roundtrip. This meant the server had to sometimes be dynamic, sacrificing the great initial loading performance of Jamstack. We built partial prerendering to solve this tradeoff and have the best of both worlds.
However, along the way, the developer experience suffered due to the caching defaults and controls we provided.
This is an excerpt. The full article is published by Next.js Blog.
Answers to common questions regarding this article's topic, architecture, and practical applications.
The first thing you'll notice is that when you add data to your components, you will now get an error. async function Component() {
return fetch(...) // error
}
export default async function Page() {
return <Component />
}
To use data, cookies, headers, current time or random values, you now have a choice: do you want the data to be cached (server or client-side) or executed on every request? I'm using fetch() as an example, but this applie
General
You can also mix and match. For example, you can put use cache in your root layout to ensure it is cached. Each layout or page can be cached independently. "use cache"
export default async function Layout({ children }) {
const response = await fetch(...)
const data = await response.json()
return <html>
<body>
<div>{data.notice}</div>
{children}
</body>
</html>
}
While using dynamic data within a specific Page:
General
Frontend performance can be hard to get right. Even in highly optimized apps, the most common culprit by far is client-server waterfalls. When introducing Next.js App Router, we knew we wanted to solve this issue. To do that, we needed to move client-server REST fetches to the server using React Server Components in a single roundtrip. This meant the server had to sometimes be dynamic, sacrificing the great initial loading performance of Jamstack. We built partial prerendering to solve this trad
Definition / Understanding
If you're building something static and don't want to use dynamic functionality, you can use the new use cache directive. "use cache"
export default async function Page() {
return fetch(...) // no error
}
By marking the Page with use cache, you're indicating that the entire segment should be cached. This means any data you fetch can now be cached, allowing the page to be statically rendered. No <Suspense> boundary is used for static content.
General
If you want to explicitly clear a cache entry by tag, you can use the new cacheTag() API inside the use cache function. import { cacheTag } from 'next/cache';
async function getNotice() {
'use cache';
cacheTag('my-tag');
}
Then, just call revalidateTag('my-tag') from a Server Action as before. Since this API can be called after data loading, you can now use data to tag your cache entries.