Next.js 通过手动缓存重新验证(按需)

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

总结

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

结构,如果这很重要的话

    

javascript caching next.js
1个回答
0
投票
@sanity/client

正式

支持Nextjs的缓存选项
。我对此进行了测试,它的工作原理与本机 fetch 的工作方式完全相同。
    

© www.soinside.com 2019 - 2024. All rights reserved.