我正在按照此文档https://nextjs.org/docs/app/building-your-application/data-fetching/incremental-static-regenesis来创建ISR应用程序。它说使用
fetch
来缓存文档中的数据。但是,我在某些情况下数据不是来自http服务器。相反,数据可以来自数据库查询、graphql 服务器等。有没有一种方法可以在不使用 fetch
的情况下使用 ISR?我正在寻找一些可以用来包装获取逻辑的包装函数。
您可以使用
unstable_cache
引自doc:
如果您正在使用ORM或连接到数据库,则可以使用unstable_cache:
来自同一文档的示例代码:
import { unstable_cache } from 'next/cache'
import { db, posts } from '@/lib/db'
const getCachedPosts = unstable_cache(
async () => {
return await db.select().from(posts)
},
['posts'],
{ revalidate: 3600, tags: ['posts'] }
)
export default async function Page() {
let posts = getCachedPosts()
// ...
}