Exploring the caching capabilities of NextJS and Vercel for optimizing simple fetch

I have successfully deployed a NextJS (13.2.4) app to Vercel after testing it locally with great results. The issue I am facing is related to a simple fetch request from a component ('use client').

This request is directed towards a route within the /app directory, which in turn contains an API call to a third-party service.

While everything functions as expected on my local environment, the same cannot be said for Vercel. When I execute the fetch request on the deployed app, it initially retrieves the data properly. However, it seems to cache the response thereafter, failing to update with fresh data.

Although it appears to be refreshing the data and making the network call, the process is completed so quickly that no changes are reflected in the data (even though there should be).

Even after trying to include 'cache: "no-store"' in the fetch request, the problem persists.

If further details are required, I am more than willing to provide them.

Answer №1

After struggling all day, I finally found a solution to the caching issue with Next13 on Vercel and my Google API call.

A helpful answer that guided me in the right direction was provided by Youssouf Oumar:

How to avoid caching in the app directory of Next.js?

To delve deeper into this topic, check out this section in the documentation:

Revalidating Static Data

In production, Next13 tends to cache most fetch calls in the app folder, but adding ""{ cache: 'no-store' }"" to your fetch in the route.js function can prevent caching for plain fetches.

const response = await fetch('/agents', { cache: 'no-store' });

However, this approach didn't work for me since I was using the Google API SDK.

Ultimately, what resolved the issue for me was setting the revalidate variable to 0 in my route.js file.

// app/agents/route.js
import { NextResponse } from 'next/server'
import getAgentInfo from '../../lib/google-sheets/getAgentInfo.js';

export async function GET() {
  const agents = await getAgentInfo();
 
  return NextResponse.json({ agents })
}

export const revalidate = 0;

This simple change eliminated the caching problem in the specific page where I made the fetch, ensuring that I received up-to-date data (phew!).

I hope this information proves useful for you!

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

Real-time filtering in personalized dropdown menu with search input

I have been attempting to create a custom list filter for input using a computed property. In one file, I am developing a widget, while in another file, I am implementing it. Below is the code snippet from the widget creation file: <template> ...

How can I show the total sum of input values in a React.js input box?

Is there a way to dynamically display the sum of values entered in front of my label that updates automatically? For example, you can refer to the image linked below for the desired output Output Image I have initialized the state but I'm struggling ...

how can JavaScript be used to retrieve an object based on a condition from an array of objects and an ArrayList

My JavaScript challenge involves working with an array of objects called arrobj and a list called prgList. The goal is to extract the names from arrobj based on the programs listed in prgList. If the program exists in arrobj, it should be displayed accor ...

Struggling with preloading information in Next.js

I have set up a simple dummy API that provides an array of objects with dummy data: // api/projectsdata.js export default function handler(req, res) { res.status(200).json([{...},{...},{...}] } The fetch API I am using looks ...

Issue with Vue and Firebase data transmission

I am currently working on an app that tracks user visits, and I am using vue.js 3 and firebase for this project. During testing, I encountered an error message stating "firebase.database is not a function" when trying to send data to firebase. Can anyone ...

I want to trigger the opening and closing of an accordion by clicking on an arrow

I am encountering an issue with the Material UI accordion. When I click on the arrow, the accordion opens but clicking again does not close it. I would like to make it so that when the user clicks on the arrow, the accordion will toggle between open and cl ...

Tips on effectively organizing information retrieved from an XML file?

I would like to organize the data in ascending order based on the DATE after making this ajax call function parseXML(xml){ var weatherArray = []; var weatherElement = xml.getElementsByTagName("forecast")[0]; weatherArray.queryTime = weatherEl ...

The Ajax database browser page refresh feature updates the content without actually refreshing the entire page

I am encountering an issue with my AJAX and PHP database integration. I believe many novice Ajax programmers are facing a similar problem. Despite shortening the code, it remains lengthy so please bear with me. There's a button on the homepage index.p ...

What is the best way to ensure that the content container's max-width for a split tier is the same as the width of a full-width tier?

My goal is to create a split tier on a webpage with a 60/40 layout. The size of this tier should be calculated based on the maximum width of the container above it. Using JavaScript and jQuery, I managed to derive the max-width value of the full-width-tier ...

Find similarities between two JavaScript arrays using unique identifiers

Seeking a more efficient and streamlined approach in javascript to compare two arrays and generate a third one. We have the following two arrays : var array1 = [ [{ id: 1, enabled: false }], [{ id: 2, enabled: true, }], [{ ...

How can I create an onclick link within an alert message using JavaScript, even when the alert is not consistently displayed?

I am facing an issue with an alert that is supposed to appear under specific conditions on my website. The alert includes a link that should trigger some code when clicked. However, I keep getting an error message saying "ReferenceError: doTheThing is not ...

Generate a new item using an existing one

I am seeking to extract the desired output from the provided input: Input Configuration: var inputParams = { 'inputDetails' :[ { 'field' : 'specificationName', 'value' : 'strong'}, { ...

Transferring data from AJAX form in JavaScript to PHP function

My website features a login form that sends data, such as email address, to a PHP function and initiates the creation of a user. I am looking to include additional information on the page where the form is located. Here is the data I want to include in t ...

How can I use VueJS Cli to create a shared variable that is accessible across all pages and can be monitored for changes within a specific page?

Recently, I've delved into the world of VueJs and I'm encountering some issues with a project that I can't seem to resolve through online resources. I am trying to establish a variable that is common across all pages (month), and whenever t ...

Is it possible to create a Single-Page Application using Nextjs?

Recently, I have been exploring the capabilities of Nextjs and got intrigued by the idea of running a Single Page Application (SPA) in it. My particular requirement involves having two distinct sides to my application - one that is server-side rendered (SS ...

I encountered an issue where the error "data.map is not a function" occurs whenever I try to execute a React component

I have implemented the HorizontalScrollbar component and passed data as a prop. When I try to run the HorizontalScrollbar component, I encounter the following error in the console tab of Chrome: Error: Uncaught TypeError: data.map is not a function Horiz ...

Update the display using a button without the need to refresh the entire webpage

I am currently working on a website project that requires randomized output. I have successfully implemented a solution using Javascript, but the output only changes when the page is reloaded. Is there a way to update the output without refreshing the en ...

Locate the positions of 2 identification numbers within a Mongoose array

I am currently working on developing a middleware that validates if a conversation exists between two users in the database. If the conversation does not exist, the middleware will create a new conversation. I am attempting to utilize Mongoose's $in o ...

Shuffle letters in a word when hovered over, then unscramble them back to their original order

I have noticed a fascinating effect on several websites. To give you an idea, here are two websites that showcase this effect: Locomotive and TUX. Locomotive is particularly interesting to look at as the desired effect can be seen immediately when hovering ...

The JSON.stringify() method does not update the object that has been modified by an event

Below is the code snippet: //function triggered when columns are reordered dataTable.on('column-reorder', function (e, settings, details) { var userData = tableWidget.grid('userData'); console.log(userData); //userData object s ...