Utilize useSWR to trigger a new fetch upon page initialization even when the key is stored in the cache within Next.js

Currently, I am utilizing the useSWR hook to retrieve data from my api endpoints. It is my understanding that it uses the api routes as the key for caching the data.

The issue at hand is that I have implemented an api route on 2 separate pages (referred to as Page1 and Page2), each with distinct queries. When I visit Page1 first and then navigate to Page2, it displays the cached version of Page1, and vice versa.

I attempted to utilize the mutate function on both pages, but it only displays the cached version before revalidating. This is not the desired outcome. My goal is to display the correct data upon page mount.

Furthermore, I tried setting the revalidateOnMount flag to true in the SWRConfig options within the _app.js file like this:

...
   <SWRConfig value={{ revalidateOnMount: true}}>
      ...
    </SWRConfig>
...

Unfortunately, this approach did not work as expected.

It's worth noting that I am also receiving initialData from getStaticProps functions on both pages.

Any assistance or guidance would be greatly appreciated. I have been struggling with this issue for quite some time now.

Answer №1

Highlighting @juliomalves' observation, it is possible to transmit numerous arguments to the hook which together serve as the unique identifier for the cached information.

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

Mapping an array using getServerSideProps in NextJS - the ultimate guide!

I'm facing an issue while trying to utilize data fetched from the Twitch API in order to generate a list of streamers. However, when attempting to map the props obtained from getServerSideProps, I end up with a blank page. Interestingly, upon using co ...

The Next.js dynamic route in production is displaying a 403 error instead of the expected 404. What might be causing this issue?

Whenever I attempt to access https://site.con/categories/, I am greeted with a 403 Forbidden error. However, if I visit https://site.con/categories/sport, everything works perfectly fine, and all other routes function properly. What could potentially be ca ...

Is there a way to position the Image component from next/image using absolute positioning?

Is it possible to use an element Image from 'next/image' with the CSS style { position: absolute; left: 50% }? It appears that it is not being applied. For example: import React from 'react' import Image from 'next/image' imp ...

The upcoming development server will be shutting down on its own

After setting up a basic next.js application, both with the nextjs-blog starter and a generic nextjs application, I encountered an issue when running "npm run dev." The command seemed to hang for a moment before abruptly closing. Visiting localhost:3000 on ...

How to resolve hydration error in Next.js: Issue - Server-rendered HTML content does not match the text content

What should be done when the client has additional or different information than the server? For example, reading data from localStorage and displaying it. The content is different, so why does this hydration error occur? Error: Text content does not matc ...

Encountering a PropertyTypeError while attempting to process a payment via Stripe in conjunction with use-shopping-cart on Next.js

Upon reaching the checkout page, I encounter the message: Invalid value with type "undefined" was received for stripe. Valid type for stripe is "string". This issue seems to be related to the redirectToCheckout function. Can someone assist me? The cart-s ...

When implementing variables from input boxes, my SQL query fails to populate any data in the database table

I have been using phpMyAdmin to store test data. As I try to insert data from a form, I encounter an issue where no data gets inserted when using variables in the SQL query. Being new to coding, I am struggling to find a solution to this problem. Additiona ...

How can I stop TypeScript from causing my builds to fail in Next.js?

Encountering numerous type errors when executing yarn next build, such as: Type error: Property 'href' does not exist on type '{ name: string; }'. This issue leads to the failure of my build process. Is there a specific command I can ...

`Need help with adding meta tags to your Next JS metadata?`

I've been attempting to include a meta tag in Next JS 13.4, but so far I haven't had any success. Despite going through the documentation and conducting thorough searches online, I still haven't found a solution. My goal is to add this parti ...

No solution was found for implementing Airbnb TypeScript in React.js (Next.js) using ESLint

screenshot I encountered an issue where I couldn't locate the Airbnb typescript option in React JS (Next JS) within ESLint. Prior to this, I had installed Storybook and mistakenly clicked "yes" when prompted to migrate ESLint to Storybook. ...

What could be causing the issue with typing into the Formik and Yup input fields?

Whenever I attempt to enter text into the input fields, no characters show up. Strangely, a similar login page using the same code is functioning perfectly fine. At this moment, I'm baffled as to why it isn't working on this specific one. My susp ...

What is causing ESLint errors not to be displayed in the browser when they occur?

Recently, I noticed that ESLint errors are no longer appearing on the browser screen while I develop my Next.js (v14) app. Despite having several ESLint rules in place, they do not display on the screen like they did when using CRA. I prefer it when the a ...

When the back button is clicked, pagination returns to number 1

I'm currently working on implementing Pagination in ReactJS and facing an issue where the pagination resets to page 1 when I navigate away from the current page and then come back. Ideally, I would like to resume from the same page where I left off. H ...

What steps should I take to resolve the NextRouter "not mounted" error when deploying my Next JS 13 app on Vercel

I am encountering an issue while deploying my Next.js 13 app. The error message states: Module not found: Can't resolve 'encoding' in '/vercel/path0/node_modules/node-fetch/lib' Additionally, I am facing a "Next Router not mounte ...

How to access the dynamic route's path in Next.js when using Server Components?

Obtaining the dynamic route path in a Next JS server component poses a challenge. This task is simple when working with client components. If you are working on src/app/[id]/page.tsx "use client"; import { usePathname } from "next/navigatio ...

Using Next.js to Retrieve JSON Data and Render it in Components

I want to refactor the MainMenu function and getStaticProps from index.js into separate components. Here is my current index.js page that is functioning correctly. #index.js import Link from 'next/link'; function MainMenu({ menuLists }) { re ...

Unable to see Next.js page in the browser window

I have set up a next.js project with App Router and my file structure under the app folder looks like this: *some other files* . . user | [id] | | page.tsx | @users | | page.tsx | layout.tsx | page.tsx I am ...

Trouble with Next.js loading files efficiently

Exploring the realm of Next.js, I find myself diving into the world of JavaScript. This is where my directory resides. https://i.stack.imgur.com/GLEWR.png Within the 'counter-state.js' file: I have implemented the following code: import {useSt ...

Setting up Next.js 13 with GraphQL and Apollo Configuration

I am currently working on configuring a graphql frontend application with nextjs13. However, I have encountered an issue with the new folder structure as there is no longer an _app.tsx file. I am now unsure of how to proceed with setting it up. Previously ...

Creating a fetcher that seamlessly functions on both the server and client within Nextjs 13 - the ultimate guide!

My Nextjs 13 frontend (app router) interacts with a Laravel-powered backend through an api. To handle authentication in the api, I am utilizing Laravel Sanctum as suggested by Laravel for SPAs. This involves setting two cookies (a session and a CSRF token) ...