ရိုးရိုးလေး ရှင်းပြမယ်
Streaming ကို ရိုးရိုးလေး ပြောရရင်
ဆိုင်မှာ ထမင်းနဲ့ ဟင်းသုံးမျိုး မှာလိုက်တယ် ဆိုပါစို့။ ဟင်းတစ်မျိုးက မီးဖိုပေါ် နောက်ထပ် မိနစ် ၂၀ ကြာဦးမယ်ဆိုရင် အားလုံးကို အဲဒီ ဟင်းပြီးမှ တစ်ခါတည်း ချပေးတာထက် အဆင်သင့်ဖြစ်တာကို အရင်ချပေးပြီး ကျန်တာကို နောက်မှ ပို့ပေးတာက ပိုကောင်းပါတယ်။ Streaming ကလည်း အဲဒီလိုပါပဲ: page ရဲ့ အဆင်သင့် အပိုင်းကို ချက်ချင်းပို့ပြီး နှေးတဲ့ အပိုင်းကို ရောက်မှ ဖြည့်ပါတယ်။
Next.js မှာ နှေးတဲ့ အပိုင်းကို <Suspense> ထဲ ထည့်ပြီး fallback (စောင့်နေစဉ် ပြမယ့် UI) ပေးလိုက်ရုံပါပဲ။ ကျန်တဲ့ page က အဲဒီ အပိုင်းကို မစောင့်တော့ပါဘူး။
Problem → Why → Solution
ပြဿနာ: analytics တစ်ခုကြောင့် dashboard တစ်ခုလုံး ဖြူနေခြင်း
Dashboard မှာ user profile က 100ms နဲ့ ရပေမဲ့ analytics query က 3 စက္ကန့် ကြာပါတယ်။ အောက်က page က await နှစ်ခုလုံး ပြီးမှ return လုပ်လို့ server က ဘာ HTML မှ မပို့နိုင်ဘဲ user က 3 စက္ကန့် screen အလွတ်ကို ကြည့်နေရပါတယ်။ Profile ကို ချက်ချင်း ပြလို့ ရနေပေမဲ့ analytics ကြောင့် အကုန် စောင့်နေရတာပါ။
export default async function DashboardPage() {
const profile = await getProfile(); // about 100 ms
const analytics = await getAnalytics(); // about 3 s: nothing is sent until here
return (
<main>
<h1>Hello, {profile.name}</h1>
<AnalyticsChart data={analytics} />
</main>
);
}ဒါက ဘာကြောင့် အရေးကြီးတာလဲ
Server Component တစ်ခုက await လုပ်နေသရွေ့ အဲဒီ component ပါတဲ့ အပိုင်းကို render မလုပ်နိုင်ပါဘူး။ Suspense boundary မရှိရင် အဲဒီ 'အပိုင်း' က route တစ်ခုလုံး ဖြစ်သွားပါတယ်။ နှေးတဲ့ data ကို သူ့ကိုယ်ပိုင် component နဲ့ Suspense boundary ထဲ ရွှေ့လိုက်ရင် စောင့်ရတာက အဲဒီ နေရာလေး တစ်ခုတည်း ဖြစ်သွားပါတယ်။
Core concept: shell ကို အရင်ပို့ပြီး boundary တွေကို နောက်မှ ဖြည့်ခြင်း
<Suspense fallback={...}> က 'ဒီ အထဲက component တွေ မပြီးသေးရင် fallback ကို ပြထားပါ' လို့ React ကို ပြောတာပါ။ Server က render လုပ်တဲ့အခါ boundary အပြင်က အရာအားလုံးနဲ့ fallback ကို ပေါင်းပြီး ပထမ HTML အပိုင်း (shell) အဖြစ် ချက်ချင်း ပို့ပါတယ်။ Boundary ထဲက async component ရဲ့ data ရောက်လာတဲ့အခါ အဲဒီ HTML ကို connection တစ်ခုတည်းပေါ်ကနေ နောက်ထပ် အပိုင်းအဖြစ် ပို့ပြီး browser က fallback နေရာမှာ အစားထိုးပါတယ်။
Streaming အလုပ်လုပ်ဖို့ အရေးကြီးဆုံး rule က နှေးတဲ့ await ကို boundary ရဲ့ အတွင်းဘက် component ထဲမှာ ထားရမယ်ဆိုတာပါ။ Parent page က await getAnalytics() ကို ကိုယ်တိုင် လုပ်ပြီးမှ <Suspense> ကို return လုပ်ရင် parent ကိုယ်တိုင် စောင့်နေလို့ shell ပါ မပို့နိုင်တော့ပါဘူး။ ဒါကြောင့် နှေးတဲ့ data ကို ယူတဲ့ async component သေးသေးလေး တစ်ခု ခွဲထုတ်ပြီး parent ကို await မပါအောင် ထားပါ။
Lesson 10 ရဲ့ loading.tsx က တကယ်တော့ page တစ်ခုလုံးကို ဝိုင်းထားတဲ့ Suspense boundary ပါပဲ။ Page တစ်ခုလုံးအတွက် loading UI လိုရင် အဆင်ပြေပေမဲ့ heading နဲ့ profile ကို ချက်ချင်းပြပြီး chart တစ်ခုကိုပဲ စောင့်စေချင်ရင် ကိုယ်တိုင် ထားတဲ့ <Suspense> က ပိုတိကျပါတယ်။ Sibling boundary တွေက တစ်ခုနဲ့တစ်ခု မစောင့်ဘဲ ကိုယ့် data ရောက်တာနဲ့ ကိုယ် ပေါ်လာပါတယ်။
| Tool | Boundary ရဲ့ အကျယ် | သုံးသင့်တဲ့နေရာ |
|---|---|---|
| loading.tsx | Route segment တစ်ခုလုံး | Page တစ်ခုလုံး data စောင့်ရတဲ့အခါ |
| <Suspense> | ကိုယ်ရွေးတဲ့ component | Page ထဲက နှေးတဲ့ အပိုင်းတစ်ခုအတွက် |
Fallback ကို 'Loading…' စာကြောင်းတစ်ကြောင်းထက် ရောက်လာမယ့် content နဲ့ အရွယ်တူ skeleton အဖြစ် ရေးပါ။ အရွယ်မတူရင် content ရောက်တဲ့အခါ အောက်က အရာတွေ ခုန်ဆင်းသွားပြီး Cumulative Layout Shift (CLS) score ဆိုးသွားပါတယ်။ Boundary တွေကို အထပ်ထပ် nest လုပ်လို့လည်း ရပါတယ်: အပြင်ဘက် fallback က panel တစ်ခုလုံးကို ဖုံးပြီး အတွင်းဘက် boundary က အဲဒီ panel ရဲ့ အစိတ်အပိုင်းတစ်ခုကို ကျန်တာထက် အရင် ပေါ်စေပါတယ်။
- Boundary အပြင်ဘက် + fallback = shell
- Shell ကို ချက်ချင်း ပို့
- Boundary ထဲက data ရောက်
- HTML အပိုင်းက fallback ကို အစားထိုး
Boundary အပြင်ဘက်က await တိုင်းက shell ကို နောက်ကျစေပါတယ်။
အထွေထွေ ပုံစံ
async function SlowPart() {
const data = await loadSlowData(); // the await lives inside the boundary
return <Result data={data} />;
}
export default function Page() {
return (
<Layout>
<FastPart />
<Suspense fallback={<SameSizeSkeleton />}><SlowPart /></Suspense>
</Layout>
);
}နှေးတဲ့ data ကို async component တစ်ခုထဲ ထည့်ပြီး react ကနေ import လုပ်ထားတဲ့ Suspense နဲ့ ဝိုင်းပါ။ Page component ကိုယ်တိုင်က အဲဒီ data ကို await မလုပ်ပါဘူး။
Worked example: note list ကို stream လုပ်ခြင်း
အပေါ်ဆုံးက import line က getSlowNotes ကို ယူလာပါတယ်။ getSlowNotes ကို နှေးတဲ့ database query လို့ ယူဆပါ။ Page ရဲ့ heading က ချက်ချင်း ပေါ်ပြီး note list ကိုပဲ စောင့်ရပါတယ်။
import { Suspense } from "react";
import { getSlowNotes } from "@/lib/notes";
async function SlowNotes() {
const notes = await getSlowNotes();
return notes.map((note) => <article key={note.id}>{note.title}</article>);
}
export default function NotesPage() {
return (
<main>
<h1>Notes</h1>
<Suspense fallback={<p>Loading the note list…</p>}>
<SlowNotes />
</Suspense>
</main>
);
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- The slow component
async function SlowNotes() {နှေးတဲ့ data ကို ယူတဲ့ အလုပ်ကို component သေးသေးလေး တစ်ခုထဲ ခွဲထုတ်ထားပါတယ်၊ ဒါမှ သူ့ကို boundary ထဲ ထည့်လို့ ရပါမယ်။
- Await inside the boundary
const notes = await getSlowNotes();ဒီ await က SlowNotes ထဲမှာ ရှိလို့ စောင့်ရတာ SlowNotes တစ်ခုတည်းပါ။ NotesPage က await တစ်ခုမှ မပါလို့ async ပင် မဖြစ်ပါဘူး။
- Sent in the shell
<h1>Notes</h1>Boundary အပြင်ဘက်မှာ ရှိလို့ ပထမ HTML အပိုင်းထဲမှာ fallback နဲ့အတူ ချက်ချင်း ပါသွားပါတယ်။
- The boundary
<Suspense fallback={<p>Loading the note list…</p>}>SlowNotes မပြီးသေးသရွေ့ fallback ကို ပြပြီး ပြီးတာနဲ့ note list က အစားထိုးပါတယ်။ Production မှာ ဒီ စာကြောင်းကို list နဲ့ အရွယ်တူ skeleton နဲ့ ပြောင်းပါ။
"Notes" heading နဲ့ "Loading the note list…" fallback က ချက်ချင်း ပေါ်ပါတယ်။ getSlowNotes ပြီးတဲ့အခါ fallback နေရာမှာ note title တွေ (ဥပမာ Routing၊ Caching) ကို <article> အဖြစ် မြင်ရပါတယ်။Wire ပေါ်မှာ ဘာတွေ သွားလဲ
Streaming ကို မျက်စိနဲ့ မြင်ရအောင် React ရဲ့ renderToPipeableStream နဲ့ ဒီ example ကို Node မှာ run ကြည့်ခဲ့ပါတယ် (getSlowNotes က 300ms စောင့်တဲ့ fake function)။ ပထမ chunk မှာ heading နဲ့ fallback ပါပြီး fallback ကို marker comment တွေနဲ့ ဝိုင်းထားပါတယ်။ 300ms ကြာမှ ဒုတိယ chunk က hidden div ထဲမှာ article တွေနဲ့ fallback ကို အစားထိုးမယ့် script လေးကို ပို့ပါတယ်။ Next.js ရဲ့ တကယ့် output မှာ RSC data ပါ ပါလို့ ပိုရှည်ပေမဲ့ သဘောတရား တူပါတယ်။
Two chunks of one response (simplified)
---------------------------------------
t = 0 ms <main><h1>Notes</h1>
<!--$?--><template id="B:0"></template>
<p>Loading the note list…</p><!--/$--></main>
t = 300 ms <div hidden id="S:0">
<article>Routing</article><article>Caching</article>
</div>
<script>/* move S:0 into B:0, drop the fallback */</script>Status code နဲ့ crawler
Shell ပို့ပြီးတာနဲ့ HTTP status က 200 အဖြစ် သတ်မှတ်ပြီးသား ဖြစ်လို့ boundary ထဲမှာ notFound() ခေါ်ရင် UI မှာ 404 ပြပေမဲ့ status မပြောင်းပါဘူး (Next.js က noindex tag ထည့်ပေးပါတယ်)။ JavaScript မ run နိုင်တဲ့ bot အချို့အတွက်တော့ Next.js က streaming မလုပ်ဘဲ page အပြည့်ကို စောင့်ပြီး ပို့ပါတယ်။
လက်တွေ့ project တွေမှာ
လက်တွေ့ project တွေမှာ
Streaming က data source မြန်နှုန်း မတူတဲ့ page တွေမှာ အကျိုးအများဆုံး ရပါတယ်။ E-commerce product page မှာ product အမည်၊ ဈေး၊ ပုံကို ချက်ချင်းပြပြီး recommendation နဲ့ review တွေကို သီးခြား boundary တွေထဲကနေ stream လုပ်ကြပါတယ်။ User က ဝယ်ဖို့ လိုတဲ့ အချက်အလက်ကို ပထမဆုံး မြင်ရပါတယ်။
- Analytics dashboard တွေမှာ chart တစ်ခုချင်းစီကို ကိုယ်ပိုင် Suspense boundary နဲ့ ထားလို့ မြန်တဲ့ chart တွေက နှေးတဲ့ chart ကို မစောင့်ရပါဘူး၊ fail ဖြစ်တဲ့ chart အတွက်လည်း ကိုယ်ပိုင် error boundary ပေးလို့ ရပါတယ်။
- Skeleton component တွေကို design system ထဲမှာ card၊ table row၊ chart အရွယ်အလိုက် ကြိုလုပ်ထားပြီး fallback အဖြစ် ပြန်သုံးကြပါတယ်။
- Self-host လုပ်တဲ့အခါ nginx လို reverse proxy က response ကို buffer လုပ်ရင် streaming ပျက်သွားနိုင်လို့ deployment config မှာ buffering ကို ပိတ်ဖို့ Next.js self-hosting guide ကို စစ်ပါတယ် (lesson 27)။
အမှားများ
Streaming နဲ့ ဖြစ်လေ့ရှိတဲ့ အမှားများ
Data ကို Suspense အပြင်ဘက်မှာ await လုပ်ခြင်း
const notes = await getSlowNotes(); ကို page ထဲမှာ ရေးပြီး <Suspense><NoteList notes={notes} /></Suspense> လို့ ပြန်ပေးရင် boundary က ဘာမှ မစောင့်ရတော့လို့ fallback ဘယ်တော့မှ မပေါ်ဘဲ page တစ်ခုလုံး စောင့်ပါတယ်။ Await ကို boundary ထဲက component ထဲ ရွှေ့ပါ။
အရွယ်မတူတဲ့ fallback ကြောင့် layout ခုန်ခြင်း
စာကြောင်းတစ်ကြောင်း fallback က 20px ပဲ မြင့်ပြီး ရောက်လာတဲ့ list က 600px ရှိရင် အောက်က footer နဲ့ button တွေ ရုတ်တရက် ဆင်းသွားလို့ user က မှားနှိပ်မိပါတယ်။ Fallback ကို min-height ဒါမှမဟုတ် skeleton row အရေအတွက်နဲ့ content အရွယ်နီးပါး ဖြစ်အောင် လုပ်ပါ။
Boundary တစ်ခုတည်းထဲ နှေးတာ အားလုံး ထည့်ခြင်း
Chart သုံးခုကို Suspense တစ်ခုတည်းနဲ့ ဝိုင်းရင် အနှေးဆုံး chart ပြီးမှ သုံးခုလုံး တစ်ပြိုင်နက် ပေါ်ပါတယ်။ တစ်ခုနဲ့တစ်ခု မဆိုင်ရင် boundary သီးခြားစီ ပေးပါ။
Best Practices
Best practices
- Boundary ကို user ရဲ့ အမြင်နဲ့ ဆွဲပါ: တစ်ပြိုင်နက် ပေါ်သင့်တဲ့ အရာတွေကို boundary တစ်ခုတည်းထဲ၊ သီးခြား ပေါ်လို့ရတာတွေကို boundary ခွဲပြီး ထားပါ။
- Heading၊ navigation နဲ့ page ရဲ့ အဓိက အချက်အလက်ကို shell ထဲမှာ ထားပြီး အပိုအချက်အလက်ကိုပဲ stream လုပ်ပါ။
- DevTools မှာ network ကို နှေးအောင်လုပ်ပြီး ဒါမှမဟုတ် React DevTools နဲ့ Suspense ကို toggle လုပ်ပြီး fallback တွေကို ကိုယ်တိုင် စစ်ကြည့်ပါ။
Mini challenge
Mini challenge: profile ကို ချက်ချင်း၊ activity ကို stream
/me page ကို ရေးပါ။ Profile (မြန်) ကို ချက်ချင်း ပြပြီး activity list (2 စက္ကန့် ကြာ) ကို Suspense နဲ့ stream လုပ်ရပါမယ်။ Fallback က activity row သုံးကြောင်းနဲ့ အရွယ်တူ skeleton ဖြစ်ရပါမယ်။
- Page component က getActivity() ကို await မလုပ်ရဘူး
- ActivityList က async Server Component ဖြစ်ပြီး Suspense ထဲမှာ ရှိရမယ်
- ActivitySkeleton က row သုံးကြောင်း ပြရမယ်၊ row တစ်ကြောင်း 48px
- Profile name က shell ထဲမှာ ပါရမယ်
import { Suspense } from "react";
import { getProfile, getActivity } from "@/lib/me"; // getActivity takes ~2 s
// TODO: async component that awaits getActivity() and renders a <ul>
async function ActivityList() {}
// TODO: three grey rows, 48px each, so nothing jumps when the list arrives
function ActivitySkeleton() {}
export default async function MePage() {
const profile = await getProfile(); // fast, fine to await here
// TODO: render the name, then ActivityList inside Suspense with the skeleton
}ရလဒ်: Page ဖွင့်တာနဲ့ profile name နဲ့ မီးခိုးရောင် row သုံးကြောင်း ချက်ချင်း ပေါ်ပါတယ်။ 2 စက္ကန့်လောက်ကြာရင် row တွေ နေရာမှာ activity list ဝင်လာပြီး အောက်က content တွေ မခုန်ပါဘူး။
အရိပ်အမြွက် ကြည့်ရန်
getProfile က မြန်လို့ page ထဲမှာ await လုပ်လို့ ရပါတယ်၊ shell က 100ms လောက်ပဲ နောက်ကျပါတယ်။ getActivity ကိုတော့ ActivityList ထဲမှာပဲ ခေါ်ပါ။ Skeleton row တွေကို Array.from({ length: 3 }) နဲ့ ဖန်တီးလို့ ရပါတယ်။
အဖြေ ကြည့်ရန်
import { Suspense } from "react";
import { getProfile, getActivity } from "@/lib/me";
async function ActivityList() {
const items = await getActivity(); // only this component waits
return (
<ul>
{items.map((item) => (
<li key={item.id} style={{ height: 48 }}>{item.text}</li>
))}
</ul>
);
}
function ActivitySkeleton() {
return (
<ul aria-busy="true">
{Array.from({ length: 3 }, (_, i) => (
<li key={i} style={{ height: 48, background: "#e5e7eb", marginBottom: 4 }} />
))}
</ul>
);
}
export default async function MePage() {
const profile = await getProfile();
return (
<main>
<h1>{profile.name}</h1>
<Suspense fallback={<ActivitySkeleton />}>
<ActivityList />
</Suspense>
</main>
);
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Suspense boundary အပြင်ဘက်က UI နဲ့ fallback တွေက shell အဖြစ် ချက်ချင်း ပို့ခံရပြီး boundary ထဲက content ကို နောက်မှ stream လုပ်ပါတယ်။
- နှေးတဲ့ await ကို boundary ထဲက async component ထဲမှာ ထားပါ၊ parent ထဲမှာ await လုပ်ရင် streaming ပျက်ပါတယ်။
- loading.tsx က page တစ်ခုလုံးအတွက်၊ <Suspense> က page ထဲက အပိုင်းတစ်ခုအတွက် ဖြစ်ပြီး sibling boundary တွေက သီးခြားစီ ပေါ်ပါတယ်။
- Fallback ကို content နဲ့ အရွယ်တူ skeleton အဖြစ် ရေးပြီး layout shift ကို ရှောင်ပါ။
Next.js — Fetching Data — Next.js
Next.js — Fetching Data — Next.js