I noticed that when using Next.js with the `revalidate: 1` option on a static page, it is triggering two full F5 refresh actions instead of just one. I was hoping for

Currently, I have set up a blog post edit page in my Next.js project.

The post pages are utilizing the Incremental Static Regeneration feature with a revalidation time of 1 second for testing purposes. In the future, I plan to increase this to a revalidation time of 300 seconds (5 minutes).

When updating a post, here is my process:

As an admin, I navigate to /admin/post/edit/my-post-slug, make any necessary changes, and then save the post.

After saving, I use the route.push method to move to the post page and view the updated version at /post/my-post-slug.

Here is the code snippet:

const savePost = async (post: BlogPost, router: NextRouter) => {
  await savePostAPI(post);
  const POST_ROUTE = "/post/my-post-slug";
  router.push(POST_ROUTE);
};

Everything functions as intended, but there is a small issue.

This is the sequence of events that I experience:

- I'M ON ADMIN POST EDIT PAGE AND I SAVE
- I GO TO POST PAGE VIA router.push() AND I SEE THE OLD CONTENT (THIS IS EXPECTED)
- WAIT A LITLE
- I HIT REFRESH (F5) ONCE AND I RELOAD THE PAGE AND STILL SEE OLD CONTENT (NOT EXPECTED)
- WAIT A LITLE
- I HIT REFRESH (F5) AGAIN AND SEE THE NEW CONTENT

It's acceptable to see the old content initially after using router.push() since Next.js caches content and serves stale versions before generating new ones. However, why do I need to refresh the page twice to see the updated content? Shouldn't router.push initiate a server request to regenerate the page, considering it is already stale due to the revalidation setting of 1 second? Why does this double refresh occur?

Instead of using router.push(), would it be better to employ

window.location.href = "https://www.example.com/post/my-post-slug"
to ensure the request is sent and triggers the regeneration of the page?

Answer №1

Upon digging deeper, I can now verify.

router.push() carries out a client-side transition, which does not prompt a new getStaticProps request. Instead, it simply retrieves a previously generated page's JSON object with properties.

In other words, this indicates that regardless of how many client-side transitions you execute, the page will not undergo revalidation.

To obtain fresh data, utilize client-side code or opt for periodic full page reloads. A minimum of 2 reloads are required. The initial one will deliver stale data and initiate the revalidation process, while the second will retrieve updated information.

Answer №2

Yes, according to the Next.js documentation, it seems that two page reloads are required for incremental static regeneration, which can be frustrating and not user-friendly:

After a 10-second window, the next request will still display the cached (stale) page Next.js initiates a background regeneration of the page.

My workaround for this issue is to utilize ISR in conjunction with SWR, as recommended by Next.js and Vercel. This approach ensures that the data remains up-to-date even during normal routing without the necessity of full page refreshes.

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

Struggling with this npm i problem?

I have tried multiple solutions, but I keep encountering an issue with npm install. I even deleted the package-lock file and attempted using the --force or --legacy-peer-deps option without success. npm WARN deprecated <a href="/cdn-cgi/l/email-protecti ...

How to Pass a JSON Object to a Child Component in Angular and Display It Without Showing "[Object

Need help with my API call implementation. Here's a snippet from my Input component: Input.html <form (submit)="getTransactions()"> <div class="form-group"> <label for="exampleInputEmail1"></label> <input type="t ...

Issues relating to the total count of pages in the uib-pagination component of AngularJS

While there is a previous thread discussing a similar topic Calculating total items in AngularJs Pagination (ui.bootstrap) doesn't work correctly, it does not address my specific issue and I am unsure how to contribute to it. We are using a complex s ...

Retrieve the initial value from the TextField

My website features multiple filters, including by date and duration, allowing users to easily find the information they need from a large dataset. There is also a "reset all filters" button that clears all filters and displays the full list of products. ...

The functionality of the React Material-UI menu anchor is disrupted when interacting with a react-window

In the project I am working on, I have encountered an issue with the Material-UI and react-window integration. Specifically, the material-ui menu component does not anchor properly when placed within a virtualized list created with react-window. Instead of ...

