Each file is a boundary Next.js inserts for you, in this order under the segment's layout and template: error.js (an error boundary), then loading.js (a <Suspense> boundary whose fallback is the file), then not-found.js, then the page or the child segment. What the files add on top of React:
loading.js. The fallback is part of the prefetched route, so a<Link>click shows it at once while the page renders on the server and streams in. On a first load it's in the initial HTML and the page streams in later. Because it sits below the segment's layout, it can't cover a slow layout in the same folder; the parent'sloading.jscan.error.js. Must be a Client Component. It catches errors from rendering the page and child segments, including Server Component errors, which Next.js forwards to the client: in productionerror.messageis React's generic error #441 anderror.digestmatches the server log.retry()refetches and re-renders the segment;reset()only clears the error state. It doesn't catch its own layout's errors: the parent'serror.jsdoes, andglobal-error.jscovers the root layout.not-found.js. Renders whennotFound()is thrown in the segment or below, and for unmatched URLs at the root. Next.js adds<meta name="robots" content="noindex">.
The status code depends on when the boundary triggers. Before anything has streamed, notFound() yields a real 404. Once a loading.js or <Suspense> fallback has been sent, the headers are gone, so the response stays 200 and the not-found UI streams in with noindex. With Cache Components, the prerendered shell is sent first, so a request-time notFound() also gives 200. To return a real 404 there, check before streaming starts, for example in proxy.ts.