로딩 및 오류 UI 규칙
App Router의 파일 규칙인 loading.js, error.js, not-found.js를 사용해 복원력 있고 스트리밍되는 경로와 자연스러운 대체 동작을 구현합니다.
로딩 및 오류 UI 규칙은(는) CoddyKit의 무료 Next.js 15 Fullstack Web Apps 강의입니다. 이것은 4개 중 4번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Next.js 15 Fullstack Web Apps 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Next.js 15 Fullstack Web Apps 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
Why Loading and Error UI Matter
Advanced routing is not only about where a route lives but about what users see while it resolves or fails. The App Router gives you special files that wrap segments automatically.
loading.jsrenders an instant fallback while the segment streams.error.jscatches runtime errors in that segment.not-found.jsrenders whennotFound()is called.
The loading.js Convention
A loading.js file in a segment folder is automatically wrapped around page.js in a React Suspense boundary. While the server component awaits data, the loading UI shows instantly.
export default function Loading() {
return <div className="spinner">Loading dashboard...</div>;
}Skeletons Beat Spinners
For perceived performance, render a skeleton that mirrors the final layout instead of a generic spinner. It reduces layout shift and feels faster.
export default function Loading() {
return (
<ul>
{Array.from({ length: 5 }).map((_, i) => (
<li key={i} className="skeleton-row" />
))}
</ul>
);
}Streaming with Suspense
Because loading.js is just Suspense under the hood, the rest of the layout renders immediately while only the slow segment streams in. You can also nest your own Suspense boundaries inside a page for finer control.
import { Suspense } from 'react';
export default function Page() {
return (
<section>
<h1>Reports</h1>
<Suspense fallback={<p>Loading chart...</p>}>
<SlowChart />
</Suspense>
</section>
);
}The error.js Convention
error.js must be a Client Component. It receives the thrown error and a reset function to retry rendering the segment.
'use client';
export default function Error({ error, reset }) {
return (
<div>
<p>Something went wrong: {error.message}</p>
<button onClick={() => reset()}>Try again</button>
</div>
);
}Error Boundaries Are Scoped
An error.js catches errors in its segment and its children, but not in the layout at the same level. To catch layout errors, place the error file one level up.
- Errors bubble up to the nearest parent boundary.
- The root layout cannot be caught by a sibling error file.
global-error.js for the Root
To catch errors in the root layout itself, add global-error.js. It replaces the entire document, so it must render its own <html> and <body> tags.
'use client';
export default function GlobalError({ error, reset }) {
return (
<html>
<body>
<h2>App crashed</h2>
<button onClick={() => reset()}>Reload</button>
</body>
</html>
);
}Triggering not-found.js
Call notFound() from next/navigation inside a server component to render the nearest not-found.js and send a 404 status.
import { notFound } from 'next/navigation';
export default async function Page({ params }) {
const post = await getPost(params.id);
if (!post) notFound();
return <article>{post.title}</article>;
}Custom not-found.js UI
Place not-found.js in any segment to override the default 404 for that part of the route tree. A root-level one acts as the global 404 page.
import Link from 'next/link';
export default function NotFound() {
return (
<div>
<h2>Post not found</h2>
<Link href="/blog">Back to blog</Link>
</div>
);
}Combining the Conventions
A robust segment folder often contains all four files working together:
page.js— the contentloading.js— streamed fallbackerror.js— runtime failure recoverynot-found.js— missing resource
Each is wired up automatically by the App Router with no manual provider setup.
Logging Errors in Production
Use a useEffect inside error.js to report errors to your monitoring service while still showing recovery UI to the user.
'use client';
import { useEffect } from 'react';
export default function Error({ error, reset }) {
useEffect(() => {
reportToSentry(error);
}, [error]);
return <button onClick={reset}>Retry</button>;
}Quick Check
Which statement about error.js in the App Router is correct?
Recap
You learned the App Router's resilience conventions:
loading.jswraps segments in Suspense for instant streamed fallbacks.error.js(Client Component) recovers from runtime errors withreset.global-error.jscatches root-layout failures.not-found.jsrenders whennotFound()is called.
Together they make advanced routes graceful under load and failure.
자주 묻는 질문
“로딩 및 오류 UI 규칙” 강의는 무료인가요?
네 — “로딩 및 오류 UI 규칙” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Next.js 15 Fullstack Web Apps 강의 전체를 잠금 해제할 수 있습니다. Next.js 15 Fullstack Web Apps 강의에는 총 4개의 강의가 포함되어 있습니다.
“로딩 및 오류 UI 규칙”에서 뭘 배우나요?
App Router의 파일 규칙인 loading.js, error.js, not-found.js를 사용해 복원력 있고 스트리밍되는 경로와 자연스러운 대체 동작을 구현합니다. 브라우저에서 직접 실행하는 실습 코드로 Next.js 15 Fullstack Web Apps을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Next.js 15 Fullstack Web Apps을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Next.js 15 Fullstack Web Apps은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 4번째 강의입니다.
“로딩 및 오류 UI 규칙” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Next.js 15 Fullstack Web Apps 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Next.js 15 Fullstack Web Apps 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- 동적 경로와 포괄 세그먼트
- 중첩 레이아웃과 경로 그룹
- 병렬 경로와 가로채기 경로
- 로딩 및 오류 UI 규칙