Vue.js - Capturing a scroll event within a vuetify v-dialog component

Currently, I am working on a JavaScript project that involves implementing a 'scroll-to-top' button within a Vuetify v-dialog component. The button should only appear after the user has scrolled down by 20px along the y-axis. Within the v-dialog, ...

Unable to display information from a repeated `textarea` in ngRepeat

Check out my code on Plunker: https://plnkr.co/edit/rBGQyOpi9lS0QtnCUq4L I'm trying to log the content of each textarea when typing, using the printStuff() function: $scope.printStuff = function(customize, item) { console.log(customize[item.inde ...

Steps to dynamically populate a datatable with JSON data by triggering a click event in jQuery

I am facing an issue with feeding JSON data into datatables when the search button is clicked. The JSON data structure is as follows: [ { "port_code":"BOM", "cont_details_id":"9", "price":"44.000", "cont_price":"500", "c ...

Error with Cross-Origin Resource Sharing (CORS) on my website

During the development of a website, I disabled web security in order to bypass CORS using the command chrome.exe --disable-web-security --user-data-dir=/path/to/foo However, after successfully completing the website and uploading it to my domain, I enco ...

Using Vue to fetch JSON data with Axios

When trying to retrieve user data from a MongoDB in JSON format using axios.get within a Vue.js application, my aim is to visualize this data by iterating through all user objects in the users array. The issue I'm facing is that each character is trea ...

Issue with the Material UI theme module enhancement feature not functioning as expected

I've been researching the MUI documentation, blogs, and various posts on Stackoverflow, but despite my efforts, I can't seem to get my vscode intellisense/typescript to recognize the changes I've made. These are fairly straightforward modif ...

Encountered an error while attempting to load module script

Upon launching an Angular application on Heroku, a situation arises where accessing the URL displays a blank page and the console reveals MIME type errors. The error message reads: "Failed to load module script: The server responded with a non-JavaScrip ...

I pressed the button but the globalCompositeOperation isn't working as expected. How can I make it function correctly to achieve the desired output like the second canvas?

<!DOCTYPE html> <html> <head> <title>Canvas Compositing Tutorial</title> </head> <body> <canvas id="myCanvas" width="200" height="200" style="border: 1px solid"></canvas> <br> <butt ...

Looping through a dynamic array in Vue.js

I am working with two arrays: days:[0,1,2,3,4,5,6] and wdays:[2,3,6] My goal is to iterate through both arrays and display the output as follows: 0 : not present 1 : not present 2 : present 3 : present 4 : not present etc... The implementation should be ...

The text alignment in the Material-UI Paper component is not centralized

Hello everyone, I am new to React Material-UI and I'm facing an issue with the paper component. The text within the paper does not center properly when the height is low. Can someone provide guidance on how to fix this? If you check out the sandbox l ...

Utilizing a file type validator in SurveyJs: A guide

Is there a way to validate uploaded documents on a form using surveyJs and typescript with a custom validator before the file is uploaded? The current issue I am facing is that the validator gets called after the upload, resulting in an API error for unsup ...

Angular 2 does not recognize the existence of .then in type void

I have a query regarding Angular2 and I'm struggling with the void function in my code. Can someone help me out? I am new to Angular2 and unsure of what needs to be added in the void function. Check out this image for reference export class PasswordR ...

Is the popup not opening with just one click?

https://i.stack.imgur.com/09dcf.png Upon clicking the first input cell, the popup opens and closes as expected. However, when closing the initial input and opening another one, an orange mark icon appears but the popup doesn't open until the second c ...

Encountering issues compiling a Next.js project using hooks

I recently created a straightforward Next.js project with just one page, index.js, which looks like this: import React, { useState } from 'react'; const MyComponent = () => { const [instance, setInstance] = useState() return ( ...

Should an HTML canvas in Angular be classified as a Component or a Service?

I have a basic drawing application that uses an MVC framework in TypeScript, and I am looking to migrate it to Angular. The current setup includes a Model for data handling, a View for rendering shapes on the canvas, and a Controller to manage interactio ...