App Router 里怎么取数?fetch 的缓存策略有哪些?
App Router 的 Server Component 里可直接 await fetch,Next 自动按请求 memoization + 路由级缓存处理:
async function getData() {
const res = await fetch('https://api.x.com/data', { next: { revalidate: 60 } }) // ISR:60s 再生
return res.json()
}
export default async function Page() {
const data = await getData() // 同一请求在渲染中只取一次
return <List data={data} />
}缓存选项(next 字段):
{ cache: 'no-store' }:每次请求都拉最新(等同 SSR){ next: { revalidate: 60 } }:ISR,60 秒再生{ next: { tags: ['post'] } }+revalidateTag('post'):按 tag 按需失效
Pages Router 用
getStaticProps/getServerSideProps;App Router 统一用 async 组件 + fetch 的next配置。