Konwencje interfejsów ładowania i błędów
Korzystaj z konwencji plików App Router: loading.js, error.js i not-found.js, aby tworzyć odporne, strumieniowane trasy z eleganckimi ekranami zastępczymi.
Konwencje interfejsów ładowania i błędów to bezpłatna lekcja Next.js 15 Fullstack Web Apps na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Next.js 15 Fullstack Web Apps, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Next.js 15 Fullstack Web Apps zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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.
Często zadawane pytania
Czy lekcja „Konwencje interfejsów ładowania i błędów” jest bezpłatna?
Tak — pełny tekst „Konwencje interfejsów ładowania i błędów” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Next.js 15 Fullstack Web Apps, przejdź na CoddyKit PRO. Kurs Next.js 15 Fullstack Web Apps zawiera 4 lekcji w sumie.
Co nauczysz się w „Konwencje interfejsów ładowania i błędów”?
Korzystaj z konwencji plików App Router: loading.js, error.js i not-found.js, aby tworzyć odporne, strumieniowane trasy z eleganckimi ekranami zastępczymi. Ćwiczysz Next.js 15 Fullstack Web Apps z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Next.js 15 Fullstack Web Apps?
Nie wymagamy żadnego doświadczenia. Next.js 15 Fullstack Web Apps w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.
Ile czasu zajmuje lekcja „Konwencje interfejsów ładowania i błędów”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Next.js 15 Fullstack Web Apps?
Tak. Każda lekcja Next.js 15 Fullstack Web Apps zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Trasy dynamiczne i segmenty catch-all
- Zagnieżdżone układy i grupy tras
- Trasy równoległe i przechwytujące
- Konwencje interfejsów ładowania i błędów