Thuta Learning
Next.js
IntermediateWeb Developmentအလယ်အလတ်

Loading၊ Error နှင့် Not Found UI

Next.jsသင်ခန်းစာ 1030 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • loading.tsx က page ကို Suspense boundary နဲ့ ဘယ်လို အလိုအလျောက် ဝိုင်းပေးလဲ ရှင်းပြနိုင်ဖို့
  • error.tsx ကို "use client" နဲ့ ရေးပြီး retry button ပေးနိုင်ဖို့
  • notFound() နဲ့ not-found.tsx ကို သုံးပြီး မျှော်မှန်းထားတဲ့ 'မရှိ' အခြေအနေကို bug နဲ့ ခွဲပြနိုင်ဖို့
  • Error တစ်ခုကို ဘယ် error.tsx က ဖမ်းမလဲ (layout အပါအဝင်) ခန့်မှန်းနိုင်ဖို့

ရိုးရိုးလေး ရှင်းပြမယ်

Loading၊ error၊ not found ကို ရိုးရိုးလေး ပြောရရင်

စားသောက်ဆိုင်တစ်ဆိုင်ကို စဉ်းစားကြည့်ပါ။ ဟင်းချက်နေတုန်း စားပွဲထိုးက 'ခဏစောင့်ပါ' လို့ ပြောပါတယ် (loading)။ မီးဖိုချောင်မှာ မီးလောင်သွားရင် 'တောင်းပန်ပါတယ်၊ ပြန်မှာကြည့်ပါ' လို့ ပြောပါတယ် (error)။ Menu ထဲမှာ မရှိတဲ့ ဟင်းကို မှာရင် 'ဒီဟင်း မရှိပါဘူး' လို့ ပြောပါတယ် (not found)။ စကားသုံးခွန်းလုံးကို 'ပြဿနာ တစ်ခုခု ရှိတယ်' လို့ပဲ ပြောရင် ဧည့်သည်က ဘာလုပ်ရမလဲ မသိပါဘူး။

Next.js App Router က ဒီအခြေအနေ သုံးမျိုးအတွက် route folder ထဲမှာ ထားရုံနဲ့ အလုပ်လုပ်တဲ့ file သုံးခုကို ပေးထားပါတယ်: loading.tsx၊ error.tsx နဲ့ not-found.tsx ပါ။

Problem → Why → Solution

ပြဿနာ: တိတ်ဆိတ်တဲ့ click နဲ့ 'Something went wrong' တစ်မျိုးတည်း

အောက်က note page မှာ ပြဿနာ သုံးခု ရှိပါတယ်။ Data load လုပ်နေတဲ့ စက္ကန့်အနည်းငယ်အတွင်း screen မှာ ဘာမှ မပြောင်းလို့ user က link ကို ထပ်ခါထပ်ခါ နှိပ်ပါတယ်။ try/catch က မရှိတဲ့ note၊ database ကျနေတာ၊ code bug အားလုံးကို စာကြောင်းတစ်ကြောင်းတည်းနဲ့ ဖုံးလိုက်ပါတယ်။ ပိုဆိုးတာက catch block က status 200 နဲ့ ပြန်ပေးတဲ့အတွက် monitoring tool ကလည်း ဘာမှ မသိရပါဘူး။

app/notes/[id]/page.tsx
export default async function NotePage({
  params,
}: {
  params: Promise<{ id: string }>;
}) {
  try {
    const { id } = await params;
    const note = await getNote(id); // slow: the screen just freezes meanwhile
    return <h1>{note!.title}</h1>;
  } catch {
    // a missing note, an outage and a bug all end up here, as a 200
    return <p>Something went wrong.</p>;
  }
}

ဒါက ဘာကြောင့် အရေးကြီးတာလဲ

အခြေအနေ တစ်ခုချင်းစီက user ကို မတူတဲ့ လုပ်ဆောင်ချက်ဆီ ပို့ရပါမယ်: စောင့်ပါ၊ ပြန်ကြိုးစားပါ၊ ဒါမှမဟုတ် တခြား note ကို ရှာပါ။ Special file သုံးခုက ဒါကို page code ထဲက try/catch မလိုဘဲ folder အဆင့်မှာ လုပ်ပေးပြီး error တွေကိုလည်း မဖုံးဘဲ log လုပ်ခွင့်ပေးပါတယ်။

Core concept: folder အဆင့်က boundary သုံးမျိုး

