如何在 vercel 中使用 ISR 而不使用 fetch 函数?

问题描述 投票:0回答:1

我正在按照此文档https://nextjs.org/docs/app/building-your-application/data-fetching/incremental-static-regenesis来创建ISR应用程序。它说使用

fetch
来缓存文档中的数据。但是,我在某些情况下数据不是来自http服务器。相反,数据可以来自数据库查询、graphql 服务器等。有没有一种方法可以在不使用
fetch
的情况下使用 ISR?我正在寻找一些可以用来包装获取逻辑的包装函数。

node.js next.js vercel
1个回答
0
投票

您可以使用

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()
  // ...
}
© www.soinside.com 2019 - 2024. All rights reserved.