🎓 A1Learner
🔍
0 of 50 lessons completed

Next.js not-found.tsx

not-found.tsx is the 404 page. It shows when an address has no page, or when a page calls notFound().

The books 404 page

The file holds the missing-book message. It lives next to the books page.

export default function NotFound() {
  return (
    <div>
      <h1>That book is not on the shelf.</h1>
      <p>No book with this number lives in My Bookshelf.</p>
    </div>
  );
}

A missing book, checked

The check asks for book 999. The missing-book text is there.

curl -s http://localhost:3000/books/999 | grep -o "That book is not on the shelf."

An address with no page

The check asks for /nope, which has no page at all. It answers 404.

curl -s -o /dev/null -w "HTTP %{http_code}\n" http://localhost:3000/nope

Example

console.log("/books/999: That book is not on the shelf.");
console.log("/books/999 status: 200 with loading.tsx, 404 without it");
console.log("/nope status: 404, Page not found.");

Key points

  • 404: the status that means "no page here"
  • notFound() is the call that shows the 404 page for a missing book.
  • /books/999 shows "That book is not on the shelf." It answers 200 while app/books/loading.tsx exists.
  • With loading.tsx moved aside, the same address answers 404. The file was put back after the check.
  • /nope answers 404 with "Page not found." That is the root 404 page.

In this example, two missing addresses show two 404 pages. Book 999 shows the books 404 text, but its status is 200 while loading.tsx exists and 404 once that file moves aside. The address /nope has no page at all, so it answers 404 with the root text "Page not found."

Note

The 200 status with loading.tsx in place was the measured result here. The text is right, but the status turns to 404 once the loading file moves.

Try it Yourself

Edit the code, then press Run.

Running is simulated · TypeScript Playground

Exercise

What status does /books/999 answer while loading.tsx exists?