loading.tsx ကို folder ထဲထားလိုက်ရင် Next.js က အဲဒီ folder ရဲ့ page (နဲ့ အောက်က nested layout တွေ) ကို <Suspense fallback={<Loading />}> နဲ့ အလိုအလျောက် ဝိုင်းပေးပါတယ်။ Navigation လုပ်တဲ့အခါ အပေါ်က layout တွေ ချက်ချင်း ကျန်နေပြီး page ရောက်မယ့်နေရာမှာ fallback ကို ပြပါတယ်၊ data ရလာတာနဲ့ page က fallback ကို အစားထိုးပါတယ်။ Fallback ကို prefetch လုပ်ထားလို့ click နှိပ်တာနဲ့ ချက်ချင်း ပေါ်ပြီး navigation ကို ကြားဖြတ်ပြီး တခြား link ကို သွားလို့လည်း ရပါတယ်။

error.tsx က folder ကို React error boundary နဲ့ ဝိုင်းပါတယ်။ Render လုပ်ရင်း ဒါမှမဟုတ် Server Component ထဲမှာ exception ဖြစ်ရင် app တစ်ခုလုံး မပြိုဘဲ အဲဒီ segment နေရာမှာပဲ error UI ပေါ်ပါတယ်။ Error boundary က state ထိန်းပြီး button click ကို တုံ့ပြန်ရလို့ error.tsx ရဲ့ ထိပ်ဆုံးမှာ "use client" မဖြစ်မနေ ရေးရပါတယ်။ Production မှာ Server Component ကလာတဲ့ error ရဲ့ message ကို လုံခြုံရေးအတွက် ယေဘုယျ စာသားနဲ့ အစားထိုးပြီး server log နဲ့ ချိတ်ဖို့ error.digest ကို ပေးပါတယ်။

Retry button အတွက် prop နာမည်က version အလိုက် ကွာပါတယ်။ Next.js docs ရဲ့ version history အရ unstable_retry ကို 16.2 မှာ ထည့်ခဲ့ပြီး 16.3 မှာ retry အဖြစ် stable ဖြစ်လာပါတယ်။ retry() က segment ကို data ပြန်ယူပြီး ပြန် render လုပ်ပါတယ်။ အရင်ကတည်းက ရှိခဲ့တဲ့ reset() ကတော့ data ပြန်မယူဘဲ error state ကိုပဲ ရှင်းပါတယ်။ ကိုယ့် project ရဲ့ version ကို error.js API reference မှာ စစ်ပါ။

not-found.tsx ကတော့ bug အတွက် မဟုတ်ပါဘူး၊ မျှော်မှန်းထားပြီးသား 'မရှိ' အခြေအနေအတွက်ပါ။ Page ထဲမှာ notFound() ကို ခေါ်လိုက်ရင် အနီးဆုံး not-found.tsx ကို render လုပ်ပြီး noindex meta tag ကိုလည်း ထည့်ပေးပါတယ်။ Root app/not-found.tsx ကတော့ app ထဲ ဘယ် route နဲ့မှ မကိုက်တဲ့ URL တွေကိုလည်း ကိုင်တွယ်ပါတယ်။ app/notes/not-found.tsx လို nested ဖြစ်တဲ့ဟာကတော့ app/notes/[id]/page.tsx အပါအဝင် သူ့အောက်က page တိုင်းက ခေါ်တဲ့ notFound() ကို တုံ့ပြန်ပါတယ်။

Request တစ်ခုက ဘယ် UI ဆီ ရောက်မလဲ
  1. Data မရောက်သေး → loading.tsx
  2. notFound() ခေါ် → not-found.tsx
  3. Exception throw → error.tsx
  4. အားလုံး အဆင်ပြေ → page.tsx

File တစ်ခုချင်းက folder အဆင့်မှာ အလုပ်လုပ်ပြီး အနီးဆုံး parent folder ထဲကဟာက အနိုင်ရပါတယ်။

အထွေထွေ ပုံစံ

app/<segment>/
// loading.tsx: no props, shown while the segment streams in
export default function Loading() { return <Placeholder />; }

// error.tsx: must be a Client Component
"use client";
export default function Error({ retry }: { error: unknown; retry: () => void }) {
  return <button onClick={() => retry()}>Try again</button>;
}

// not-found.tsx: no props, shown after notFound()
export default function NotFound() { return <Message />; }

File သုံးခုလုံးက default export component တစ်ခုစီ ဖြစ်ပြီး route folder ထဲမှာ ထားရုံပါပဲ။ Props ယူတာ error.tsx တစ်ခုတည်းဖြစ်ပြီး "use client" လိုတာလည်း သူတစ်ခုတည်းပါ။

