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

Triggering notFound in generateMetadata results in Error: NEXT_NOT_FOUND when the delayed notFound call is used (e.g. fetch) #65951

Closed
fax1ty opened this issue May 19, 2024 · 2 comments
Labels
bug Issue was opened via the bug report template. locked Metadata Related to Next.js' Metadata API. Navigation Related to Next.js linking (e.g., <Link>) and navigation.

Comments

@fax1ty
Copy link

fax1ty commented May 19, 2024

Link to the code that reproduces this issue

https://github.com/fax1ty/next-generatemetadata-repro

To Reproduce

  1. Start the application in dev mode (yarn dev)
  2. Go to /en/test/id

Current vs. Expected behavior

Current: I get NEXT_NOT_FOUND in console
Desired: I see the 404 content

Provide environment information

Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 10 Pro
  Available memory (MB): 16291
  Available CPU cores: 12
Binaries:
  Node: 21.2.0
  npm: 10.2.3
  Yarn: 1.22.22
  pnpm: N/A
Relevant Packages:
  next: 14.2.3 // Latest available version is detected (14.2.3).
  eslint-config-next: 14.0.4
  react: 18.3.1
  react-dom: 18.3.1
  typescript: 5.4.5
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Metadata, Navigation

Which stage(s) are affected? (Select all that apply)

next dev (local), next start (local), Vercel (Deployed), Other (Deployed)

Additional context

May relate to #65013
If you remove the setTimeout, everything works as expected
P.S. I have created a copy of real-world project that demonstrates this problem. Perhaps the issue is something else entirely.

@fax1ty fax1ty added the bug Issue was opened via the bug report template. label May 19, 2024
@github-actions github-actions bot added Metadata Related to Next.js' Metadata API. Navigation Related to Next.js linking (e.g., <Link>) and navigation. labels May 19, 2024
@ztanner
Copy link
Member

ztanner commented May 20, 2024

Hi @fax1ty --

This doesn't appear to be related to where you call it -- ie, I see the same problem when I do the setTimeout + notFound() inside the Page component itself.

Can you clarify the real-world use-case a bit more? If you're trying to trigger notFound() after some async work, you should await that async work and call notFound() afterwards. For example:

const asyncWork = new Promise((resolve) => {
  setTimeout(() => {
    resolve();
  }, 500);
});

await asyncWork;
notFound();

When you call it inside of a setTimeout callback, Next.js won't be able to catch & handle the error, because it will happen asynchronously outside the main execution flow of your component. The notFound() function needs to be called within the lifecycle of a request in a way that Next.js can properly handle and render the appropriate error page.

In your original code, the notFound() function is being executed inside the setTimeout callback, which detaches it from the main request lifecycle. Next.js is designed to handle such errors synchronously within the request lifecycle. To fix this, ensure any asynchronous operations are properly awaited and errors like notFound() are called synchronously after those operations complete.

I'm going to close this as I don't think there's a bug, but please let me know if I'm misunderstanding a use-case that makes the above recommendation not possible.

@ztanner ztanner closed this as not planned Won't fix, can't repro, duplicate, stale May 20, 2024
Copy link
Contributor

github-actions bot commented Jun 5, 2024

This closed issue has been automatically locked because it had no new activity for 2 weeks. 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 added the locked label Jun 5, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 5, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. locked Metadata Related to Next.js' Metadata API. Navigation Related to Next.js linking (e.g., <Link>) and navigation.
Projects
None yet
Development

No branches or pull requests

2 participants