File Conventions — loading.tsx · error.tsx · not-found.tsx
File Conventions — loading.tsx · error.tsx · not-found.tsx
💡 Why Learn This? — 'File Name = Behavior' Convention
5 Special Files — The Name Is the Convention
1. The 5 Files and Their Roles
2. loading.tsx — Automatic Suspense Wrapping
3. error.tsx — Automatic Error Boundary
- ▸Calling
reset()resets the Error Boundary and re-attempts rendering the page. - ▸
error.digestis a server-side error identifier (for logging). - ▸
error.tsxcannot catch errors thrown bylayout.tsxin the same folder — place it one directory level up.
4. not-found.tsx — 404 UI
5. Nesting — The Nearest File Takes Effect
The nearest file takes priority. If no file exists in the same folder, Next.js walks up to the parent folder.
💡 💡 File Convention: 5 Practical Tips
1. error.tsx must always have 'use client'
React Error Boundaries are client-only by nature, so the convention enforces this. Omitting it will cause a build error.
2. error.tsx cannot catch errors from layout.tsx in the same folder
If a layout throws, the error.tsx one directory level up will catch it. Root layout errors are caught by global-error.tsx (Client only).
3. loading.tsx is shown only during the first await in page.tsx
It disappears once the page has finished rendering. It will reappear on subsequent client-side navigations.
4. notFound() is similar to throw — code below it does not run
TypeScript also narrows post to non-null after this point.
5. Keep all convention files in the same folder
The folder becomes a UI state machine — every state of the page is visible at a glance.