Worked example: /notes route အတွက် file သုံးခု

အောက်က code block မှာ file သုံးခုကို comment နဲ့ ခွဲပြထားပါတယ်၊ တကယ့် project မှာတော့ app/notes/ folder ထဲက file သုံးခု သီးခြားစီ ဖြစ်ပါတယ်။ Error component က Next.js 16.3 နောက်ပိုင်းရဲ့ retry prop ကို သုံးထားပါတယ်။

app/notes/
// app/notes/loading.tsx
export default function Loading() {
  return <p>Loading notes…</p>;
}

// app/notes/error.tsx
"use client";

export default function NotesError({
  retry,
}: {
  error: unknown;
  retry: () => void;
}) {
  return (
    <div role="alert">
      <p>We could not load your notes.</p>
      <button onClick={() => retry()}>Try again</button>
    </div>
  );
}

// app/notes/not-found.tsx
export default function NotFound() {
  return <h2>This note was not found</h2>;
}

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. Instant feedbackreturn <p>Loading notes…</p>;

    Data ရောက်မလာခင် ပြမယ့် UI ပါ။ Navigation စတာနဲ့ ချက်ချင်း ပေါ်လို့ user က click အလုပ်လုပ်တယ်ဆိုတာ သိပါတယ်။

  2. Client boundary"use client";

    Error boundary က state နဲ့ onClick လိုလို့ ဒီ file ကို Client Component အဖြစ် ကြေညာရပါတယ်။ မပါရင် Next.js က error ပေးပါတယ်။

  3. The props contractretry: () => void;

    Next.js က error နဲ့ retry ကို ထည့်ပေးပါတယ်။ error က unknown ဖြစ်လို့ message ကို မဖတ်ခင် instanceof Error နဲ့ စစ်ရပါမယ်။ Version အဟောင်းမှာ ဒီနေရာမှာ reset ဒါမှမဟုတ် unstable_retry ဖြစ်ပါတယ်။

  4. Recover<button onClick={() => retry()}>Try again</button>

    ယာယီ network ပြဿနာ ဆိုရင် user က ကိုယ်တိုင် ပြန်ကြိုးစားလို့ ရပါတယ်၊ page တစ်ခုလုံး reload စရာ မလိုပါဘူး။

  5. The expected missreturn <h2>This note was not found</h2>;

    notFound() ခေါ်တဲ့အခါ ပြမယ့် UI ပါ။ Error မဟုတ်လို့ retry button မလိုဘဲ list page ကို ပြန်သွားတဲ့ link က ပိုအသုံးဝင်ပါတယ်။

You should see
Data load လုပ်နေတုန်း "Loading notes…" ကို ချက်ချင်း မြင်ရပါတယ်။ Exception ဖြစ်ရင် "We could not load your notes." နဲ့ Try again button ပေါ်ပြီး notFound() ခေါ်ရင် "This note was not found" ကို မြင်ရပါတယ်။

ဘယ် boundary က ဘာကို ဖမ်းလဲ

Segment တစ်ခုထဲမှာ special file တွေကို Next.js က သတ်မှတ်ထားတဲ့ အစီအစဉ်နဲ့ စီပါတယ်: layout ထဲမှာ error boundary၊ အဲဒီထဲမှာ loading (Suspense)၊ အဲဒီထဲမှာ not-found နဲ့ page ပါ။ ဒါကြောင့် error.tsx က တူညီတဲ့ folder ထဲက layout.tsx ကို မဝိုင်းပါဘူး — layout ထဲက error ကို parent folder ရဲ့ error.tsx က ဖမ်းပါတယ်။ Root layout ထဲက error အတွက်တော့ <html> နဲ့ <body> ကိုယ်ပိုင် ပါတဲ့ app/global-error.tsx လိုပါတယ်။

text
Nesting inside the app/notes segment
------------------------------------
notes/layout.tsx          errors here go to app/error.tsx
 +- error boundary        (notes/error.tsx)
     +- Suspense          (fallback: notes/loading.tsx)
         +- not-found     (notes/not-found.tsx)
         +- page          (notes/page.tsx)

Streaming စပြီးမှ notFound() ခေါ်ရင်

