ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
ဒီ lesson မှာ အသစ် မသင်ပါဘူး။ Chapter အစောပိုင်းက routing နဲ့ component စည်းမျဉ်းတွေကို သင်ခဲ့ပြီးပြီ။ အခုက လက်နဲ့ ကိုယ်တိုင် ရေးကြည့်ရမယ့် အချိန်ပါ။ ကားမောင်းသင်တန်းမှာ စာအုပ်ဖတ်ပြီးရင် ကွင်းထဲမှာ ကိုယ်တိုင်မောင်းရသလိုပဲ ဖတ်ထားတာနဲ့ ရေးနိုင်တာ မတူပါဘူး။
Task တစ်ခုစီက မိနစ် 10 လောက်ပဲ ကြာပါတယ်။ Task တိုင်းမှာ "ဘယ် URL ပေါ်မလဲ" ကို အရင် ခန့်မှန်း၊ ပြီးမှ ရေး၊ browser မှာ စစ်ပါ။ ခန့်မှန်းချက် မှားတဲ့နေရာက နားလည်မှု လိုနေတဲ့ နေရာပါ။
Problem → Why → Solution
ပြဿနာ: ဖတ်တုန်းက နားလည်တယ် ထင်ခဲ့တာ
Lesson တွေ ဖတ်တုန်းက အကုန်ရှင်းပါတယ်။ ဒါပေမယ့် ကိုယ်တိုင် ရေးကြည့်တော့ အမှားသုံးခု တစ်ပြိုင်နက် ပေါ်လာပါတယ်။ Route group ကို parentheses မပါဘဲ marketing/ လို့ ရေးမိလို့ URL က /marketing/pricing ဖြစ်သွားတယ်။ Product page က params.slug ကို တိုက်ရိုက်ဖတ်လို့ TypeScript က ငြင်းတယ်။ Counter မှာ "use client" မေ့လို့ useState က Client Component မှာပဲ သုံးလို့ရတယ်ဆိုတဲ့ error ပေါ်လာတယ်။
ဒီ error တွေက concept မသိလို့ မဟုတ်ပါဘူး။ လက်က အလေ့အကျင့် မရသေးလို့ပါ။
// app/marketing/pricing/page.tsx <- should be (marketing): URL is /marketing/pricing
// app/products/[slug]/page.tsx
export default function ProductPage({ params }: { params: { slug: string } }) {
return <h1>Product: {params.slug}</h1>; // params is a Promise in current Next.js
}
// components/Counter.tsx <- no "use client" at the top
import { useState } from "react";
export default function Counter() { const [n, setN] = useState(0); /* ... */ }ဘာကြောင့် လေ့ကျင့်ခန်း လိုတာလဲ
File-system routing မှာ စည်းမျဉ်းတွေက folder နာမည်ရဲ့ စာလုံးတစ်လုံးချင်းစီ ([ ]၊ ( )) နဲ့ file ရဲ့ ပထမဆုံး line ပေါ်မှာ မူတည်ပါတယ်။ အဲဒီ အသေးစိတ်တွေကို ကိုယ်တိုင် မှားကြည့်ပြီး ပြင်ကြည့်မှ မှတ်မိပါတယ်။ ဖတ်ရုံနဲ့တော့ မမှတ်မိပါဘူး။
Core concept: task လေးခုနဲ့ စစ်ပုံ
Project တစ်ခု (npx create-next-app@latest practice-1) ကို ဖွင့်ပြီး task လေးခုကို အစဉ်လိုက် လုပ်ပါ။ Task တစ်ခုစီမှာ concept တစ်ခုစီ ရှိပြီး browser မှာ ဘယ်လို စစ်ရမလဲဆိုတာ အောက်က table မှာ ပါပါတယ်။
| Task | လေ့ကျင့်မယ့် concept | ဘယ်လို စစ်မလဲ |
|---|---|---|
| 1. app/about/page.tsx + nav Link | Static route၊ Link | /about ပေါ်ပြီး nav နှိပ်ရင် reload မဖြစ် |
| 2. app/products/[slug]/page.tsx | Dynamic segment | /products/abc နဲ့ /products/123 heading ကွဲ |
| 3. app/(marketing)/layout.tsx | Route group | /pricing မှာပဲ နောက်ခံအရောင် ပြောင်း |
| 4. components/Counter.tsx | Client boundary | Click တိုင်း count တက် |
Bracket နဲ့ parentheses
[slug] က "ဒီ segment မှာ ဘာတန်ဖိုးမဆို" လို့ ဆိုလိုပြီး တန်ဖိုးကို params.slug အဖြစ် ရပါတယ်။ (marketing) ကတော့ URL ထဲ လုံးဝ မပါဘဲ folder တွေကို အုပ်စုဖွဲ့ပြီး အုပ်စုအတွက် သီးသန့် layout ထည့်ဖို့ပဲ သုံးပါတယ်။ ဒါကြောင့် app/(marketing)/pricing/page.tsx က /pricing ဖြစ်ပါတယ်။
page.tsx ပါမှ route ဖြစ်
Folder ထဲမှာ page.tsx မရှိရင် အဲဒီ URL ကို ဖွင့်လို့ မရပါဘူး။ ဒါကြောင့် app/products/ProductCard.tsx လို component file ကို app/ ထဲ ထားလည်း route မဖြစ်ပါဘူး။
Client boundary ကို သေးသေးထား
"use client" ကို file ရဲ့ ထိပ်ဆုံးမှာ ရေးရပြီး အဲဒီ file နဲ့ သူ import လုပ်တဲ့ module တွေက browser bundle ထဲ ရောက်ပါတယ်။ About page တစ်ခုလုံးကို client လုပ်စရာ မလိုပါဘူး။ Button ပါတဲ့ Counter ကိုပဲ client လုပ်ပြီး Server Component ဖြစ်တဲ့ page ကနေ <Counter /> လို့ render လုပ်ပါ။
Task တစ်ခုပြီးတိုင်း dev server terminal ကို ကြည့်ပါ။ TypeScript error နဲ့ route conflict လို ပြဿနာတွေက browser မှာ မပေါ်ခင် terminal မှာ အရင်ပေါ်လေ့ရှိပါတယ်။ Task တစ်ခု အလုပ်မလုပ်ရင် ကျန်တဲ့ task ကို မသွားခင် ပြင်ပါ။ Task လေးခုက တစ်ခုပေါ် တစ်ခု ဆင့်ထားတာပါ၊ ဥပမာ Task 4 ရဲ့ Counter ကို Task 1 ရဲ့ About page ထဲမှာ သုံးပါတယ်။ ဒါကြောင့် မပြီးသေးတဲ့ task ကို ကျော်သွားရင် နောက်ပိုင်း error ရဲ့ အရင်းအမြစ်ကို ရှာရခက်ပါမယ်။
- Folder path ကို ကြည့်ပြီး URL ခန့်မှန်း
- File ကို အနည်းဆုံး code နဲ့ ရေး
- Browser မှာ ခန့်မှန်းချက်ကို စစ်
- မှားရင် ဘာကြောင့်လဲ မှတ်ထား
ခန့်မှန်းချက် မှားတာက ကောင်းပါတယ်။ အဲဒီနေရာက ကိုယ် တကယ် သင်ယူနေတဲ့ နေရာပါ။
အခြေခံ ပုံစံ: folder name → URL
app/page.tsx -> /
app/<name>/page.tsx -> /<name>
app/<name>/[param]/page.tsx -> /<name>/<any value> params.param
app/(group)/<name>/page.tsx -> /<name> (group) is not in the URL
app/(group)/layout.tsx -> wraps only the pages inside (group)
app/<name>/Widget.tsx -> no URL (not a page file)Square bracket က တန်ဖိုးကို ဖမ်းပြီး parentheses က URL ထဲ မပါဘဲ အုပ်စုဖွဲ့ပါတယ်။ page.tsx ပါမှသာ URL တစ်ခု ဖြစ်ပါတယ်။
Task လေးခုရဲ့ နမူနာ အဖြေ
ကိုယ်တိုင် အရင်ကြိုးစားပြီးမှ ဒီ code နဲ့ တိုက်စစ်ပါ။ Task တစ်ခုစီက // comment မှာ ပြထားတဲ့ file သီးသန့်ပါ။ Task 1 မှာ root layout ရဲ့ nav ထဲ <Link href="/about">About</Link> ကိုလည်း ထည့်ပါ။ (marketing)/layout.tsx က root layout ထဲမှာ ထပ်ဆင့်ပြီး render ဖြစ်တာပါ၊ root layout ကို အစားမထိုးပါဘူး။
// Task 1 - app/about/page.tsx -> "/about" (also add <Link href="/about"> to the nav)
import Counter from "@/components/Counter";
export default function AboutPage() {
return (
<section>
<h1>About this shop</h1>
<p>This text is rendered on the server.</p>
<Counter />
</section>
);
}
// Task 2 - app/products/[slug]/page.tsx -> "/products/blue-mug"
type Props = { params: Promise<{ slug: string }> };
export default async function ProductPage({ params }: Props) {
const { slug } = await params;
const name = slug.replaceAll("-", " ");
return <h1>Product: {name}</h1>;
}
// Task 3 - app/(marketing)/layout.tsx wraps /pricing, not /about
export default function MarketingLayout({ children }: { children: React.ReactNode }) {
return <div style={{ background: "#fff7e6", padding: 24 }}>{children}</div>;
}
// app/(marketing)/pricing/page.tsx -> "/pricing" (no "(marketing)" in the URL)
export default function PricingPage() {
return <h1>Pricing</h1>;
}
// Task 4 - components/Counter.tsx: the only file that ships click logic
"use client";
import { useState } from "react";
export default function Counter() {
const [count, setCount] = useState(0);
return (
<button type="button" onClick={() => setCount((c) => c + 1)}>
Clicked {count} times
</button>
);
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- A Server Component renders a Client Component
<Counter />AboutPage မှာ "use client" မပါလို့ server ပေါ်မှာ render ဖြစ်ပါတယ်။ Counter ကိုတော့ HTML အဖြစ် အရင်ပို့ပြီး browser မှာ JavaScript ရောက်မှ click ကို တုံ့ပြန်ပါတယ်။
- Awaiting the slug
const { slug } = await params;Component ကို async လုပ်ပြီး params Promise ကို await လုပ်ပါတယ်။ slug က URL ထဲက စာသားအတိုင်း string ပါ။
- Turning the slug into a label
const name = slug.replaceAll("-", " ");URL slug တွေက hyphen နဲ့ ရေးလေ့ရှိလို့ ပြတဲ့အခါ space နဲ့ ပြောင်းပါတယ်။ တကယ့် shop မှာတော့ slug နဲ့ database ကို ရှာပြီး ပစ္စည်းနာမည်အစစ်ကို ပြပါမယ်။
- A layout only for the group
export default function MarketingLayout({ children }: { children: React.ReactNode }) {(marketing) folder ထဲက page တွေပဲ ဒီ layout ရဲ့ children အဖြစ် ဝင်ပါတယ်။ app/about က အပြင်မှာ ရှိလို့ နောက်ခံ မပြောင်းပါဘူး။
- The directive comes first
"use client";Directive က import တွေထက်တောင် အရင် file ရဲ့ ပထမဆုံး statement ဖြစ်ရပါတယ်။ ဒီ example မှာ comment တစ်ကြောင်းပဲ အပေါ်မှာ ရှိပြီး comment ကတော့ ပြဿနာ မရှိပါဘူး။
/about မှာ "About this shop" heading နဲ့ "Clicked 0 times" button ပေါ်ပြီး နှိပ်တိုင်း 1၊ 2၊ 3 တက်ပါမယ်။ /products/blue-mug မှာ "Product: blue mug" ပေါ်ပါမယ်။ /pricing မှာ လိမ္မော်ဖျော့ နောက်ခံပေါ်မှာ "Pricing" ပေါ်ပြီး /marketing/pricing ကတော့ 404 ပါ။Task ပြီးရင် ထပ်စမ်းကြည့်စရာ
- app/(shop)/about/page.tsx ကို ထပ်ထည့်ကြည့်ပါ။ app/about နဲ့ URL တူနေလို့ Next.js က route conflict error ပြပါမယ်။ Route group က URL ကို မပြောင်းတာကို ဒီလို သက်သေပြလို့ ရပါတယ်။
- Counter ကို <Counter /> နှစ်ခါ render လုပ်ပြီး count တွေ သီးခြားစီ ရှိနေလား ကြည့်ပါ။
- AboutPage ထဲမှာ console.log("server") ထည့်ပါ။ Log က browser console မှာ မဟုတ်ဘဲ dev server terminal မှာ ပေါ်ပါမယ်။
- /products/blue-mug ကနေ /products/red-cup ကို Link နဲ့ သွားပြီး root layout ရဲ့ header က ပြန်မဆွဲတာ စစ်ပါ။
လက်တွေ့ project တွေမှာ
လက်တွေ့ project တွေမှာ
အခုလေ့ကျင့်တဲ့ ပုံစံလေးခုက company website အများစုရဲ့ အရိုးစုပါ။ Marketing page တွေ (/pricing၊ /features) ကို (marketing) group နဲ့ စုပြီး ကြော်ငြာဆန်တဲ့ layout ပေး၊ app ရဲ့ dashboard page တွေကို (app) group နဲ့ စုပြီး sidebar ပါတဲ့ layout သီးသန့် ပေးလေ့ရှိပါတယ်။ URL တွေကတော့ /pricing၊ /dashboard လို တိုတိုလေးတွေ ဖြစ်နေပါတယ်။
Product၊ blog post၊ user profile စတာတွေက [slug] ဒါမှမဟုတ် [id] dynamic route တွေပါ။ Interactive ဖြစ်တဲ့ အပိုင်းတွေ (cart button၊ like button၊ dropdown) ကိုပဲ Client Component သေးသေးလေးတွေ လုပ်ထားလို့ browser ကို ပို့ရတဲ့ JavaScript နည်းပြီး page က မြန်ပါတယ်။ Team ထဲ ဝင်လာတဲ့ developer အသစ်တွေကို ပထမအပတ်မှာ ဒီလို လေ့ကျင့်ခန်းမျိုးတွေ လုပ်ခိုင်းလေ့ရှိပါတယ်။
အမှားများ
ဖြစ်လေ့ရှိတဲ့ အမှားများ
Route group မှာ parentheses မေ့တာ
app/marketing/pricing/ လို့ ရေးရင် marketing က ရိုးရိုး segment ဖြစ်ပြီး URL က /marketing/pricing ဖြစ်သွားပါတယ်။ /pricing က 404 ဖြစ်ပြီး error message လည်း မပေါ်ပါဘူး။
"use client" ကို page တစ်ခုလုံးမှာ ထည့်တာ
Error ပျောက်အောင် page.tsx ရဲ့ ထိပ်မှာ "use client" ထည့်လိုက်ရင် အလုပ်လုပ်ပေမယ့် page တစ်ခုလုံးနဲ့ သူ import လုပ်တဲ့ အရာအားလုံး browser bundle ထဲ ရောက်သွားပြီး server-only data fetching ကိုလည်း page ထဲမှာ တိုက်ရိုက် မလုပ်နိုင်တော့ပါဘူး။ Interactive component ကိုပဲ client လုပ်ပါ။
params ကို synchronous object လို ဖတ်တာ
{ params: { slug: string } } ဆိုတဲ့ type က Next.js 14 ခေတ်ကပါ။ လက်ရှိ version တွေမှာ params က Promise ဖြစ်လို့ Promise<{ slug: string }> လို့ type ပေးပြီး await လုပ်ပါ။
Group နှစ်ခုက URL တူတူ ထုတ်တာ
app/(marketing)/about နဲ့ app/(shop)/about နှစ်ခုလုံးက /about ဖြစ်လို့ Next.js က လက်မခံပါဘူး။ Group နာမည်က URL ထဲ မပါတာကို သတိရပါ။
Best Practices
လေ့ကျင့်တဲ့အခါ အကြံပြုချက်များ
- Reference answer ကို မကြည့်ခင် အနည်းဆုံး မိနစ် 10 ကိုယ်တိုင် ကြိုးစားပါ။ ကြည့်ပြီးရင်လည်း copy မကူးဘဲ ပိတ်ပြီး ပြန်ရေးပါ
- Task တစ်ခုချင်းစီ ပြီးတိုင်း git commit တစ်ခု လုပ်ပါ။ နောက် task မှာ ရှုပ်သွားရင် အလုပ်လုပ်ခဲ့တဲ့ အခြေအနေကို ပြန်သွားလို့ ရပါတယ်
- Error message ကို အပေါ်ကနေ အကုန်ဖတ်ပါ။ Next.js error အများစုက file နာမည်နဲ့ ပြင်ရမယ့် နည်းကို တစ်ခါတည်း ပြောပြပါတယ်
Mini challenge
App Router စည်းမျဉ်းအတိုင်း route resolver ရေးပါ
Task လေးခုမှာ သုံးခဲ့တဲ့ စည်းမျဉ်းတွေကို code နဲ့ ပြန်ရေးကြည့်ပါ။ File path list ကနေ URL pattern တွေ ထုတ်ပြီး pathname တစ်ခုကို ဘယ် route က ကိုင်မလဲ ဆုံးဖြတ်တဲ့ function သုံးခု ရေးပါ။ Next.js ရဲ့ တကယ့် router မဟုတ်ပါဘူး၊ စည်းမျဉ်းကို နားလည်ကြောင်း စစ်ဖို့ model သေးသေးလေးပါ။
- routeFromFile(): app/ ထဲက page.tsx ကိုပဲ route အဖြစ် ယူပြီး (group) segment တွေကို ဖယ်ပါ
- matchRoute(): segment အရေအတွက် တူရမယ်၊ [name] က ဘာတန်ဖိုးမဆို ဖမ်းပြီး params ထဲ ထည့်ပါ
- resolve(): dynamic segment နည်းတဲ့ route ကို အရင်စစ်ပြီး ဘာမှ မကိုက်ရင် "404 Not Found"
- /products/new က static route ကို၊ /products/blue-mug က [slug] ကို ရောက်ရပါမယ်
type Params = Record<string, string>;
// "app/(marketing)/pricing/page.tsx" -> "/pricing"; non-page files -> null
function routeFromFile(file: string): string | null {
// TODO: must start with "app" and end with page.tsx/ts/jsx/js
// TODO: drop "app", the file name and every "(group)" segment
}
// matchRoute("/products/[slug]", "/products/blue-mug") -> { slug: "blue-mug" }
function matchRoute(pattern: string, pathname: string): Params | null {
// TODO: compare segment by segment; "[name]" captures, others must be equal
}
function resolve(routes: string[], pathname: string): string {
// TODO: sort so routes with fewer [dynamic] segments are tried first
// TODO: return "<route> <params as JSON>" or "404 Not Found"
}ရလဒ်: File ခုနစ်ခုထဲက page ငါးခုက route ဖြစ်ပြီး layout.tsx နဲ့ ProductCard.tsx က (not a route) ပါ။ /products/new က static route ကို၊ /products/blue-mug က {"slug":"blue-mug"} နဲ့ [slug] ကို ရောက်ပြီး /marketing/pricing နဲ့ /products က 404 ဖြစ်ပါတယ်။
အရိပ်အမြွက် ကြည့်ရန်
file.split("/") နဲ့ segment တွေ ခွဲပါ။ Group ကို s.startsWith("(") && s.endsWith(")") နဲ့ သိနိုင်ပါတယ်။ pathname.split("/").filter(Boolean) က "/" ကို [] ဖြစ်စေလို့ root route "/" နဲ့လည်း ကိုက်ပါတယ်။
အဖြေ ကြည့်ရန်
type Params = Record<string, string>;
const isGroup = (s: string) => s.startsWith("(") && s.endsWith(")");
const isDynamic = (s: string) => s.startsWith("[") && s.endsWith("]");
function routeFromFile(file: string): string | null {
const parts = file.split("/");
const last = parts[parts.length - 1];
if (parts[0] !== "app" || !/^page\.(tsx|ts|jsx|js)$/.test(last)) return null;
const segments = parts.slice(1, -1).filter((s) => !isGroup(s));
return "/" + segments.join("/");
}
function matchRoute(pattern: string, pathname: string): Params | null {
const want = pattern.split("/").filter(Boolean);
const got = pathname.split("/").filter(Boolean);
if (want.length !== got.length) return null;
const params: Params = {};
for (let i = 0; i < want.length; i++) {
if (isDynamic(want[i])) params[want[i].slice(1, -1)] = got[i];
else if (want[i] !== got[i]) return null;
}
return params;
}
// Static segments beat dynamic ones, like the App Router.
function resolve(routes: string[], pathname: string): string {
const dynamicCount = (r: string) => r.split("/").filter(isDynamic).length;
const ordered = [...routes].sort((a, b) => dynamicCount(a) - dynamicCount(b));
for (const route of ordered) {
const params = matchRoute(route, pathname);
if (params) return `${route} ${JSON.stringify(params)}`;
}
return "404 Not Found";
}
const files = [
"app/page.tsx",
"app/about/page.tsx",
"app/products/[slug]/page.tsx",
"app/products/new/page.tsx",
"app/(marketing)/layout.tsx",
"app/(marketing)/pricing/page.tsx",
"app/products/ProductCard.tsx",
];
const routes: string[] = [];
for (const f of files) {
const route = routeFromFile(f);
console.log(`${f} -> ${route ?? "(not a route)"}`);
if (route) routes.push(route);
}
const paths = [
"/products/blue-mug", "/products/new", "/pricing", "/marketing/pricing", "/products",
];
for (const path of paths) console.log(`${path} => ${resolve(routes, path)}`);Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Task တိုင်းမှာ URL ကို အရင် ခန့်မှန်းပြီးမှ ရေးကာ browser မှာ စစ်ပါ
- [slug] က တန်ဖိုးကို ဖမ်းပြီး (group) က URL ထဲ မပါဘဲ layout သီးသန့် ပေးဖို့ပဲ သုံးပါတယ်
- page.tsx ပါမှ route ဖြစ်ပြီး static segment က dynamic segment ထက် ဦးစားပေးခံရပါတယ်
- "use client" ကို interactive component သေးသေးလေးမှာပဲ ထားပြီး Server Component page ကနေ render လုပ်ပါ
Next.js Documentation — Next.js