Code Monkey home page Code Monkey logo

Comments (1)

ztanner avatar ztanner commented on July 22, 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.

from next.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.