1

I've jus started working and learning Next so I have a lot of confusions,

I was using the useEffect on react and it always updated the UI with the new stuff that was added to the API however, its not working on next.js

SO I have an index file

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;

This file is connected to a [slug].js file which displays more details about a game,

Now in production when I deployed the app to vercel I have noticed that when a new game is added to the API it displays in the index.js but when I click on it I'm redirected to a fallback(404) page.

After I redeploy my project this is fixed, however every time a new game is added and rendered I'm unable to access its individual page which I defined in [slug].js

export const getStaticPaths = async () => {
    const res = await fetch(`https://api.pandascore.co/matches/running?sort=&page=1&per_page=50&token=#`);
    const data = await res.json();

    const paths = data.map(o => {
        return {
            params: { slug: o.slug.toString() }
        }
    })
    return {
        paths,
        fallback: false
    }
}


export const getStaticProps = async (context) => {
    const slug = context.params.slug;
    const res = await fetch(`https://api.pandascore.co/matches/running?search[slug]=${slug}&token=#`);
    const data = await res.json();
    console.log(data)

    return {
        props: {
            game: data
        }
    }
}

export default function live({ game }) {
    return (
        <div className="container">
            <h2> Single Game deets.</h2>
            {game.map((g) => (
  
  
  
  
  <div className="container" key={g.id} >


    <div className="inner-box" key={g.slug}>
      {/** Fetch team and display their corresponding score - A bit of code repition :( */}
      <div className="score-board-min columns is-mobile is-multiline">
        <div className="column is-full"> {g.opponents.slice(0, -1).map((o) => <span className="team" key={o.id}>{o.opponent.name}</span>)}
          {g.results.slice(0, -1).map((res, i) => (
            <span className="scores" key={i}>{res.score}</span>
          ))}</div>

        <div className="column">
          {g.opponents.slice(-1).map((o) => <span className="team" key={o.id}>{o.opponent.name}</span>)}
          {g.results.slice(-1).map((res, i) => (
            <span className="scores" key={i}><div>{res.score}</div></span>
          ))}

        </div>
      </div>
      <br />

      <div className="lower-box columns is-multine">

        <div className="column is-half">

          <div className="dark"><span className="is-pulled-left">League</span>  <span className="is-pulled-right">{g.league && g.league.name}</span></div>
          <div className="dark"><span className="is-pulled-left">Game:</span>  <span className="is-pulled-right">  {g.videogame && g.videogame.name} </span></div>
          <div className="dark alt"><span className="is-pulled-left">Tournament</span>  <span className="is-pulled-right"> {g.tournament && g.tournament.name} | </span></div>
          <div className="dark"><span className="is-pulled-left">Series</span>  <span className="is-pulled-right"> {g.serie.full_name} | {g.serie.tier.toUpperCase()} </span></div>
          <div className="dark alt"><span className="is-pulled-left">Teams</span>  <span className="is-pulled-right">   {g.opponents.map((o) => o.opponent.name).join(" vs ")}  </span></div>
     

        </div>

  


      </div>




      <br />
    </div>


  </div>
))}
            


        </div>
    )
}
juliomalves
  • 42,130
  • 20
  • 150
  • 146
Kentrell Jr
  • 197
  • 1
  • 15
  • You need to use `fallback: 'blocking'` in `getStaticPaths` to make new games available. `fallback: false` does not allow new pages to be added. See [How to add new pages without rebuilding an app with +150k static pages?](https://stackoverflow.com/a/66037031/1870780). – juliomalves Apr 07 '22 at 19:02

1 Answers1

4

During development (next dev) getStaticPaths gets called on every request, but for production it only gets called the next time you run next build. So when a new game is added to the API, the paths named after ${some_new_game_slug} won't exist until you run next build again, i.e., re-deploy. If this type of data changes frequently, you might have to use getServerSideProps for [slug].js as well (so no static paths) or opt for the client-side data fetching approach.

Mark G
  • 1,868
  • 1
  • 6
  • 15