Revalidate with manual caching in Next.js allows for refreshing data only when needed by manually storing

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.

Answer №1

Currently, the @sanity/client library has officially added compatibility for Nextjs' caching features as documented in their latest release notes. Upon testing this feature, I can confirm that it operates similarly to a traditional fetch request.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What's the best location to input a script that will trigger instantly when a DropDown value is altered?

In my web application built on asp.net using vb.net, I have encountered an issue on a UI Screen where a drop down triggers a post back on onSelectedIndexChange. The dilemma at hand is that I need to prevent user interaction with the entire UI when onSelec ...

Utilizing Ajax for sending information from the view to the controller in Laravel

I am currently facing an issue with passing two variables to my controller using ajax. Despite no errors being thrown, the data arrives as null when accessed in the controller. Web.php Route::get('/donate/select-card', 'CardController@choo ...

Tips on preventing jquery ui tooltip from appearing within a child div

Is there a way to include a tooltip only in the parent div and not in its child div? I am currently using jQuery UI tooltip like this: $(function () { $("#parent_div").tooltip(); }); This is how the HTML looks: <div id="parent_div" title="Hello ...

Guide to generating a continuous flow of numbers in real time with Node.js using sockets, followed by capturing and displaying the data on an HTML page

What is the best method to generate an endless stream of numbers using nodejs and web sockets (preferably socket.io) on a backend server, then display them on an HTML page using JavaScript? ...

Unable to modify the title property of a link

Currently, I am in the process of transforming my website to support multiple languages. To achieve this, I have implemented a function using jQuery that dynamically changes text when a language button is clicked. While this method has been successful for ...

JavaScript: Preventing Duplicate Keystrokes in Keyboard Input

Currently, I am working on a snake game project as part of my JavaScript course. I have encountered an issue where the game registers a collision when two keys are pressed simultaneously, even though there is no visual collision. https://i.sstatic.net/j42 ...

The ActivatedRoute.routeConfig object appears to be empty in an Angular 2 project built with Angular-cli

Two projects I've created using angular-cli are working perfectly fine. However, in one of them, the routeConfig is showing as null and I can't figure out what's causing this issue. Both projects have identical package.json files, so there ...

Challenges with personalized music player

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <style type="text/CSS"> #custom{ font-family: monospace; font-size: 16px; max-width: 650px; ...

Guidance on exporting an Excel file from an HTML table, excluding the final row, using JavaScript

I'm able to export an Excel file from an HTML table, but I'm facing an issue where the last row (tr) meant for pagination on the screen is also being included in the exported result. How can I exclude this row from showing up in the Excel file? ...

There is a problem with the "loader" property in Next.js Image that is causing width implementation issues

Here is my implementation of the Next.js Image component: ... Cell: ({ value }: CellType) => ( <Image priority src={value} loader={() => value} className={''} height={100} width={100} alt={'profile_pic ...

"I am interested in using the MongoDB database with Mongoose in a Node.js application to incorporate the

I am facing a situation where I need to validate the name and code of a company, and if either one matches an existing record in the database, it should notify that it already exists. Additionally, when receiving data with isDeleted set to true, I want to ...

Unable to retrieve the previous state data in AngularJS upon clicking the back button in the browser

Currently, I am developing a web application using AngularJs. One issue that I am facing is related to filtered data. After applying specific parameters through checkboxes and radio buttons to filter the data, I navigate to another state. However, when th ...

I'm experiencing a strange issue where my component's values remain unchanged even after re-rendering the component with new values. I wonder if this could be a result of Next.js caching

Perhaps the title didn't fully capture what I'm trying to explain, so here's a breakdown. I'm in the process of developing a habit tracker. This tracker enables users to create their own habits which are stored in a habits mongodb tabl ...

Numerous asynchronous AJAX requests accompanied by various loading indicators

My ASP.net MVC page is utilizing ajax calls to load data for each of the six divs. To handle the loading indicator, I have created two methods as shown below. $("#divId").loading(); $("#divId").stopLoading(); Here is an example of one of my ajax calls ( ...

"Enhancing user experience with advanced checkbox filtering in Django using JavaScript

For filtering my results on an HTML page, I am attempting to use checkboxes. I pass my data in a list through a Django view, and then iterate through it using a loop like this: </div> {% for r in rows %} <div class="res_show"> ...

Display the output of an array in JavaScript within the same document

I am struggling to print the contents of a JavaScript array data: [ JSON.parse(genreCanvas.dataset.count).forEach(count => { `${count},\n`; }) ], to achieve the following result: data: [ 1, ...

What is the best method for designing a filtering menu with a "Narrow By" option?

Looking to create a sidebar menu similar to the functionality on mcmaster.com. This specific feature allows users to efficiently filter products and toggle through different options dynamically. Upon selecting the "metric" option, the entire page adjusts t ...

Animated text in ThreeJS

I'm interested in finding a way to animate text in ThreeJS that doesn't involve placing it directly on a plane. My ideal scenario would be to have the text appear as 2D, floating above a model. I've experimented with using divs positioned ou ...

Why is it that setTimeout was only executed once?

How can I ensure that the function NotifyMe() is only executed once for a timeout? $(document).ready(function(){ var mydata = []; $.ajax({ url: '3.php', async: true, dataType: 'json', success: function (json) { mydata = ...

Nodejs is utilized to alter the data format as it is transferred from the client to the server

I'm encountering an issue with transmitting my data to the server using Node.js. I have a feeling that there are discussions on this topic already, but I'm unsure of what to search for to locate them... Here's a brief overview of my applica ...