Skip to content

Next.js 缓存组件

<CacheComponents> 是 Next.js 16 的实验性功能,用于控制组件级别的缓存行为,替代旧的 unstable_cache

基本用法

tsx
import { cache } from 'react'

const getData = cache(async (id: string) => {
  return await db.post.findUnique({ where: { id } })
})

// 多次调用只执行一次
const a = await getData('1')
const b = await getData('1')  // 从缓存取

与 fetch 缓存配合

fetch() 在 RSC 中默认缓存,可通过 cache: 'no-store' 禁用。