ရိုးရိုးလေး ရှင်းပြမယ်
Dynamic route ကို ရိုးရိုးလေး ပြောရရင်
Myanmar Notes app မှာ note တစ်ခုချင်းစီအတွက် page file တစ်ခုစီ ရေးရမယ်ဆိုရင် note တစ်ထောင်ဆို file တစ်ထောင် ဖြစ်သွားပါမယ်။ Dynamic route ကတော့ ကွက်လပ်ပါတဲ့ ဖောင်တစ်ရွက်လိုပါပဲ: page template တစ်ခုတည်း ရေးထားပြီး URL ထဲက id က ကွက်လပ်ကို ဖြည့်ပေးပါတယ်။
Folder နာမည်ကို square bracket နဲ့ ရေးလိုက်တာနဲ့ (ဥပမာ app/notes/[id]/) Next.js က အဲဒီ segment ကို 'ဘာစာသားမဆို လက်ခံမယ့် နေရာ' လို့ နားလည်ပါတယ်။ /notes/routing ကို ဖွင့်ရင် id က "routing" ဖြစ်ပြီး /notes/caching ကို ဖွင့်ရင် id က "caching" ဖြစ်ပါတယ်၊ page code ကတော့ တစ်ခုတည်းပါပဲ။
Problem → Why → Solution
ပြဿနာ: id ကို ဖတ်မရဘဲ မရှိတဲ့ note မှာ crash ဖြစ်ခြင်း
အောက်က page ကို ပထမဆုံးအကြိမ် ရေးကြည့်တယ် ဆိုပါစို့။ Bug နှစ်ခု ပါနေပါတယ်။ ပထမတစ်ခုက params ကို object ရိုးရိုးလို သုံးထားတာပါ၊ လက်ရှိ App Router မှာ params က Promise ဖြစ်လို့ params.id က TypeScript error ပေးပါတယ်။ ဒုတိယတစ်ခုက တစ်ယောက်ယောက်က /notes/does-not-exist လို့ ရိုက်လိုက်ရင် getNote က undefined ပြန်ပေးပြီး note.title ကို ဖတ်တဲ့နေရာမှာ page တစ်ခုလုံး error နဲ့ ပြိုသွားတာပါ။
import { getNote } from "@/lib/notes";
export default async function NotePage({
params,
}: {
params: Promise<{ id: string }>;
}) {
const note = await getNote(params.id); // params is a Promise: no .id on it
return <h1>{note.title}</h1>; // TypeError when the note does not exist
}ဒါက ဘာကြောင့် အရေးကြီးတာလဲ
URL ဆိုတာ user ရိုက်ချင်တာ ရိုက်လို့ရတဲ့ input ပါ။ Page က id မှန်ရင် အလုပ်လုပ်ရုံနဲ့ မလုံလောက်ပါဘူး၊ id မှားရင်လည်း 'ဒီ note မရှိပါ' လို့ ယဉ်ကျေးစွာ ပြောရပါမယ်။ notFound() ကို သုံးရင် user က ရှင်းလင်းတဲ့ 404 စာမျက်နှာ မြင်ရပြီး search engine တွေကလည်း မရှိတဲ့ URL ကို index မလုပ်တော့ပါဘူး။
Core concept: folder နာမည်က params ရဲ့ key ဖြစ်လာတယ်
Bracket ထဲက စာလုံးက params object ရဲ့ key နာမည် ဖြစ်လာပါတယ်။ app/notes/[id]/page.tsx ဆိုရင် params က { id: string } ဖြစ်ပြီး folder ကို [noteId] လို့ ပေးရင် key က noteId ဖြစ်ပါတယ်။ URL ကနေ လာတဲ့ value တွေကို string အဖြစ်ပဲ ရပါတယ် — /notes/42 ဖြစ်နေရင်တောင် id က number 42 မဟုတ်ဘဲ string "42" ပါ။ Number လိုရင် ကိုယ်တိုင် ပြောင်းပြီး NaN ဟုတ်မဟုတ် စစ်ရပါမယ်။ [id] folder ထဲမှာ ထည့်ထားတဲ့ loading.tsx ဒါမှမဟုတ် not-found.tsx လို တခြား file တွေကလည်း note အားလုံးအတွက် တစ်ပြိုင်နက် အလုပ်လုပ်ပေးပါတယ်။
Next.js 15 ကစပြီး params က Promise ဖြစ်လာပါတယ်၊ ဒါကြောင့် async Server Component ထဲမှာ const { id } = await params လို့ ရေးရပါတယ်။ Next.js 14 နဲ့ အရင် version တွေမှာတော့ ရိုးရိုး object ဖြစ်ခဲ့လို့ အင်တာနက်ပေါ်က tutorial ဟောင်းတွေမှာ params.id ကို တိုက်ရိုက် သုံးထားတာ တွေ့ရပါလိမ့်မယ်။ Client Component page ဖြစ်ရင် React ရဲ့ use(params) ဒါမှမဟုတ် useParams() hook ကို သုံးပါ။ အသေးစိတ်ကို Next.js docs ရဲ့ Dynamic Segments စာမျက်နှာမှာ ကြည့်ပါ။
| Folder | URL ဥပမာ | params |
|---|---|---|
| app/notes/[id] | /notes/routing | { id: "routing" } |
| app/docs/[...slug] | /docs/a/b | { slug: ["a", "b"] } |
| app/docs/[[...slug]] | /docs | { slug: undefined } |
Data မတွေ့တဲ့အခါ notFound() ကို next/navigation ကနေ import လုပ်ပြီး ခေါ်ပါ။ သူက special error တစ်ခုကို throw လုပ်တဲ့အတွက် အဲဒီနောက်က code တွေ မ run တော့ဘဲ Next.js က အနီးဆုံး not-found.tsx ကို render လုပ်ပါတယ်။ သူ့ return type က never ဖြစ်လို့ if (!note) notFound(); ရဲ့ အောက်မှာ TypeScript က note ကို undefined မဟုတ်တော့ဘူးလို့ သိပြီး note.title ကို error မပေးဘဲ ဖတ်ခွင့်ပေးပါတယ်။
Dynamic segment တွေကို အထပ်ထပ် ထားလို့လည်း ရပါတယ်: app/[category]/[item]/page.tsx က /books/atlas အတွက် { category: "books", item: "atlas" } ကို ပေးပါတယ်။ အဲဒီ params Promise တစ်ခုတည်းက page တင်မကဘဲ layout.tsx၊ route.ts နဲ့ generateMetadata (lesson 11) ဆီကိုလည်း ရောက်ပါတယ်။ Layout က သူ့အောက်က segment တွေရဲ့ params ကိုတော့ မမြင်ရပါဘူး၊ သူ့ အထက်နဲ့ သူ့ကိုယ်ပိုင် segment ရဲ့ params ကိုပဲ ရပါတယ်။
- URL /notes/routing ဝင်လာ
- [id] folder က "routing" ကို ဖမ်း
- await params က { id } ကို ပေး
- getNote(id) နဲ့ record ရှာ
- မတွေ့ရင် notFound()၊ တွေ့ရင် render
params ထဲက value တွေဟာ user ရိုက်ထည့်တဲ့ စာသားပါ၊ database ကနေ စစ်ပြီးမှ ယုံပါ။
အထွေထွေ ပုံစံ
export default async function Page({
params,
}: {
params: Promise<{ param: string }>;
}) {
const { param } = await params;
const item = await loadItem(param);
if (!item) notFound();
return <ItemView item={item} />;
}Folder နာမည်နဲ့ params ရဲ့ key ကို တူအောင်ထားပါ၊ params ကို await လုပ်ပြီး data layer ကို မေးပါ၊ မတွေ့ရင် notFound() ကို ခေါ်ပြီးမှ render လုပ်ပါ။
Worked example: note detail page
အောက်က file ကို app/notes/[id]/page.tsx မှာ ထားပါ။ getNote က project ရဲ့ data layer function ဖြစ်ပြီး id နဲ့ note ကို ရှာပေးကာ မတွေ့ရင် undefined ပြန်ပေးတယ်လို့ ယူဆထားပါတယ်။ Problem section ထဲက bug နှစ်ခုလုံးကို ဒီ version က ပြင်ထားတာကို သတိထားကြည့်ပါ။
import { notFound } from "next/navigation";
import { getNote } from "@/lib/notes";
export default async function NotePage({
params,
}: {
params: Promise<{ id: string }>;
}) {
const { id } = await params;
const note = await getNote(id);
if (!note) notFound();
return (
<article>
<h1>{note.title}</h1>
<p>{note.content}</p>
</article>
);
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Type the Promise
params: Promise<{ id: string }>;params ကို Promise အဖြစ် type ပေးထားလို့ await မလုပ်ဘဲ .id ကို ဖတ်ရင် compiler က ချက်ချင်း သတိပေးပါတယ်။ Key နာမည် id က folder [id] နဲ့ တူရပါမယ်။
- Unwrap the params
const { id } = await params;Promise ကို await လုပ်ပြီး id ကို destructure လုပ်ပါတယ်။ Component က async ဖြစ်လို့ ဒီလို ရေးလို့ ရပါတယ်။
- Ask the data layer
const note = await getNote(id);Server ပေါ်မှာ run တဲ့အတွက် database ဒါမှမဟုတ် API ကို တိုက်ရိုက် မေးလို့ ရပါတယ်။ ရလဒ်က Note ဒါမှမဟုတ် undefined ဖြစ်ပါတယ်။
- Guard with notFound
if (!note) notFound();Note မရှိရင် rendering ကို ဒီနေရာမှာ ရပ်ပြီး not-found UI ကို ပြပါတယ်။ notFound() က never ပြန်တဲ့အတွက် အောက်က line တွေမှာ note ကို သေချာပေါက် ရှိတယ်လို့ TypeScript က သိပါတယ်။
- Render the record
<h1>{note.title}</h1>ဒီအထိ ရောက်လာရင် note တကယ်ရှိနေပြီမို့ title နဲ့ content ကို လုံခြုံစွာ ပြလို့ ရပါပြီ။
ရှိသော id အတွက် note detail ပေါ်ပြီး မရှိသော id အတွက် 404 UI ပေါ်မည်။generateStaticParams: build time မှာ path တွေ ကြိုဖန်တီးခြင်း
Default အားဖြင့် dynamic page ကို request ဝင်လာမှ render လုပ်ပါတယ်။ လူကြည့်များတဲ့ note တွေကို build လုပ်တုန်းကတည်းက HTML အဆင်သင့် ဖြစ်နေစေချင်ရင် page file ထဲမှာ generateStaticParams ကို export လုပ်ပါ။ သူက params object တွေရဲ့ array ကို ပြန်ပေးပြီး Next.js က အဲဒီ path တစ်ခုချင်းစီကို build time မှာ render လုပ်ထားပါတယ်။ List ထဲမပါတဲ့ id တွေကတော့ request ဝင်လာတဲ့အချိန်မှ render လုပ်ပါတယ် (dynamicParams = false လို့ export လုပ်ရင်တော့ အဲဒီ id တွေက 404 ဖြစ်ပါတယ်)။
import { getPopularNoteIds } from "@/lib/notes";
// Each object is one set of params; ids must be strings.
export async function generateStaticParams() {
const ids = await getPopularNoteIds(); // e.g. ["routing", "caching"]
return ids.map((id) => ({ id }));
}Cache Components ဖွင့်ထားရင်
Cache Components (lesson 15) ကို ဖွင့်ထားတဲ့ project မှာ generateStaticParams မပါရင် params ကို runtime data အဖြစ် သတ်မှတ်ပြီး params ကို ဖတ်တဲ့ အပိုင်းကို Suspense boundary ဒါမှမဟုတ် loading.tsx နဲ့ ဝိုင်းဖို့ လိုပါတယ်။ ဒီ rule ကို lesson 15 မှာ အသေးစိတ် ပြန်ကြည့်ပါမယ်။
လက်တွေ့ project တွေမှာ
လက်တွေ့ project တွေမှာ dynamic route
Detail page တိုင်းနီးပါးက dynamic route ပါ: online shop ရဲ့ /products/[slug]၊ blog ရဲ့ /posts/[slug]၊ social app ရဲ့ /users/[username] စတာတွေပါ။ Production မှာ ဒီ page တွေကို ရေးတဲ့အခါ အောက်ပါ အလေ့အထတွေကို မကြာခဏ တွေ့ရပါတယ်။
- Database id အစား ဖတ်ရလွယ်တဲ့ slug (ဥပမာ nextjs-routing-basics) ကို URL မှာ သုံးကြပါတယ်၊ link share ရတာ လွယ်ပြီး search result မှာလည်း ပိုရှင်းပါတယ်။
- params ကို database မမေးခင် format စစ်ပါတယ် — ဥပမာ number id ဖြစ်ရမယ့်နေရာမှာ Number(id) က NaN ဖြစ်ရင် query မလုပ်ဘဲ notFound() ကို တန်းခေါ်ပါတယ်။
- ဈေးဝယ်သူ အများဆုံးကြည့်တဲ့ product အနည်းငယ်ကို generateStaticParams နဲ့ ကြိုဖန်တီးပြီး ကျန်တာတွေကို request ဝင်လာမှ render လုပ်စေပါတယ်၊ build time ကိုလည်း တိုစေပါတယ်။
- User ပိုင်တဲ့ data (ဥပမာ /invoices/[id]) ဆိုရင် record ရှိရုံနဲ့ မပြဘဲ login ဝင်ထားတဲ့ user ပိုင်မပိုင် စစ်ပါတယ်၊ ဒါကို lesson 20 မှာ ဆက်လေ့လာပါမယ်။
အမှားများ
Dynamic route နဲ့ ဖြစ်လေ့ရှိတဲ့ အမှားများ
params ကို await မလုပ်ဘဲ သုံးခြင်း
Next.js 14 ခေတ် tutorial ကနေ ကူးလာတဲ့ params.id က လက်ရှိ version မှာ Promise ပေါ်က property ဖြစ်သွားလို့ type error ဒါမှမဟုတ် undefined ကို ပေးပါတယ်။ Next.js 15 မှာ migration အတွက် synchronous access ကို warning နဲ့ ခဏ ခွင့်ပြုထားပေမဲ့ အဲဒါကို အားမကိုးပါနဲ့၊ အမြဲ await params ဒါမှမဟုတ် use(params) ကို သုံးပါ။
Record မရှိတဲ့အခါ undefined ကို render လုပ်ခြင်း
if (!note) စစ်ချက် မပါရင် မရှိတဲ့ id တစ်ခုက 'Cannot read properties of undefined' error ဖြစ်စေပြီး user က 404 အစား error page မြင်ရပါတယ်။ return null လို့ ရေးတာလည်း မကောင်းပါဘူး — စာမျက်နှာ အလွတ်ကို 200 status နဲ့ ပို့ပါတယ်။ notFound() ကသာ မှန်ကန်တဲ့ 404 UI နဲ့ noindex ကို ပေးပါတယ်။
Folder နာမည်နဲ့ destructure လုပ်တဲ့ key မကိုက်ခြင်း
Folder ကို [noteId] လို့ ပေးထားပြီး const { id } = await params လို့ ရေးရင် id က undefined ဖြစ်ပြီး getNote(undefined) က အမြဲ မတွေ့ပါဘူး။ Page တိုင်းက 404 ဖြစ်နေရင် ပထမဆုံး folder နာမည်နဲ့ params type ကို နှိုင်းယှဉ်ကြည့်ပါ။
Best Practices
Best practices
- Folder နာမည်ကို ရှင်းရှင်းပေးပါ: [id] ထက် [noteSlug] က ဘာဝင်လာမလဲ ပိုပြောပြပါတယ်။
- params ကို data layer ဆီ မပို့ခင် format စစ်ပြီး မမှန်ရင် notFound() ကို စောစောခေါ်ပါ။
- Detail page တိုင်းအတွက် not-found.tsx ကို သက်ဆိုင်ရာ route folder ထဲမှာ ထားပြီး list page ကို ပြန်သွားတဲ့ link ထည့်ပါ။
Mini challenge
Mini challenge: user profile page
app/users/[username]/page.tsx ကို ရေးပါ။ URL ထဲက username နဲ့ user ကို ရှာပြီး ရှိရင် နာမည်နဲ့ bio ကို ပြပါ၊ မရှိရင် 404 ကို ပြပါ။ User နှစ်ယောက်ကိုတော့ build time မှာ ကြိုဖန်တီးထားပါ။
- params ကို Promise<{ username: string }> အဖြစ် type ပေးပြီး await လုပ်ပါ
- User မရှိရင် notFound() ကို ခေါ်ပါ
- generateStaticParams က "aye" နဲ့ "min" ကို ပြန်ပေးရပါမယ်
- Heading မှာ @username ကို ပြပါ
import { notFound } from "next/navigation";
type User = { username: string; name: string; bio: string };
const users: User[] = [
{ username: "aye", name: "Aye Chan", bio: "Writes about CSS." },
{ username: "min", name: "Min Thu", bio: "Learning Next.js." },
];
// TODO: export generateStaticParams that returns one object per user
// TODO: type the props so params is a Promise of { username: string }
export default async function UserPage(props) {
// TODO: await params and find the user
// TODO: call notFound() when there is no match
// TODO: render an <h1> with @username, then the name and bio
}ရလဒ်: /users/aye က "@aye" heading၊ Aye Chan နဲ့ bio ကို ပြပါတယ်။ /users/nobody က not-found UI ကို ပြပါတယ်။ next build ရဲ့ output မှာ /users/aye နဲ့ /users/min က prerender လုပ်ထားတဲ့ path အဖြစ် ပေါ်ပါတယ်။
အရိပ်အမြွက် ကြည့်ရန်
generateStaticParams က users.map((u) => ({ username: u.username })) လို object array ပြန်ရုံပါပဲ။ users.find(...) က User | undefined ကို ပြန်ပေးလို့ notFound() နဲ့ စစ်ပြီးမှ property တွေကို ဖတ်ပါ။
အဖြေ ကြည့်ရန်
import { notFound } from "next/navigation";
type User = { username: string; name: string; bio: string };
const users: User[] = [
{ username: "aye", name: "Aye Chan", bio: "Writes about CSS." },
{ username: "min", name: "Min Thu", bio: "Learning Next.js." },
];
export async function generateStaticParams() {
return users.map((user) => ({ username: user.username }));
}
export default async function UserPage({
params,
}: {
params: Promise<{ username: string }>;
}) {
const { username } = await params;
const user = users.find((u) => u.username === username);
if (!user) notFound();
return (
<section>
<h1>@{user.username}</h1>
<p>{user.name}</p>
<p>{user.bio}</p>
</section>
);
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- [id] လို bracket folder တစ်ခုက URL segment ကို ဖမ်းပြီး bracket နာမည်က params ရဲ့ key ဖြစ်လာပါတယ်။
- Next.js 15 ကစပြီး params က Promise ပါ: Server Component မှာ await params၊ Client Component မှာ use(params) ကို သုံးပါ။
- URL ကလာတဲ့ value တွေက string (catch-all ဆို string array) ဖြစ်ပြီး user input အဖြစ် စစ်ဆေးရပါမယ်။
- Record မရှိရင် notFound() ကို ခေါ်ပါ၊ generateStaticParams နဲ့ လူကြည့်များတဲ့ path တွေကို build time မှာ ကြိုဖန်တီးနိုင်ပါတယ်။
Next.js — Layouts and Pages — Next.js
Next.js — Layouts and Pages — Next.js