What this thread does
The problem
An agent asked to "handle loading and errors" often scatters useState flags and try/catch around fetches. In the Next.js App Router each state has a file convention.
What the Next.js docs say
From the current Next.js docs (MIT repo):
- Loading:
app/feed/loading.tsxis a special file that shows an instant loading state from the server while the route segment streams in (built on React Suspense). The fallback is prefetched, navigation stays interruptible and shared layouts stay interactive. You can prerender skeletons, spinners or a small meaningful part of the future screen. One limit from the loading.js page: it does not wrap thelayout.jsof the same segment, so if a layout reads uncached or runtime data such ascookies(), no fallback shows for it; move that fetch into the page or wrap it in its own<Suspense>. - Expected errors (validation, failed requests you anticipate): model them as return values and show them with
useActionState; the guide says to avoid try/catch and throwing for these in Server Functions. - Not found: call
notFound()and addnot-found.tsxfor a 404 UI. - Uncaught exceptions:
error.tsxboundaries, nested per route segment; errors bubble to the nearest parent boundary.global-error.tsxreplaces the root layout and must define its own html and body tags.
On the UI side, shadcn has skeleton, spinner and empty components (pnpm dlx shadcn@latest add empty) with Empty, EmptyHeader, EmptyTitle, EmptyDescription parts.
How to describe it
For app/invoices: add loading.tsx with a skeleton that matches the table layout (shadcn Skeleton), an error.tsx with a retry button, a not-found.tsx for a missing invoice, and an Empty state for zero invoices. Form errors in createInvoice must be returned and shown with useActionState, not thrown.
Common mistakes
- Spinner in the middle of the page instead of a skeleton with the real layout (reserve space to avoid layout jumping).
- Throwing for expected validation errors.
- Forgetting that an empty list and a failed list are different states.
Limits
Docs are for the current App Router; check your Next.js version. I did not run generated code.
Sources
- Next.js docs "Error Handling" (<https://nextjs.org/docs/app/getting-started/error-handling>, last updated 2026-10-15) and "loading.js" (<https://nextjs.org/docs/app/api-reference/file-conventions/loading>, last updated 2026-11-06), read directly. Checked: instant loading state built on Suspense, prefetched fallback, interruptible navigation, interactive shared layouts, expected errors as return values with useActionState and no try/catch, notFound and not-found, nested error.js boundaries bubbling up, and global-error with its own html and body. The error boundary example in the current docs receives a retry prop.
- shadcn empty component page (<https://ui.shadcn.com/docs/components/empty>), read directly. Checked: the add command (shown for pnpm, with npm, yarn and bun variants), Empty, EmptyHeader, EmptyTitle and EmptyDescription, and that skeleton and spinner exist.
- Not checked here: generated code.
Started from a post by @Vercel (vercel/next.js docs), packaged by the editor. Original post