Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unstable_revalidate errors when returning notFound: true #34809

Closed
musme opened this issue Feb 25, 2022 · 9 comments · Fixed by #34826
Closed

unstable_revalidate errors when returning notFound: true #34809

musme opened this issue Feb 25, 2022 · 9 comments · Fixed by #34826
Assignees
Labels
Navigation Related to Next.js linking (e.g., <Link>) and navigation.

Comments

@musme
Copy link

musme commented Feb 25, 2022

What is the improvement or update you wish to see?

Please, describe in the documentation how to remove a page that returns 404.

Is there any context that might help us understand?

Suppose i have the page /pages/post/[slug].tsx with

getStaticProps

export const getStaticProps: GetStaticProps = async ({ params }) => {
  let slug = params.slug;
  const post = await getPostFromWordpress(slug);
  if (!post) {
    return {
      notFound: true
    };
  }
  return {
    props: {
      post
    }
  };
};

and getStaticPaths

export const getStaticPaths: GetStaticPaths = async () => {
  const posts = await getPostFromWordpress();

  return {
    paths: posts .map(post => `/post/${post.slug}`) || [],
    fallback: 'blocking'
  };
};

And also i have an api route: /api/wordpress/revalidate:

export default async function handler(req, res) {

  if (req.query.secret !== process.env.MY_SECRET_TOKEN) {
    return res.status(401).json({ message: 'Invalid token' })
  }

  try {
    await res.unstable_revalidate(`/post/${req.query.slug}`)
    return res.json({ revalidated: true })
  } catch (err) {
    return res.status(500).send(err.message)
  }
}

From client side i can click "Delete post". After clicking the button, the post is deleted in Wordpress.
When I delete a post in Wordpress I call this api route /api/wordpress/revalidate?secret=&slug=some_slug, but I am getting an error "Failed to revalidate /post/some_slug".

This happens because {notFound: true } is returned from getStaticProps

try {
const res = await fetch(`${baseUrl}${urlPath}`, {
headers: {
[PRERENDER_REVALIDATE_HEADER]: context.previewModeId,
...extraHeaders,
},
})
if (!res.ok) {
throw new Error(`Invalid response ${res.status}`)
}
} catch (err) {
throw new Error(`Failed to revalidate ${urlPath}`)
}
}

and res.ok = false

Please, describe in the documentation how to delete /pages/post/some_slug?

Does the docs page already exist? Please link to it.

https://nextjs.org/blog/next-12-1#on-demand-incremental-static-regeneration-beta

@musme musme added the Documentation Related to Next.js' official documentation. label Feb 25, 2022
@ijjk ijjk added kind: bug and removed Documentation Related to Next.js' official documentation. labels Feb 25, 2022
@ijjk ijjk self-assigned this Feb 25, 2022
@ijjk ijjk added the Navigation Related to Next.js linking (e.g., <Link>) and navigation. label Feb 25, 2022
@ijjk
Copy link
Member

ijjk commented Feb 25, 2022

Hi, this looks like a bug as we shouldn't be considering returning notFound: true during a revalidate an error as this is valid, thanks for reporting this!

@ijjk ijjk changed the title Docs: On-demand Incremental Static Regeneration unstable_revalidate errors when returning notFound: true Feb 25, 2022
@musme musme closed this as completed Feb 25, 2022
@musme musme reopened this Feb 25, 2022
@musme
Copy link
Author

musme commented Feb 25, 2022

Thanks 👍

@ijjk
Copy link
Member

ijjk commented Feb 26, 2022

Hi, this should be updated in v12.1.1-canary.4 of Next.js, please update and give it a try!

@ramiel
Copy link

ramiel commented Mar 3, 2022

I checked version 12.1.1-canary.6 and revalidation is still not working. For me it constantly crash with no clear indication of why

2022-03-03T11:19:34.587Z	8f3d7a9a-33aa-44db-91fc-5b1703c0cdd2	ERROR	Error: Failed to revalidate /site1
    at unstable_revalidate (/var/task/sites/live/node_modules/next/dist/server/api-utils/node.js:236:15)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
    at async handler (/var/task/sites/live/.next/server/pages/api/revalidate.js:24:9)
    at async Object.apiResolver (/var/task/sites/live/node_modules/next/dist/server/api-utils/node.js:186:9)
    at async NextNodeServer.runApi (/var/task/sites/live/node_modules/next/dist/server/next-server.js:388:9)
    at async Object.fn (/var/task/sites/live/node_modules/next/dist/server/base-server.js:485:37)
    at async Router.execute (/var/task/sites/live/node_modules/next/dist/server/router.js:234:32)
    at async NextNodeServer.run (/var/task/sites/live/node_modules/next/dist/server/base-server.js:597:29)
    at async NextNodeServer.handleRequest (/var/task/sites/live/node_modules/next/dist/server/base-server.js:304:20)
    at async Server.<anonymous> (/var/task/sites/live/___next_launcher.js:32:9)

Can I help somehow?

@ijjk
Copy link
Member

ijjk commented Mar 3, 2022

@ramiel is this specifically not working when revalidating and returning notFound: true or are you seeing this error for all cases? This issue is specific to the notFound: true case which is being tested now.

@ramiel
Copy link

ramiel commented Mar 3, 2022

I first updated to the canary version because of the 404 problem. I just want to tell that the problem is not solved with that version. Nonetheless I'm seeing the error in all the calls I do, but it's difficult to understand what's happening because the error message is not helpful. Maybe the original error shouldn't be replaced with that generic text?

@ijjk
Copy link
Member

ijjk commented Mar 3, 2022

@ramiel I think your issue is more likely related to #34874

@ramiel
Copy link

ramiel commented Mar 4, 2022

Ok, because this definitely doesn't happen to me on localhost (dev or production mode), but this definitely happens on production on vercel. I'll monitor that issue, thanks. But just to understand: do you know there's a problem in some configurations/hosts?

@github-actions
Copy link
Contributor

github-actions bot commented Apr 4, 2022

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 4, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Navigation Related to Next.js linking (e.g., <Link>) and navigation.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants