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

Project Folder Structure ကို ဖတ်တတ်အောင်လုပ်ခြင်း

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

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

  • Project ရဲ့ top-level folder နဲ့ config file တစ်ခုချင်းရဲ့ တာဝန်ကို ပြောနိုင်ဖို့
  • app/ ထဲက file path တစ်ခုကို ကြည့်ပြီး ဘယ် URL ဖြစ်မလဲ (ဒါမှမဟုတ် route မဖြစ်ဘူးလား) ခန့်မှန်းနိုင်ဖို့
  • public/ ထဲက file ကို မှန်ကန်တဲ့ URL နဲ့ ညွှန်းနိုင်ဖို့
  • Component နဲ့ helper တွေကို colocation၊ private folder ဒါမှမဟုတ် components/ နဲ့ lib/ မှာ စီစဉ်နိုင်ဖို့

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

ရိုးရိုးလေး ရှင်းရရင်

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 နှစ်ခုကို မသိလို့ ဖြစ်တာပါ။

project tree
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.jsonDependency တွေနဲ့ dev/build/start script
next.config.tsFramework setting (remote image domain၊ redirect စတာ)
tsconfig.jsonTypeScript 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 လုပ်ရပါမယ်။

မှတ်ထားရမယ့် ပုံစံ: file path ကနေ URL
  1. app/ အောက်မှာ ရှိလား စစ်
  2. _ နဲ့ စတဲ့ folder ပါလား စစ်
  3. File နာမည်က page.tsx လား စစ်
  4. Folder path ကပဲ URL ဖြစ်တယ်

စစ်ချက် တစ်ခုခု မအောင်ရင် အဲဒီ file က route မဟုတ်ဘဲ import လုပ်သုံးရမယ့် module ရိုးရိုးပါ။

အထွေထွေ ပုံစံ: route ဖြစ်တဲ့ file နဲ့ မဖြစ်တဲ့ file

text
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/
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.json

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

  1. Root files| |-- layout.tsx root layout: wraps every page

    app/layout.tsx က app တစ်ခုလုံးရဲ့ root layout ဖြစ်ပြီး app/page.tsx က "/" ပါ။ Root layout ကို နောက် lesson မှာ အသေးစိတ် ရေးပါမယ်။

  2. notes/| |-- page.tsx -> /notes

    notes folder က /notes segment ဖြစ်ပြီး ထဲက page.tsx က အဲဒီ URL ကို ဖွင့်လို့ ရစေပါတယ်။

  3. Colocated filenote-card.tsx

    Page ဘေးမှာ ရှိပေမယ့် special နာမည် မဟုတ်လို့ /notes/note-card လို URL မရှိပါဘူး။ notes page က import လုပ်သုံးဖို့ပဲ ဖြစ်ပါတယ်။

  4. Dynamic segment[id]/

    Square bracket နဲ့ folder က dynamic segment ပါ — /notes/routing၊ /notes/42 လို URL အားလုံးကို page တစ်ခုတည်းက ဖြေပါတယ်။ Dynamic route lesson မှာ id ကို ဘယ်လို ဖတ်လဲ လေ့လာပါမယ်။

  5. Outside app/|-- components/ shared UI, never a route

    components/ နဲ့ lib/ က app/ အပြင်မှာ ရှိလို့ ထဲမှာ ဘာ file ရှိရှိ URL မဖြစ်ပါဘူး။ public/logo.png ကတော့ /logo.png အဖြစ် serve ဖြစ်ပါတယ်။

You should see
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 လို့ ရေးလို့ ရပါတယ်။

app/notes/[id]/page.tsx
// 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 ထုတ်ပါ
route-for.ts
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("/") က "/" ဖြစ်ပါတယ်။

အဖြေ ကြည့်ရန်
route-for.ts
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

app/blog/_components/page.tsx file ရဲ့ URL က ဘာလဲ။

Quick Quiz 2 / 3

public/images/avatar.png ကို <img> မှာ ဘယ်လို ညွှန်းရမလဲ။

Quick Quiz 3 / 3

app/notes/ folder ထဲမှာ page.tsx၊ note-card.tsx နဲ့ utils.ts ရှိရင် URL ဘယ်နှခု ရမလဲ။

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

  • 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

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

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

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

Project Folder Structure ကို ဖတ်တတ်အောင်လုပ်ခြင်း | Thuta Learning