The data in Next.js getStaticProps does not update or refresh as expected

As I recently transitioned to working with Next.js, I have encountered several challenges. One issue that arose was related to the use of useEffect in React compared to its behavior in Next.js.

In my index file, I have implemented the following code:

import Link from "next/link";
import React, {useState, useEffect} from "react";
import { useRouter } from 'next/router';

export async function getStaticProps({ res }) {
    try {
        const result = await fetch(`https://api.pandascore.co/matches/running??sort=&page=1&per_page=10&&token=#`);
        const data = await result.json();

        return {
            props: { game: data },
            revalidate: 10 // 10 seconds 
        };
    } catch (error) {
        res.statusCode = 404;
        return { props: {} };
    }
}

const upcomingGames = ({ game }) => {

    return (
            <div className="container">
                <h2>Live Games - </h2>
                <div className="columns is-multiline">
                {game.map(q => (
                    <div className="column is-half" key={q.id}>
                        <div className="inner">
                        <div className="inner__box">
                     <Link href={'/live/' + q.slug}  key={q.slug}>
                       <a className="h2link" key={q.slug}>{q.name}</a>
                  </Link></div>
                  </div>
                  </div>
                ))}
                </div>
            </div>
    );
}

export default upcomingGames;

The above component is linked to a [slug].js file which provides additional details about a specific game. However, after deploying the application on Vercel, I noticed an issue where newly added games were not accessible when clicking on them, leading to a fallback page (404).

To address this issue, I made modifications to the code as follows:

Code for getStaticPaths and getStaticProps functions...

This adjustment resolved the problem, ensuring that newly added games could be accessed without encountering any errors.

Answer №1

While in the development phase with next dev, the function getStaticPaths is called on every request. However, when moving to production, it will only be invoked the next time you execute next build. This means that if a new game is added to the API, the paths linked to ${some_new_game_slug} will not exist until you run next build again, requiring a re-deployment. If this data frequently changes, consider using getServerSideProps for [slug].js or exploring client-side data fetching as an alternative approach.

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 causes a user to log out when the page is refreshed in a React/Redux application?

Each time the page is reloaded in my React/Redux application, the user gets logged out, even though I have stored the token in localStorage. There seems to be an error somewhere. The token should ideally be saved when the user logs in and not lost upon rel ...

Cross-building targets for Node modules for deployment on npm platform

I'm working on an ES6 module that needs to be compatible with different environments. I'm not sure whether to use webpack or rollup for building, and how to target specific environments. Building for Different Environments ES6 environments like ...

Encountering a Node.js error when executing the JavaScript file

When I try to run my Node.js application using the command "node main.js" in the terminal, I encounter an error saying "Error: Cannot find module 'D:\nodeP\main.js'". This is confusing for me as I have set the environment variable path ...

Troubleshooting Issue with Nested ng-include in AngularJS: Difficulty arises when the parent element with the ng-include attribute is dynamically added using the ng-

Click here to see a demo plunker that will help you understand my issue. On my main page, I have a table. Each table row is followed by a hidden empty row. When the first row is clicked, I use a directive to inject HTML into the empty row below it. Main ...

Jasmine secretly observes the behavior of Angular services

I'm currently in the process of setting up unit tests for an angular controller using jasmine and karma. Due to the length of my code, I won't be able to include it all here. However, I'll provide some snippets: CompilerController.js (the c ...

I'm caught in a never-ending cycle as I navigate through sending information to my Python API and subsequently utilizing that information in the Next.js application to execute the Python script

I am encountering an issue with the response message, getting "Error sending data." The problem seems to arise when trying to retrieve data in server.py that is already specified in the code. For example, instead of using "msg_user", I should use ...

The cloud function in Firebase was unable to connect to the dialogflow route

I am currently working on creating a chatbot using Dialogflow integrated with OpenAI in Firebase Cloud Function. However, I am facing an issue where I cannot access the /dialogflow endpoint and keep receiving the error message: "Cannot GET /dialogflow". My ...

How to include a javascript file in a vuejs2 project

Just starting out with the Vue.js framework and I've hit a snag trying to integrate js libraries into my project. Would greatly appreciate any assistance! By the way, I attempted adding the following code to my main.js file but it didn't have th ...

What is the process for storing user-provided document names in Firestore database entries?

I'm encountering an issue with my API while trying to utilize Firestore for inputting data for login and registration via the API. The problem arises when attempting to add a document entry in the database with the user's input email during regis ...

Unable to utilize angular-local-storage

Here is the code snippet I'm working with: angular.module('MyModule').controller('MyController', ['$scope', '$stateParams','$location', '$http','LocalStorageModule', function($s ...

When creating a schema in Sanity, how can you define a field that allows for unknown properties to be specified?

I am currently adding sanity to a NextJS application. I have encountered an issue while defining the schema type for a project. I would like to include a field called features, which are unique in that I do not know beforehand what they will be. For instan ...

What could be causing my middleware to fail in safeguarding routes?

I am currently working with next.js and attempting to implement middleware. My goal is to restrict access to certain pages, such as the profile page, if the user does not have a valid token stored in their cookies. However, I seem to be encountering an iss ...

Display Numerous Values Using Ajax

Having difficulty showing data from the 'deskripsisrt' table in a modal bootstrap? I have successfully displayed from the 'srtptr' table, but not sure how to proceed with the 'deskripsisrt' table. Here's a snippet from my ...

What is the method to select a hyperlink that includes a variable in the "href" attribute and click on it?

Currently, I am in the process of creating acceptance tests utilizing Selenium and WebdriverIO. However, I have encountered a problem where I am unable to successfully click on a specific link. client.click('a[href=#admin/'+ transactionId + &apo ...

Converting JSON information into a JavaScript array of objects

I have a JSON file containing placeholder articles for testing purposes. I'm using jQuery to parse the data from the JSON file and create an array of objects with the retrieved information. Take a look at my JSON file: { "news": [ { ...

Utilizing ng-model to control the visibility of a label or anchor tag

Here is the code snippet I am working with: <div class="tabs DeliveryRightDiv"> <label class="selected"><a>One</a></label> <label> <a>Two</a> </label> <label> ...

The autocomplete feature in Google Maps is not functioning properly when used within a modal

I've integrated the Google Maps API to enable autocomplete address functionality for an input box on my page. However, I'm encountering an issue where everything works fine on the page itself, but it doesn't work when the input box is placed ...

Using Selenium Webdriver with Node.js to Crop Images

Currently, I am using selenium-webdriver in combination with nodejs to extract information from a specific webpage. The page contains a captcha element from which I need to retrieve the image. Unfortunately, all the code snippets and solutions I came acros ...

What causes queryAsync() to generate additional metadata?

Following the instructions provided in a response to a question, I utilized queryAsync() and it is functional. However, it is appending excessive meta data to my query result, which was initially a simple query. This is the code snippet I am using to exec ...

When exporting an enum in StencilJS with TypeScript, the error "Cannot find name..." may occur

Looking for a solution: https://github.com/napolev/stencil-cannot-find-name In this project, there are two main files to consider: custom-container.tsx import { Component, Element, State } from '@stencil/core'; @Component({ tag: 'cu ...