Next.js Discord

Discord Forum

Random 404s with getStaticProps and On Demand Revalidation

Unanswered
Morgan posted this in #help-forum
Open in Discord
I'm using a combination of dynamic routes with getStaticProps and getStaticPaths, and getStaticProps on some non-dynamic pages.

When deployed, after a few days pages begin to 404 randomly.

When I inspect the network panel in Chrome devtools I see the json data files that get prefetched for each page are 404ing.

This seems to happen with any page that uses getStaticProps.

I've attached a video demonstrating what happens in the network tab when I repeatedly reload a simple index page (not dynamic).

Code for simple index page:

import { GetStaticProps, NextPage } from "next";
import { PageProps } from "@/model/pageProps";
import PageTemplate from "@/templates/PageTemplate";
import { fetchPageData } from "@/strapiLib/fetchStrapi";

const Home: NextPage<PageProps> = (props) => {
  return <PageTemplate {...props} />;
};

export const getStaticProps: GetStaticProps = async () => {
  const data = await fetchPageData("home");

  if (!data) {
    return {
      notFound: true,
    };
  }
  return {
    props: {
      components: data.attributes?.Components ?? null,
      pageMeta: data.attributes?.PageMeta ?? null,
      publishedAt: data.attributes?.publishedAt ?? null,
    },
  };
};

export default Home;


The code is deployed to Azure App Service.
Cloudflare is used for reverse proxy.

Any ideas as to why this is happening and how to stop it?

24 Replies

🦗
you can console.log the data after const data = await fetchPageData("home");; most likely it is falsy so the { notFound: true } branch is exexuted
That's a good shout, but I've had this code running in 3 separate environments, from the same endpoint, only errors like this in 1 of them.
If I restart the server this goes away, but comes back after a period of time.
that's really really weird, can you give me a minimal reproduction repository and the specified "period of time"?
unless you specified revalidate (which you didn't here), the page should work the same way a thousand years into the future
Can I PM you?
no. remove all business logic and irrelevant parts of your code, make a minimal repo and send it here
Let me know if this is what you wanted or not
@Morgan Let me know if this is what you wanted or not
Can you put it on github or a codesandbox or similar? I now know to not download random zip files on the internet
You asked me to send it "here", dont get your knickers in a twist
I'll create a private repo, what's your github user name?
No, make it a public repo. Well if you are not so fine with it, it’s ok, you can wait for assistance from other people
I have a pretty strict requirement for which post I will offer assistance in
nothing in it looks wrong at all, so it's quite weird...

to troubleshoot, instead of returning notFound: true, you use some alternate values for the props, e.g.
return {
  props: {
    components: null,
    pageMeta: null,
    publishedAt: null,
  },
};

then does the empty not-404 version show up after the few days?
I could possibly try that, but I want a notfound page to have a 404 statuscode
Could Azure / cloudflare possibly be contributing to the issue?
Just a shot in the dark
Thanks for looking at that
@Morgan Could Azure / cloudflare possibly be contributing to the issue?
no i don't think so, but it's not impossible either
@Morgan I could possibly try that, but I want a notfound page to have a 404 statuscode
yeah ik, but for now just to test whether it is really due to that notFound: true part
rather than azure/cloudflare bugging
ok