Response ကို stream လုပ်စပြီ (ဥပမာ loading.tsx fallback ပို့ပြီးပြီ) ဆိုရင် HTTP status ကို ပြောင်းလို့ မရတော့လို့ status က 200 အဖြစ် ကျန်ပါတယ်။ Next.js က not-found UI ကို ပြပြီး noindex meta tag ထည့်ပေးတဲ့အတွက် search engine က index မလုပ်ပါဘူး။ Status 404 အတိအကျ လိုရင် streaming မစခင် စစ်ရပါမယ် (loading.tsx docs ရဲ့ Status codes အပိုင်းကို ကြည့်ပါ)။

လက်တွေ့ project တွေမှာ

လက်တွေ့ project တွေမှာ

Production app တွေမှာ ဒီ file သုံးခုကို route တစ်ခုချင်းစီရဲ့ အရေးကြီးမှုအလိုက် ထားကြပါတယ်။ Dashboard တစ်ခုလုံးအတွက် error.tsx တစ်ခုထားပြီး payment လို အရေးကြီးတဲ့ route တွေမှာတော့ ပိုတိကျတဲ့ error.tsx ကိုယ်ပိုင် ထားကြပါတယ်။

  • Loading UI ကို ရောက်လာမယ့် content နဲ့ အရွယ်တူ skeleton အဖြစ် ဆွဲလို့ data ရောက်လာတဲ့အခါ layout မခုန်ပါဘူး။
  • error.tsx ထဲမှာ useEffect နဲ့ Sentry လို error reporting service ဆီ error ကို ပို့ပြီး user ကိုတော့ ယဉ်ကျေးတဲ့ စာသားနဲ့ retry ကိုပဲ ပြပါတယ်။
  • not-found.tsx မှာ search box ဒါမှမဟုတ် လူကြိုက်များတဲ့ item တွေကို ပြပြီး user ကို dead end မဖြစ်စေပါဘူး။
  • Form validation လို မျှော်မှန်းထားတဲ့ error တွေကို throw မလုပ်ဘဲ value အဖြစ် ပြန်ပေးပြီး form ဘေးမှာ ပြပါတယ် (lesson 18)။

အမှားများ

ဖြစ်လေ့ရှိတဲ့ အမှားများ

error.tsx မှာ "use client" မေ့ခြင်း

Error boundary တွေက Client Component ဖြစ်ရပါမယ်။ Directive မပါရင် Next.js က build/dev error ပေးပြီး onClick handler လည်း အလုပ်မလုပ်ပါဘူး။ "use client" ကို file ရဲ့ ပထမဆုံး statement အဖြစ် import တွေ အပေါ်မှာ ရေးပါ။

notFound() ကို try/catch ထဲမှာ ခေါ်ခြင်း

notFound() နဲ့ redirect() က special error ကို throw လုပ်ပြီး အလုပ်လုပ်ပါတယ်။ သူတို့ကို try block ထဲမှာ ခေါ်ပြီး catch က အကုန်ဖမ်းလိုက်ရင် 404 UI မပေါ်ဘဲ catch ထဲက fallback ကိုပဲ ပြပါတယ်။ Data ယူတာကိုပဲ try ထဲထားပြီး notFound() ကို အပြင်မှာ ခေါ်ပါ။

မျှော်မှန်းထားတဲ့ error ကို exception အဖြစ် throw ခြင်း

Email format မှားတာ၊ note မရှိတာလို အခြေအနေတွေက bug မဟုတ်ပါဘူး။ throw လုပ်ရင် error.tsx က 'Something went wrong' ပြပြီး user ရဲ့ form input တွေပါ ပျောက်သွားနိုင်ပါတယ်။ 'မရှိ' ဆိုရင် notFound()၊ validation ဆိုရင် error message ကို value အဖြစ် ပြန်ပေးပါ။

Best Practices

Best practices

  • loading.tsx ကို ပေါ့ပေါ့ပါးပါး ထားပါ: data မယူဘဲ static skeleton ကိုပဲ ပြပါ၊ ဒါမှ ချက်ချင်း ပေါ်ပါမယ်။
  • Error UI မှာ stack trace ဒါမှမဟုတ် error.message အကြမ်းကို မပြပါနဲ့၊ user လုပ်လို့ရတဲ့ နောက်တစ်ဆင့်ကိုပဲ ပြောပြပါ။
  • DevTools မှာ network ကို နှေးအောင်လုပ်ပြီး loading၊ error နဲ့ မရှိတဲ့ id သုံးမျိုးလုံးကို deploy မလုပ်ခင် ကိုယ်တိုင် စမ်းကြည့်ပါ။

Mini challenge

Mini challenge: profile route ကို ယုံကြည်ရအောင် လုပ်ခြင်း

