Next.js Discord

Discord Forum

Issues with getStaticPaths and revalidate() - getStaticPaths is called to late?

Unanswered
Sage Grouse posted this in #help-forum
Open in Discord
Sage GrouseOP
Hello everyone!

I work on a project that looks a bit like a CMS, so you can create some pages (/foo, /bar, etc..) and then, everytime you add or update a page I am using revalidate to rebuild only those pages.

Obiouvsly for those pages to be available, I also have a generic Next page (let's call it [...generic].ts and this page is using the getStaticPaths() hook to provide all the existing paths (/foo, /bar, etc).

The thing is I have a little timing issue. Let's say I want to edit the /foo page.

1. once I am done with my editing, I will call my api which calls revalidate() with the right paths to rebuild (here, it's gonna be ["/foo"]
2. revalidate does it things BUT it fails revalidating /foo : Failed to revalidate /foo: Invalid response 404
3. after the revalidate failed, a console.log inside my getStaticPaths is triggered and I clearly see "/foo"

So my issue is, I am currently trying to revalidate first and then getStaticPaths is triggered. So obviously for the revalidate point of view, /foo does not exist yet.
I am pretty sure it's the issue because if right after that I re-trigger a revalidate with ["/foo"] it works like a charm!

So now, we know the issue. But I have absolutely no clue how to fix it.
It seems we don't have power over when getStaticPaths is actually called? It seems to be trigger by my revalidate but always at the end of it.

So I am here to ask for guidance.

Thanks a lot everyone!

3 Replies

@Sage Grouse Hello everyone! I work on a project that looks a bit like a CMS, so you can create some pages (/foo, /bar, etc..) and then, everytime you add or update a page I am using **revalidate** to rebuild only those pages. Obiouvsly for those pages to be available, I also have a generic Next page (let's call it **[...generic].ts** and this page is using the **getStaticPaths()** hook to provide all the existing paths (/foo, /bar, etc). The thing is I have a little timing issue. Let's say I want to edit the /foo page. 1. once I am done with my editing, I will call my api which calls **revalidate()** with the right paths to rebuild (here, it's gonna be `["/foo"]` 2. revalidate does it things **__BUT__** it fails revalidating /foo : `Failed to revalidate /foo: Invalid response 404` 3. after the revalidate failed, a console.log inside my **getStaticPaths** is triggered and I clearly see `"/foo"` So my issue is, I am currently trying to revalidate **first** and then **getStaticPaths** is triggered. So obviously for the **revalidate** point of view, /foo does not exist yet. I am pretty sure it's the issue because if right after that I re-trigger a revalidate with `["/foo"]` it works like a charm! So now, we know the issue. But I have absolutely no clue how to fix it. It seems we don't have power over when **getStaticPaths** is actually called? It seems to be trigger by my revalidate but always at the end of it. So I am here to ask for guidance. Thanks a lot everyone!
@joulev use [`fallback: "blocking"`](<https://nextjs.org/docs/pages/api-reference/functions/get-static-paths#fallback-blocking>) in the `getStaticPaths`, in conjunction with [`notFound`](<https://nextjs.org/docs/pages/api-reference/functions/get-static-props#notfound>) in `getStaticProps`
Sage GrouseOP
I tried it, I just need to figure out why most of my pages are 404. If I cant find something I will update this thread otherwise I will resolve it 🙂