Loading...
Loading...
Compare original and translation side by side
| Feature | Server Component | Client Component |
|---|---|---|
| Directive | None (default) | |
| Async/await | Yes | No |
| Hooks | No | Yes |
| Browser APIs | No | Yes |
| Database access | Yes | No |
| Client JS bundle | Zero | Ships to client |
children| 特性 | 服务端组件 | 客户端组件 |
|---|---|---|
| 指令 | 无(默认) | |
| 异步等待 | 支持 | 不支持 |
| 钩子函数 | 不支持 | 支持 |
| 浏览器API | 不支持 | 支持 |
| 数据库访问 | 支持 | 不支持 |
| 客户端JS包 | 零体积 | 会打包发送到客户端 |
childrenimport { cacheLife, cacheTag } from 'next/cache'
// Cached component with duration
async function CachedProducts() {
'use cache'
cacheLife('hours')
cacheTag('products')
return await db.product.findMany()
}
// Invalidate cache
import { revalidateTag } from 'next/cache'
revalidateTag('products')// Static (cached indefinitely)
await fetch(url, { cache: 'force-cache' })
// Revalidate every 60 seconds
await fetch(url, { next: { revalidate: 60 } })
// Always fresh
await fetch(url, { cache: 'no-store' })
// Tag-based revalidation
await fetch(url, { next: { tags: ['posts'] } })import { cacheLife, cacheTag } from 'next/cache'
// 带缓存时长的组件
async function CachedProducts() {
'use cache'
cacheLife('hours')
cacheTag('products')
return await db.product.findMany()
}
// 失效缓存
import { revalidateTag } from 'next/cache'
revalidateTag('products')// 静态缓存(永久缓存)
await fetch(url, { cache: 'force-cache' })
// 每60秒重新验证一次
await fetch(url, { next: { revalidate: 60 } })
// 始终获取最新数据
await fetch(url, { cache: 'no-store' })
// 基于标签的重新验证
await fetch(url, { next: { tags: ['posts'] } })'use server'
export async function createPost(formData: FormData) {
const title = formData.get('title') as string
const post = await db.post.create({ data: { title } })
revalidatePath('/posts')
redirect("/posts/" + post.id)
}'use server'
export async function createPost(formData: FormData) {
const title = formData.get('title') as string
const post = await db.post.create({ data: { title } })
revalidatePath('/posts')
redirect("/posts/" + post.id)
}// app/posts/[slug]/page.tsx
export default async function PostPage({
params,
searchParams,
}: {
params: Promise<{ slug: string }>
searchParams: Promise<{ page?: string }>
}) {
const { slug } = await params
const { page } = await searchParams
return <Post slug={slug} page={page} />
}layout.tsxgenerateMetadata()references/nextjs-16-upgrade.md// app/posts/[slug]/page.tsx
export default async function PostPage({
params,
searchParams,
}: {
params: Promise<{ slug: string }>
searchParams: Promise<{ page?: string }>
}) {
const { slug } = await params
const { page } = await searchParams
return <Post slug={slug} page={page} />
}layout.tsxgenerateMetadata()references/nextjs-16-upgrade.mdreferences/server-components.mdreferences/server-components.mdreferences/client-components.md'use client'references/client-components.md'use client'references/streaming-patterns.mdreferences/streaming-patterns.mdreferences/react-19-patterns.mdreferences/react-19-patterns.mdreferences/server-actions.mdreferences/server-actions.mdreferences/routing-patterns.mdreferences/routing-patterns.mdreferences/migration-guide.mdreferences/migration-guide.mdreferences/cache-components.mdexperimental_ppr"use cache"experimental_pprcacheLife()cacheTag()revalidateTag()cacheComponents: truereferences/cache-components.mdexperimental_pprexperimental_ppr"use cache"cacheLife()cacheTag()revalidateTag()cacheComponents: truereferences/nextjs-16-upgrade.mdreferences/nextjs-16-upgrade.mdreferences/tanstack-router-patterns.mdreferences/tanstack-router-patterns.mdundefinedundefined
---
---childrenPromise.allgenerateStaticParamsPromise.allgenerateStaticParamsscripts/ServerComponent.tsxscripts/ClientComponent.tsxscripts/ServerAction.tsxscripts/ServerComponent.tsxscripts/ClientComponent.tsxscripts/ServerAction.tsx| Error | Fix |
|---|---|
| "You're importing a component that needs useState" | Add |
| "async/await is not valid in non-async Server Components" | Add |
| "Cannot use Server Component inside Client Component" | Pass Server Component as |
| "Hydration mismatch" | Use |
| "params is not defined" or params returning Promise | Add |
| "experimental_ppr is not a valid export" | Use Cache Components with |
| "cookies/headers is not a function" | Add |
| 错误 | 解决方法 |
|---|---|
| "You're importing a component that needs useState" | 添加 |
| "async/await is not valid in non-async Server Components" | 为函数声明添加 |
| "Cannot use Server Component inside Client Component" | 将服务端组件作为 |
| "Hydration mismatch" | 对Date.now()、Math.random()、浏览器API使用 |
| "params is not defined" 或 params返回Promise | 在 |
| "experimental_ppr is not a valid export" | 改用带 |
| "cookies/headers is not a function" | 在 |