ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
Project အသစ်ကို ဖွင့်လိုက်ရင် file တွေ အများကြီး မြင်ရပေမယ့် တစ်ခါတည်း အကုန် မှတ်စရာ မလိုပါဘူး။ နေ့တိုင်း ကိုင်ရမယ့် နေရာ သုံးခုကို အရင် နားလည်ရင် လုံလောက်ပါတယ် — page တွေ ရှိတဲ့ app/၊ ပုံနဲ့ icon တွေ ထားတဲ့ public/၊ dependency နဲ့ script တွေ ရေးထားတဲ့ package.json ပါ။
app folder ကို site map လို့ မြင်ကြည့်ပါ။ Folder တစ်ခုက URL အပိုင်းတစ်ခုဖြစ်ပြီး အဲဒီ folder ထဲမှာ page.tsx ဆိုတဲ့ နာမည်အတိအကျ file ရှိမှ browser ကနေ ဖွင့်လို့ ရပါတယ်။ တခြား နာမည်နဲ့ file တွေက ဘယ်လောက်ပဲ ရှိရှိ URL မဖြစ်ပါဘူး။
Problem → Why → Solution
ပြဿနာ: ကိုယ်ပေးချင်တဲ့ file နာမည်နဲ့ route ဖန်တီးလို့ ထင်တာ
Developer တစ်ယောက်က About page လုပ်ဖို့ app/about/about-page.tsx ကို ဖန်တီးပြီး /about ကို ဖွင့်လိုက်တော့ 404 ရပါတယ်။ Logo ကိုလည်း public/logo.png မှာ ထားပြီး src="/public/logo.png" လို့ ရေးတော့ ပုံ ကျိုးနေပါတယ်။ Code ထဲမှာ typo မရှိပေမယ့် Next.js ရဲ့ file convention နှစ်ခုကို မသိလို့ ဖြစ်တာပါ။
app/
|-- page.tsx -> / (works)
`-- about/
`-- about-page.tsx -> 404: not named page.tsx
public/
`-- logo.png <img src="/public/logo.png"> -> broken imageဘာကြောင့် ဒီလို ဖြစ်တာလဲ
Next.js က app/ ထဲမှာ page.tsx၊ layout.tsx လို သတ်မှတ်ထားတဲ့ နာမည်တွေကိုပဲ special file အဖြစ် ဖတ်ပြီး တခြား file တွေကို ရိုးရိုး module အဖြစ်ပဲ မြင်ပါတယ်။ public/ ထဲက file တွေကတော့ site ရဲ့ root ကနေ serve လုပ်တာမို့ URL ထဲမှာ "public" ဆိုတဲ့ စကားလုံး မပါပါဘူး — /logo.png ပဲ ဖြစ်ပါတယ်။
Core concept: folder တွေရဲ့ တာဝန်နဲ့ special file တွေ
| နေရာ | တာဝန် |
|---|---|
| app/ | Route၊ layout နဲ့ UI — App Router ရဲ့ နေရာ |
| public/ | ပုံ၊ icon၊ robots.txt လို static file — root (/) ကနေ serve |
| package.json | Dependency တွေနဲ့ dev/build/start script |
| next.config.ts | Framework setting (remote image domain၊ redirect စတာ) |
| tsconfig.json | TypeScript setting နဲ့ @/* လို import alias |
| .next/ | Build output — Next.js ထုတ်ပေးတာ၊ git ထဲ မထည့်ရ |
app/ ထဲမှာ file နာမည်အတိအကျက အဓိပ္ပာယ် ရှိပါတယ်။ page.tsx က route ကို public ဖြစ်စေပြီး layout.tsx က shared UI ကို wrap လုပ်ပါတယ်။ loading.tsx၊ error.tsx၊ not-found.tsx တို့က loading၊ error နဲ့ 404 UI ကို သတ်မှတ်ပြီး route.ts ကတော့ HTML page အစား API endpoint ဖန်တီးပါတယ်။ Folder တစ်ခုထဲမှာ page.tsx နဲ့ route.ts နှစ်ခုလုံး မထားရပါဘူး၊ URL တစ်ခုကို ဘယ်သူ ဖြေမလဲ ရှင်းနေရလို့ပါ။
Special နာမည် မဟုတ်တဲ့ file တွေကို app/ ထဲမှာ ထားလို့ ရပါတယ် (colocation)။ ဥပမာ app/notes/note-card.tsx က /notes page ဘေးမှာ ရှိနေပေမယ့် URL မဖြစ်ပါဘူး။ Folder နာမည်ကို _ နဲ့ စရင် (ဥပမာ _lib) အဲဒီ folder နဲ့ အောက်က အရာအားလုံး routing ကနေ ထွက်သွားလို့ ထဲမှာ page.tsx ရှိနေရင်တောင် route မဖြစ်ပါဘူး။
Page အများကြီး မျှသုံးတဲ့ component တွေကိုတော့ components/ မှာ၊ data ယူတဲ့ function နဲ့ utility တွေကို lib/ မှာ ထားတာ အဖြစ်များပါတယ်။ ဒီ folder နာမည်တွေက Next.js ရဲ့ rule မဟုတ်ဘဲ team convention ပါ။ src/ folder ထဲမှာ app/ ကို ထည့်ထားတဲ့ project တွေလည်း ရှိပါတယ် — routing rule တွေက အတူတူပါပဲ။
Root မှာ ရှိတဲ့ file အချို့ကို ကိုယ်တိုင် မပြင်ရပါဘူး။ next-env.d.ts ကို Next.js က TypeScript type တွေအတွက် အလိုအလျောက် ထုတ်ပေးပြီး .next/ က build လုပ်တိုင်း ပြန်ဖန်တီးပါတယ်။ next.config.ts ကိုတော့ remote server ကနေ ပုံယူဖို့၊ redirect ထည့်ဖို့လို framework အပြုအမူကို ပြောင်းချင်တဲ့အခါ ပြင်ပါတယ်၊ ပြင်ပြီးရင် dev server ကို restart လုပ်ရပါမယ်။
- app/ အောက်မှာ ရှိလား စစ်
- _ နဲ့ စတဲ့ folder ပါလား စစ်
- File နာမည်က page.tsx လား စစ်
- Folder path ကပဲ URL ဖြစ်တယ်
စစ်ချက် တစ်ခုခု မအောင်ရင် အဲဒီ file က route မဟုတ်ဘဲ import လုပ်သုံးရမယ့် module ရိုးရိုးပါ။
အထွေထွေ ပုံစံ: route ဖြစ်တဲ့ file နဲ့ မဖြစ်တဲ့ file
app/<segment>/page.tsx -> /<segment>
app/<segment>/layout.tsx -> wraps /<segment> and below
app/<segment>/route.ts -> API endpoint at /<segment>
app/<segment>/any-name.tsx -> not a route (colocated)
app/_private/page.tsx -> not a route (private folder)
public/<file> -> /<file>Special file နာမည်တွေက route ကို ဖန်တီးပြီး တခြား နာမည်တွေက ဘေးမှာ ထားတဲ့ module ပဲ ဖြစ်ပါတယ်။ _ နဲ့ စတဲ့ folder တွေက routing ကနေ လုံးဝ ထွက်ပြီး public/ ထဲက file တွေက "public" မပါတဲ့ URL ရပါတယ်။
Example: Myanmar Notes ရဲ့ folder structure
အောက်က tree မှာ file တစ်ခုစီ ဘေးမှာ ဘယ် URL ဖြစ်လဲ (ဒါမှမဟုတ် ဘာတာဝန်လဲ) ရေးထားပါတယ်။ Route ဖြစ်တဲ့ file တွေနဲ့ ရိုးရိုး module တွေကို ခွဲကြည့်ပါ။
ဒါက Myanmar Notes ရဲ့ အစောပိုင်း ပုံစံပါ။ နောက် lesson တွေမှာ loading.tsx၊ error.tsx နဲ့ API အတွက် route.ts တွေ ထပ်ဝင်လာပေမယ့် rule တွေက ပြောင်းမှာ မဟုတ်ပါဘူး။ Tree ကို မကြည့်ခင် route ဘယ်နှခု ရှိမလဲ အရင် ခန့်မှန်းပြီးမှ အောက်က ရလဒ်နဲ့ တိုက်ကြည့်ပါ။
myanmar-notes/
|-- app/
| |-- layout.tsx root layout: wraps every page
| |-- page.tsx -> /
| |-- globals.css
| `-- notes/
| |-- page.tsx -> /notes
| |-- note-card.tsx colocated component, not a route
| `-- [id]/
| `-- page.tsx -> /notes/[id], e.g. /notes/routing
|-- components/ shared UI, never a route
|-- lib/
| `-- data.ts data access helper
|-- public/
| `-- logo.png served as /logo.png
|-- next.config.ts
|-- package.json
`-- tsconfig.jsonCode ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Root files
| |-- layout.tsx root layout: wraps every pageapp/layout.tsx က app တစ်ခုလုံးရဲ့ root layout ဖြစ်ပြီး app/page.tsx က "/" ပါ။ Root layout ကို နောက် lesson မှာ အသေးစိတ် ရေးပါမယ်။
- notes/
| |-- page.tsx -> /notesnotes folder က /notes segment ဖြစ်ပြီး ထဲက page.tsx က အဲဒီ URL ကို ဖွင့်လို့ ရစေပါတယ်။
- Colocated file
note-card.tsxPage ဘေးမှာ ရှိပေမယ့် special နာမည် မဟုတ်လို့ /notes/note-card လို URL မရှိပါဘူး။ notes page က import လုပ်သုံးဖို့ပဲ ဖြစ်ပါတယ်။
- Dynamic segment
[id]/Square bracket နဲ့ folder က dynamic segment ပါ — /notes/routing၊ /notes/42 လို URL အားလုံးကို page တစ်ခုတည်းက ဖြေပါတယ်။ Dynamic route lesson မှာ id ကို ဘယ်လို ဖတ်လဲ လေ့လာပါမယ်။
- Outside app/
|-- components/ shared UI, never a routecomponents/ နဲ့ lib/ က app/ အပြင်မှာ ရှိလို့ ထဲမှာ ဘာ file ရှိရှိ URL မဖြစ်ပါဘူး။ public/logo.png ကတော့ /logo.png အဖြစ် serve ဖြစ်ပါတယ်။
Route သုံးခု ရပါတယ် — /၊ /notes နဲ့ note တစ်ခုချင်းအတွက် /notes/[id] (ဥပမာ /notes/routing)။ note-card.tsx၊ components/ နဲ့ lib/data.ts က route မဖြစ်ဘဲ logo ကို /logo.png ကနေ ရပါတယ်။Import alias: @/ နဲ့ import လုပ်ခြင်း
Folder အဆင့်ဆင့် နက်လာရင် ../../../lib/data လို relative path တွေက ဖတ်ရခက်ပြီး file ရွှေ့တိုင်း ကျိုးပါတယ်။ create-next-app က tsconfig.json မှာ @/* alias ကို ထည့်ပေးလို့ project root ကနေ စပြီး @/lib/data လို့ ရေးလို့ ရပါတယ်။
// tsconfig.json has: "paths": { "@/*": ["./*"] }
import { getNote } from "@/lib/data"; // instead of "../../../lib/data"
import { Header } from "@/components/header";Alias က TypeScript နဲ့ Next.js ရဲ့ bundler နှစ်ခုလုံး နားလည်လို့ editor ရဲ့ autocomplete နဲ့ "Go to definition" လည်း အလုပ်လုပ်ပါတယ်။ src/ folder သုံးတဲ့ project မှာတော့ alias က ./src/* ကို ညွှန်ပါတယ်၊ ဒါကြောင့် @/lib/data က src/lib/data ဖြစ်ပါတယ်။
လက်တွေ့ project တွေမှာ
Real project တွေမှာ structure
Project ကြီးလာတဲ့အခါ team တွေက ပုံစံ နှစ်မျိုးကို အများဆုံး သုံးကြပါတယ်။ ပထမတစ်မျိုးက route နဲ့ မသက်ဆိုင်တဲ့ code အားလုံးကို app/ အပြင်မှာ (components/၊ lib/) ထားပြီး app/ ကို routing သက်သက်အတွက် သုံးတာပါ။ ဒုတိယတစ်မျိုးက feature တစ်ခုရဲ့ component နဲ့ helper တွေကို အဲဒီ route folder ထဲမှာပဲ _components လို private folder နဲ့ colocate လုပ်တာပါ။
Next.js က ဘယ်ပုံစံကိုမှ အတင်း မခိုင်းပါဘူး၊ အရေးကြီးတာက team တစ်ခုလုံး ပုံစံ တစ်ခုတည်းကို လိုက်နာဖို့ပါ။ Developer အသစ်တစ်ယောက်က app/ ကို ကြည့်ရုံနဲ့ site ရဲ့ URL map ကို နားလည်နိုင်ရပါမယ်။ Myanmar Notes မှာတော့ shared UI ကို components/၊ data access ကို lib/ မှာ ထားပြီး page တစ်ခုတည်းနဲ့ သက်ဆိုင်တဲ့ component လေးတွေကို အဲဒီ route ဘေးမှာ colocate လုပ်ပါမယ်။ Colocate လုပ်ထားတဲ့ component တစ်ခုကို နောက်ပိုင်း page ဒုတိယ တစ်ခုမှာပါ သုံးချင်လာရင် components/ ထဲ ရွှေ့ပါတယ်။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. page.tsx အစား ကိုယ်ပိုင် file နာမည် သုံးတာ
app/about/about-page.tsx ဒါမှမဟုတ် app/about/index.tsx က /about ကို မဖန်တီးပါဘူး၊ 404 ပဲ ရပါတယ်။ Route တစ်ခုစီရဲ့ entry file ကို page.tsx လို့ အတိအကျ နာမည်ပေးပြီး component ကို default export လုပ်ပါ။
2. public/ ထဲက file ကို /public/... နဲ့ ညွှန်းတာ
public/logo.png ရဲ့ URL က /logo.png ပါ။ src="/public/logo.png" လို့ ရေးရင် ပုံ ကျိုးပါတယ်။ public/images/hero.jpg ဆိုရင်လည်း /images/hero.jpg ပဲ ဖြစ်ပါတယ်။
3. Folder တစ်ခုထဲမှာ page.tsx နဲ့ route.ts ကို တွဲထားတာ
app/notes/page.tsx နဲ့ app/notes/route.ts နှစ်ခုလုံး ရှိရင် URL တစ်ခုတည်းကို ဘယ်သူ ဖြေမလဲ ပဋိပက္ခ ဖြစ်လို့ Next.js က error ပြပါတယ်။ API ကို app/api/notes/route.ts လို သီးခြား folder မှာ ထားပါ။
4. .next/ ဒါမှမဟုတ် next-env.d.ts ကို လက်နဲ့ ပြင်တာ
ဒီ file တွေကို Next.js က ထုတ်ပေးတာမို့ ပြင်ထားသမျှ နောက် build ဒါမှမဟုတ် dev run မှာ ပြန်ရေးခံရပါတယ်။ Type ထပ်ထည့်ချင်ရင် types/ folder ထဲမှာ ကိုယ်ပိုင် .d.ts file အသစ် ဖန်တီးပါ။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- app/ ကို ဖတ်လိုက်ရင် URL map ကို နားလည်နိုင်အောင် folder နာမည်တွေကို URL မှာ ပေါ်ချင်တဲ့ စကားလုံး (lowercase၊ dash နဲ့) အတိုင်း ပေးပါ။
- Page တစ်ခုတည်းသုံးတဲ့ component ကို route ဘေးမှာ၊ page အများကြီး သုံးတာကို components/ မှာ ထားပါ။
- Route မဖြစ်စေချင်တဲ့ folder တွေကို app/ ထဲ ထားရင် _ prefix နဲ့ ရှင်းရှင်းလင်းလင်း မှတ်ပါ။
- Relative path အရှည်ကြီးတွေအစား @/ alias ကို သုံးပါ။
Mini challenge
Mini challenge: file path ကနေ URL တွက်တဲ့ function
Lesson ထဲက routing rule တွေကို code နဲ့ ပြန်ရေးကြည့်ပါ။ routeFor(filePath) က file တစ်ခုရဲ့ URL ကို return ပြန်ရပြီး route မဟုတ်ရင် null ပြန်ရပါမယ်။
- app/ အောက်က page.tsx (ဒါမှမဟုတ် page.ts/.jsx/.js) ကိုပဲ route အဖြစ် သတ်မှတ်ပါ
- app/page.tsx က "/" ဖြစ်ရမယ်
- _ နဲ့ စတဲ့ folder တစ်ခုခု ပါရင် null ပြန်ပါ
- public/ ထဲက file ဆိုရင် "public" မပါတဲ့ URL ပြန်ပါ (public/logo.png -> /logo.png)
- File ရှစ်ခုအတွက် ရလဒ်ကို print ထုတ်ပါ
const PAGE_FILE = /^page\.(tsx|ts|jsx|js)$/;
function routeFor(filePath: string): string | null {
// TODO: split the path into parts
// TODO: public/ files -> "/" + the rest of the path
// TODO: anything outside app/ -> null
// TODO: last part must match PAGE_FILE
// TODO: a folder starting with "_" -> null
// TODO: join the folders between app/ and the file into the URL
}
const files: string[] = [
// TODO: eight paths: app/page.tsx, a nested page, a dynamic page,
// a colocated component, a private folder, components/, public/
];
// TODO: print "path -> url" or "path -> not a route" for each fileရလဒ်: Terminal မှာ အောက်ပါ line ရှစ်ကြောင်း ပေါ်ရပါမယ် — app/page.tsx -> / app/notes/page.tsx -> /notes app/notes/[id]/page.tsx -> /notes/[id] app/notes/note-card.tsx -> not a route app/_drafts/page.tsx -> not a route components/header.tsx -> not a route public/logo.png -> /logo.png app/about/about-page.tsx -> not a route
အရိပ်အမြွက် ကြည့်ရန်
filePath.split("/") နဲ့ ခွဲပြီး pop() နဲ့ file နာမည်ကို ဖယ်ထုတ်ပါ။ ကျန်တဲ့ array ရဲ့ parts.slice(1) က app/ နောက်က folder တွေပါ၊ [] ဗလာဆိုရင် "/" + [].join("/") က "/" ဖြစ်ပါတယ်။
အဖြေ ကြည့်ရန်
const PAGE_FILE = /^page\.(tsx|ts|jsx|js)$/;
function routeFor(filePath: string): string | null {
const parts = filePath.split("/");
if (parts[0] === "public") return "/" + parts.slice(1).join("/");
if (parts[0] !== "app") return null;
const fileName = parts.pop() ?? "";
if (!PAGE_FILE.test(fileName)) return null;
const folders = parts.slice(1);
if (folders.some((folder) => folder.startsWith("_"))) return null;
return "/" + folders.join("/");
}
const files: string[] = [
"app/page.tsx",
"app/notes/page.tsx",
"app/notes/[id]/page.tsx",
"app/notes/note-card.tsx",
"app/_drafts/page.tsx",
"components/header.tsx",
"public/logo.png",
"app/about/about-page.tsx",
];
for (const file of files) {
console.log(`${file} -> ${routeFor(file) ?? "not a route"}`);
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- app/ က route နဲ့ UI၊ public/ က static file၊ package.json က dependency နဲ့ script၊ next.config.ts က framework setting ပါ။
- app/ ထဲမှာ page.tsx၊ layout.tsx၊ route.ts လို special နာမည်တွေကပဲ အဓိပ္ပာယ် ရှိပြီး တခြား file တွေက route မဖြစ်ပါဘူး။
- _ နဲ့ စတဲ့ folder က routing ကနေ ထွက်ပြီး public/ ထဲက file တွေကို "public" မပါဘဲ / ကနေ ညွှန်းရပါတယ်။
- components/ နဲ့ lib/ က convention သက်သက်ဖြစ်ပြီး @/* alias က root ကနေ import လုပ်ဖို့ ကူညီပါတယ်။
Next.js — Project Structure — Next.js
Next.js — Project Structure — Next.js