NextJS 有一个名为“按需重新验证”的简洁功能,我想使用它,但我有一个问题。 我使用 Sanity 作为我的 CMS,并且 Sanity 有自己的客户端来获取数据。但是如何向 Sanity 添加标签重新验证,它不使用
fetch
?
按照文档的建议,我能够通过将 API 函数包装在 React 的
cache
函数中来缓存数据。
这是上述功能之一的示例:
// sanity/api.ts
export const getPosts = cache(
async (params?: GetPostsParams): Promise<Post[]> => {
const { labKey } = params || {};
const query = `...`;
const posts = await sanity.fetch(query);
return posts;
}
);
从逻辑上讲,我应该能够做这样的事情,但显然这些不起作用:
// sanity/api.ts
export const getPosts = cache(
async (params?: GetPostsParams): Promise<Post[]> => {
const { labKey } = params || {};
const query = `...`;
const posts = await sanity.fetch(query, { next: { tags: ['posts'] }); // this doesn't work
return posts;
}
, { next: { tags: ['posts'] }); // this doesn't work either
PS:我正在使用新的
/app
结构,如果这很重要的话