Issue with Implementing On-Demand Revalidation in Sanity CMS
While NextJS offers the convenient feature of on-demand revalidation, I am facing a challenge when trying to integrate it with my Sanity CMS.
The problem lies in adding tag revalidation to Sanity, which does not utilize the fetch
method. Despite following the recommended practice of caching data using React's cache
function as outlined in the documentation, I'm still unable to achieve the desired outcome.
Below is an example of one of the functions I have attempted to modify:
// 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;
}
);
I've tried different methods logically, but none seem to work:
// 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
Just a note that I am utilizing the new /app
structure for my project.