app/profile/ folder အတွက် file သုံးခု ရေးပါ: card နှစ်ခုပါတဲ့ loading skeleton၊ Home ကို ပြန်သွားတဲ့ link နဲ့ retry button ပါတဲ့ error UI၊ နဲ့ profile မရှိကြောင်း ပြတဲ့ not-found UI ပါ။

  • loading.tsx က card အရွယ် div နှစ်ခုကို aria-busy နဲ့ ပြရမယ်
  • error.tsx က "use client" ပါပြီး retry() ကို ခေါ်တဲ့ button ပါရမယ်
  • error.tsx မှာ next/link နဲ့ / ကို ပြန်သွားတဲ့ link ပါရမယ်
  • not-found.tsx မှာ user နားလည်တဲ့ စာသား ပါရမယ်
app/profile/
// app/profile/loading.tsx
export default function Loading() {
  // TODO: two grey card placeholders inside a container with aria-busy="true"
}

// app/profile/error.tsx
// TODO: mark this file as a Client Component
import Link from "next/link";

export default function ProfileError(/* TODO: type error and retry */) {
  // TODO: a short message, a Try again button and a link to "/"
}

// app/profile/not-found.tsx
export default function NotFound() {
  // TODO: tell the user this profile does not exist
}

ရလဒ်: /profile ကို ဖွင့်တာနဲ့ မီးခိုးရောင် card နှစ်ခု ပေါ်ပါတယ်။ Data ယူတာ fail ဖြစ်ရင် message၊ Try again button နဲ့ Back to home link ကို မြင်ရပြီး notFound() ခေါ်ရင် 'This profile does not exist.' ကို မြင်ရပါတယ်။

အရိပ်အမြွက် ကြည့်ရန်

Skeleton card က style={{ height: 120, background: "#e5e7eb" }} လို div ရိုးရိုးလေးပဲ ဖြစ်ရင် ရပါတယ်။ error.tsx ရဲ့ props type ကို { error: unknown; retry: () => void } လို့ ပေးပါ။

အဖြေ ကြည့်ရန်
app/profile/
// app/profile/loading.tsx
const card = { height: 120, borderRadius: 12, background: "#e5e7eb" };

export default function Loading() {
  return (
    <div aria-busy="true" style={{ display: "grid", gap: 16 }}>
      <div style={card} />
      <div style={card} />
    </div>
  );
}

// app/profile/error.tsx
"use client";
import Link from "next/link";

export default function ProfileError({
  retry,
}: {
  error: unknown;
  retry: () => void;
}) {
  return (
    <div role="alert">
      <p>Your profile could not be loaded right now.</p>
      <button onClick={() => retry()}>Try again</button>
      <Link href="/">Back to home</Link>
    </div>
  );
}

// app/profile/not-found.tsx
export default function NotFound() {
  return <p>This profile does not exist.</p>;
}

Quick Quiz 1 / 3

app/notes/layout.tsx ထဲမှာ exception ဖြစ်ပါတယ်။ app/error.tsx နဲ့ app/notes/error.tsx နှစ်ခုလုံး ရှိရင် ဘယ်ဟာက ဖမ်းမလဲ?

Quick Quiz 2 / 3

error.tsx ကို ဘာကြောင့် "use client" နဲ့ ရေးရတာလဲ?

Quick Quiz 3 / 3

app/layout.tsx မှာ header ရှိပြီး app/notes/loading.tsx ရှိပါတယ်။ / ကနေ /notes ကို link နှိပ်ပြီး data မရောက်ခင် user က ဘာမြင်ရမလဲ?

မှတ်ထားရမယ့် အချက်များ

  • loading.tsx က page ကို Suspense နဲ့ အလိုအလျောက် ဝိုင်းပြီး navigation မှာ ချက်ချင်း feedback ပေးပါတယ်။
  • error.tsx က "use client" ပါတဲ့ error boundary ဖြစ်ပြီး retry (version အဟောင်းမှာ reset) နဲ့ ပြန်ကြိုးစားခွင့် ပေးပါတယ်။
  • notFound() နဲ့ not-found.tsx က 'မရှိ' ကို bug နဲ့ ခွဲပြီး noindex ပါတဲ့ 404 UI ကို ပြပါတယ်။
  • Error boundary က တူညီတဲ့ folder ရဲ့ layout ကို မဝိုင်းပါ၊ root layout အတွက် global-error.tsx လိုပါတယ်။

Next.js — Error Handling — Next.js

Next.js — Error Handling — Next.js

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Loading၊ Error နှင့် Not Found UI | Thuta Learning