ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
စားဖိုမှူးတစ်ယောက်က မီးဖိုချောင်ထဲမှာ ဟင်းကို မြည်းကြည့်ရင်း ချက်တာနဲ့ ဧည့်ခံပွဲအတွက် ဟင်းပွဲ ရာချီ ကြိုပြင်တာ မတူပါဘူး။ ဧည့်ခံပွဲမှာ ပစ္စည်း တစ်ခု ပျောက်နေရင် ဆိုင်ကို ပြေးဝယ်လို့ မရတော့ပါဘူး။
next dev က မြည်းကြည့်ရင်း ချက်တာပါ၊ page တစ်ခု ဖွင့်မှ အဲဒီ page ကို compile လုပ်ပါတယ်။ next build ကတော့ ဧည့်ခံပွဲ ပြင်ဆင်မှုပါ: route အားလုံးကို တစ်ခါတည်း compile လုပ်၊ type စစ်၊ static ဖြစ်နိုင်တဲ့ page တွေကို ကြိုတင် render လုပ်ပါတယ်။ ဒါကြောင့် dev မှာ မမြင်ခဲ့တဲ့ ပြဿနာတွေ build မှာမှ ပေါ်လာတတ်ပါတယ်။
Problem → Why → Solution
ပြဿနာ: dev မှာ အလုပ်လုပ်ပြီး build မှာ ကျိုးတဲ့ page
Developer တစ်ယောက်က notes page ထဲမှာ ကိုယ့် app ရဲ့ Route Handler ကို localhost URL နဲ့ fetch လုပ်ထားပါတယ်။ next dev run နေတုန်းက server က localhost:3000 မှာ ရှိနေလို့ ပြဿနာ မရှိပါဘူး။ Release နေ့ရောက်တော့ CI မှာ next build run တဲ့အခါ build က ဒီ page ကို ကြိုတင် render လုပ်ဖို့ ကြိုးစားပါတယ်။ အဲဒီအချိန်မှာ localhost:3000 မှာ ဘယ် server မှ မရှိလို့ fetch fail ဖြစ်ပြီး build တစ်ခုလုံး ရပ်သွားပါတယ်။
တစ်ချိန်တည်းမှာ NEXT_PUBLIC_SITE_URL ကို hosting dashboard မှာ ပြင်ထားပေမယ့် browser ထဲက code က URL အဟောင်းကိုပဲ သုံးနေတာကိုလည်း တွေ့ရပါတယ်။
export default async function NotesPage() {
// Fine in next dev; during next build nothing listens on localhost:3000
const res = await fetch("http://localhost:3000/api/notes");
const notes: { id: string; title: string }[] = await res.json();
return (
<ul>
{notes.map((n) => <li key={n.id}>{n.title}</li>)}
</ul>
);
}ဘာကြောင့် ဒီလို ဖြစ်တာလဲ
Server Component က server ပေါ်မှာ run နေပြီးသားမို့ ကိုယ့် API ကို HTTP နဲ့ ပြန်ခေါ်စရာ မလိုပါဘူး၊ data layer function ကို တိုက်ရိုက် ခေါ်ရပါမယ်။ Build က page တွေကို server မစခင် render လုပ်တတ်လို့ localhost ကို မှီခိုတဲ့ code က ကျိုးပါတယ်။ NEXT_PUBLIC_ variable တွေကတော့ build လုပ်ချိန်မှာ JavaScript ထဲ စာသားအဖြစ် ရေးထည့်ခံရလို့ တန်ဖိုး ပြောင်းရင် ပြန် build လုပ်ရပါတယ်။
Core concept: build က ဘာတွေ ထပ်စစ်လဲ
next build က production အတွက် optimize လုပ်ထားတဲ့ output ကို .next folder ထဲ ထုတ်ပါတယ်။ အဲဒီလုပ်ငန်းစဉ်မှာ dev server က မလုပ်တဲ့ အလုပ် သုံးခု ပါပါတယ်။ ပထမ၊ route အားလုံးကို compile လုပ်ပြီး TypeScript type error ရှိရင် build ကို ရပ်ပါတယ်။ ဒုတိယ၊ route တစ်ခုချင်းစီကို static (build time မှာ ကြိုတင် render) လား dynamic (request တိုင်းမှာ render) လား ဆုံးဖြတ်ပြီး static ဖြစ်နိုင်တာတွေကို render လုပ်ပါတယ်။ ဒီအဆင့်မှာ သင့် Server Component code တကယ် run လို့ data နဲ့ environment variable တွေ build machine ပေါ်မှာ ရှိနေဖို့ လိုပါတယ်။ တတိယ၊ client JavaScript ကို minify လုပ်ပြီး chunk တွေ ခွဲပါတယ်။
next start ကတော့ အဲဒီ output ကို production mode နဲ့ run ပါတယ်။ ဒါကြောင့် "pnpm build && pnpm start" ဆိုတာ သင့် စက်ပေါ်မှာ production ကို အနီးစပ်ဆုံး စမ်းကြည့်နိုင်တဲ့ နည်းပါ။ Error message တွေက dev ထက် တိုပြီး hot reload မရှိတာကလည်း production နဲ့ တူပါတယ်။
| next dev | next build + next start | |
|---|---|---|
| Compile | Page ဖွင့်မှ တစ်ခုချင်း | Route အားလုံး တစ်ခါတည်း |
| Type error | Editor မှာပဲ ပြ | Build ကို ရပ် |
| Static render | Request တိုင်း ပြန် render | Build time မှာ တစ်ခါ |
| Error page | အသေးစိတ် overlay | User ကို digest ပဲ ပြ |
Release မတိုင်ခင် ဒီအဆင့်တွေကို CI လို သန့်ရှင်းတဲ့ environment မှာ run ပါ။ သင့် laptop ပေါ်မှာ ကျန်နေတဲ့ .env.local ဒါမှမဟုတ် cache ကြောင့် အလုပ်လုပ်နေတာကို CI က ဖော်ထုတ်ပေးပါတယ်။ Build warning တွေကို error မဟုတ်လို့ ဆိုပြီး လျစ်လျူ မရှုပါနဲ့၊ အကြောင်းရင်းကို နားလည်ပြီးမှ လက်ခံပါ။
Release ကို ခံစားချက်နဲ့ မဟုတ်ဘဲ checklist နဲ့ ဆုံးဖြတ်ပါ။ Code check တွေ (type၊ lint၊ test၊ build) က app ကို build လုပ်လို့ ရကြောင်း သက်သေပြပါတယ်။ Environment check တွေက ရောက်မယ့် နေရာမှာ variable အားလုံး ရှိကြောင်း သက်သေပြပါတယ်။ Operational check တွေကတော့ app ကို စောင့်ကြည့်နိုင်ပြီး ပြန်ဆုတ်နိုင်ကြောင်း သက်သေပြပါတယ်: monitoring ဖွင့်ထားတယ်၊ backup ရှိတယ်၊ version အဟောင်းကို ပြန် deploy လုပ်လို့ ရတယ်။
- Type နဲ့ test ကို အရင်စစ်
- Build က static page ကို ကြို render
- next start နဲ့ production ကို အစမ်း
- Rollback ရနိုင်မှ release
Build machine ပေါ်မှာ ရှိတဲ့ data နဲ့ env တွေက static page ထဲ အသေ ပါသွားတယ်ဆိုတာကို အမြဲ သတိရပါ။
အခြေခံ ပုံစံ: package.json script တွေ
{
"scripts": {
"dev": "next dev",
"typecheck": "tsc --noEmit",
"lint": "<your linter command>",
"test": "vitest run",
"build": "next build",
"start": "next start"
}
}typecheck၊ lint၊ test ကို script သီးသန့် ခွဲထားရင် CI နဲ့ teammate တွေက command တူတူကို run နိုင်ပါတယ်။ Next.js version အသစ်တွေမှာ next build က lint ကို ကိုယ်တိုင် မ run တော့လို့ lint ကို သီးသန့် အဆင့်အဖြစ် ထားပါ။
ဥပမာ: release မတိုင်ခင် command လေးခု
အောက်က command တွေကို အစီအစဉ်အတိုင်း run ပါ။ တစ်ခုခု fail ဖြစ်ရင် ရပ်ပြီး ပြင်ပါ၊ နောက်တစ်ဆင့်ကို ဆက်မသွားပါနဲ့။ typecheck က package.json ထဲမှာ ကိုယ်တိုင် သတ်မှတ်ထားတဲ့ script (tsc --noEmit) ဖြစ်ပါတယ်။
pnpm typecheck
pnpm test
pnpm build
pnpm start
# Browser တွင်စစ်ရန်
# http://localhost:3000Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Fast checks first
pnpm typecheckType check က စက္ကန့်ပိုင်းအတွင်း ပြီးလို့ အရင် run ပါတယ်။ Build အရှည်ကြီးကို စောင့်ပြီးမှ type error တွေ့ရတာထက် အများကြီး မြန်ပါတယ်။
- Behaviour checks
pnpm testUnit နဲ့ component test တွေက rule တွေ ပြောင်းမသွားကြောင်း အာမခံပါတယ်။ CI မှာ watch mode မဟုတ်ဘဲ တစ်ကြိမ်တည်း run ပြီး ထွက်တဲ့ command ဖြစ်ရပါမယ်။
- The production build
pnpm buildRoute အားလုံးကို compile လုပ်ပြီး static page တွေကို ကြိုတင် render လုပ်ပါတယ်။ ပြီးရင် route စာရင်းကို ○ (static) နဲ့ ƒ (dynamic) သင်္ကေတတွေနဲ့ ပြပါတယ်။ ထင်ထားတဲ့ route က အမျိုးအစား မှန်လား စစ်ပါ။
- Production mode, locally
pnpm startBuild output ကို production mode နဲ့ port 3000 မှာ run ပါတယ်။ Browser ဖွင့်ပြီး login၊ note ဖန်တီး၊ ပုံ၊ metadata စတဲ့ အရေးကြီး flow တွေကို smoke test လုပ်ပါ။
Production build အောင်မြင်ပြီး အရေးကြီး user flows များ production-like mode တွင် အလုပ်လုပ်မည်။Build output ကို ဖတ်ခြင်း
Build ပြီးရင် Next.js က route တစ်ခုချင်းကို သင်္ကေတနဲ့ ပြပါတယ်။ Column တွေနဲ့ size အချက်အလက်တွေက version အလိုက် ကွာနိုင်လို့ အောက်က ဥပမာကို အတိုချုံ့ထားပါတယ်။ အဓိက ကြည့်ရမှာက သင်္ကေတပါ။
Route (app)
┌ ○ /
├ ○ /about
├ ● /notes/[slug] (generateStaticParams)
├ ƒ /dashboard
└ ƒ /api/notes
○ (Static) prerendered as static content
● (SSG) prerendered as static HTML (uses generateStaticParams)
ƒ (Dynamic) server-rendered on demand/dashboard က cookie ကို ဖတ်လို့ ƒ ဖြစ်နေတာ မှန်ပါတယ်။ ဒါပေမယ့် user တိုင်းအတွက် ပုံစံတူ ဖြစ်ရမယ့် /about က ƒ ဖြစ်နေရင် မလိုဘဲ dynamic API တစ်ခုခု သုံးမိနေတဲ့ လက္ခဏာပါ။ ဆန့်ကျင်ဘက်အနေနဲ့ user တစ်ယောက်ချင်းအတွက် data ပြရမယ့် page က ○ ဖြစ်နေရင် build time က data ကို အားလုံးကို ပြနေတာမို့ ပိုအန္တရာယ်များပါတယ်။
Environment variable: build time နဲ့ run time
| Variable | ဖတ်တဲ့ အချိန် | တန်ဖိုး ပြောင်းရင် |
|---|---|---|
| NEXT_PUBLIC_SITE_URL | next build မှာ browser JS ထဲ ရေးထည့် | ပြန် build လုပ်ရ |
| DATABASE_URL (dynamic route ထဲ) | Request တိုင်း server ပေါ်မှာ | Server ကို restart |
| Static page ထဲမှာ ဖတ်တဲ့ server variable | Build time မှာ တစ်ခါ၊ HTML ထဲ အသေ | ပြန် build လုပ်ရ |
Image တစ်ခု၊ environment အများကြီး
Staging နဲ့ production ကို build တစ်ခုတည်းနဲ့ promote လုပ်ချင်ရင် NEXT_PUBLIC_ variable တွေ environment အလိုက် ကွဲလို့ မရပါဘူး၊ build ထဲမှာ အသေ ပါသွားလို့ပါ။ Environment အလိုက် ကွဲရမယ့် တန်ဖိုးကို server ပေါ်မှာ run time မှာ ဖတ်ပြီး Server Component ကနေ prop အဖြစ် client ကို ပို့ပါ။
Migration နဲ့ rollback ကို ကြိုစီစဉ်ခြင်း
၁။ Backup ယူပါ
Database schema ပြောင်းမယ့် release တိုင်း မတိုင်ခင် backup ယူပြီး restore လုပ်လို့ ရကြောင်း တစ်ခါတစ်လေ စမ်းကြည့်ပါ။ Restore မစမ်းဖူးတဲ့ backup က အာမခံချက် မဟုတ်ပါဘူး။
၂။ Migration ကို နောက်ပြန် လိုက်ဖက်အောင် ရေးပါ
Column ကို နာမည်ပြောင်းမယ့်အစား column အသစ် ထည့်ပြီး code အဟောင်းနဲ့ အသစ် နှစ်ခုလုံး အလုပ်လုပ်အောင် ထားပါ။ Column အဟောင်းကို နောက် release မှာမှ ဖျက်ပါ။ ဒါမှ code ကို rollback လုပ်ရင် database နဲ့ မတိုက်တော့ပါဘူး။
၃။ ပြန်ဆုတ်မယ့် version ကို သိထားပါ
Release တိုင်းကို git tag ဒါမှမဟုတ် commit SHA နဲ့ မှတ်ထားပြီး ယခင် version ကို ဘယ်လို ပြန်တင်ရမလဲ ရေးထားပါ။ ပြဿနာ ဖြစ်တဲ့ ည ၂ နာရီမှာ စဉ်းစားနေရမယ့် အချိန် မရှိပါဘူး။
လက်တွေ့ project တွေမှာ
လက်တွေ့ project တွေမှာ
Team အများစုမှာ release checklist ကို repository ထဲက file တစ်ခု (ဥပမာ RELEASE.md) ဒါမှမဟုတ် pull request template အဖြစ် ထားပါတယ်။ Checklist ရဲ့ အချက်အများစုကို CI က အလိုအလျောက် စစ်ပြီး လူက စစ်ရမယ့် အချက် ("migration က backward compatible ဖြစ်လား"၊ "rollback version ဘာလဲ") တွေကိုပဲ checkbox အဖြစ် ချန်ထားပါတယ်။
CI pipeline က pull request တိုင်းမှာ pnpm install --frozen-lockfile နဲ့ lockfile အတိအကျအတိုင်း install လုပ်ပြီး typecheck၊ lint၊ test၊ build ကို run ပါတယ်။ Main branch ကို merge ပြီးရင် build artifact ကို staging မှာ အရင် deploy ပြီး smoke test အောင်မှ production ကို တင်ပါတယ်။ Release note မှာ commit SHA၊ ပြောင်းလဲမှု၊ migration ရှိမရှိ ရေးထားလို့ ပြဿနာ ဖြစ်ရင် ဘယ် version ကို ပြန်ဆုတ်ရမလဲ ချက်ချင်း သိပါတယ်။
အမှားများ
အမှားများတဲ့ အချက်တွေ
- Development mode မှာပဲ စမ်းပြီး next build ကို deploy နေ့မှ ပထမဆုံး run တာ။ Type error၊ build time မှာ မရှိတဲ့ server ကို fetch လုပ်တာ၊ static ဖြစ်သွားတဲ့ page လို ပြဿနာတွေ release နေ့မှ ပေါ်လာပါတယ်။
- NEXT_PUBLIC_ variable ကို hosting dashboard မှာ ပြောင်းပြီး ပြန် build မလုပ်တာ။ Browser ထဲက JavaScript မှာ တန်ဖိုး အဟောင်းက စာသားအဖြစ် ကျန်နေပါတယ်။ ပိုဆိုးတာက secret တစ်ခုကို NEXT_PUBLIC_ နဲ့ နာမည်ပေးမိရင် လူတိုင်း download လုပ်နိုင်တဲ့ file ထဲ ရောက်သွားပါတယ်။
- Database migration ကို rollback အစီအစဉ် မရှိဘဲ code နဲ့ တပြိုင်နက် run တာ။ Code ကို ပြန်ဆုတ်ပေမယ့် column ဖျက်ပြီးသား ဖြစ်နေရင် version အဟောင်းကလည်း အလုပ်မလုပ်တော့ပါဘူး။
- Build warning တွေကို "error မဟုတ်ဘူး" ဆိုပြီး လျစ်လျူရှုတာ။ Dynamic ဖြစ်ရမယ့် route က static ဖြစ်သွားတာ ဒါမှမဟုတ် dependency ပြဿနာကို warning က အစောကြီးကတည်းက သတိပေးနေတတ်ပါတယ်။
Mini challenge
ကိုယ်တိုင် လုပ်ကြည့်ရအောင်: build မတိုင်ခင် env စစ်တဲ့ script
CI မှာ next build မတိုင်ခင် run မယ့် preflight function ကို ရေးပါ။ Environment object ကို zod နဲ့ စစ်ပြီး ပျောက်နေတာ၊ မမှန်တာ၊ secret နဲ့တူပြီး NEXT_PUBLIC_ နဲ့ စထားတာ (browser ကို leak ဖြစ်မယ့် variable) တွေကို စာရင်းပြုစုပါ။ ပြဿနာ မရှိမှ build ကို ဆက်လုပ်ခွင့် ပေးပါ။
- DATABASE_URL က postgres:// နဲ့ စရပါမယ်၊ AUTH_SECRET က အနည်းဆုံး စာလုံး 32 လုံး ရှိရပါမယ်
- NEXT_PUBLIC_SITE_URL က URL မှန်ဖြစ်ရပါမယ်
- ပျောက်နေတဲ့ variable ကို "missing" လို့ ပြပါ
- NEXT_PUBLIC_ နဲ့ စပြီး SECRET၊ PASSWORD၊ TOKEN၊ DATABASE_URL၊ PRIVATE ပါတဲ့ နာမည်ကို သတိပေးပါ
import { z } from "zod";
type Env = Record<string, string | undefined>;
// TODO: an EnvSchema with DATABASE_URL, AUTH_SECRET and NEXT_PUBLIC_SITE_URL
export function preflight(env: Env): string[] {
const problems: string[] = [];
// TODO: safeParse the env and turn each issue into "NAME: missing" or "NAME: reason"
// TODO: flag NEXT_PUBLIC_ variables whose names look secret
return problems;
}
// TODO: report(name, env) prints "ready to build" or "BLOCKED" plus each problem
// TODO: try it with a broken "staging" env and a correct "production" envရလဒ်: "staging: BLOCKED" အောက်မှာ AUTH_SECRET တိုလွန်းတာ၊ NEXT_PUBLIC_SITE_URL ပျောက်နေတာ၊ NEXT_PUBLIC_API_TOKEN က secret နဲ့တူတာ ဆိုတဲ့ စာကြောင်း သုံးကြောင်း ပေါ်ပြီး "production: ready to build" ပေါ်ပါတယ်။
အရိပ်အမြွက် ကြည့်ရန်
safeParse ရဲ့ error.issues ထဲက issue တစ်ခုစီမှာ path[0] က variable နာမည်ပါ။ env[name] က undefined ဆိုရင် "missing" လို့ ရေးပြီး မဟုတ်ရင် issue.message ကို သုံးပါ။ z.string().min(32, "message") လို့ ကိုယ်ပိုင် message ပေးနိုင်ပါတယ်။
အဖြေ ကြည့်ရန်
import { z } from "zod";
// Run in CI before `next build`
const EnvSchema = z.object({
DATABASE_URL: z.string().startsWith("postgres://"),
AUTH_SECRET: z.string().min(32, "must be at least 32 characters"),
NEXT_PUBLIC_SITE_URL: z.string().url(),
});
const SECRET_WORDS = ["SECRET", "PASSWORD", "TOKEN", "DATABASE_URL", "PRIVATE"];
type Env = Record<string, string | undefined>;
export function preflight(env: Env): string[] {
const problems: string[] = [];
const parsed = EnvSchema.safeParse(env);
if (!parsed.success) {
for (const issue of parsed.error.issues) {
const name = String(issue.path[0]);
problems.push(`${name}: ${env[name] === undefined ? "missing" : issue.message}`);
}
}
for (const key of Object.keys(env)) {
const looksSecret = SECRET_WORDS.some((word) => key.includes(word));
if (key.startsWith("NEXT_PUBLIC_") && looksSecret) {
problems.push(`${key}: looks secret but NEXT_PUBLIC_ inlines it into browser JS`);
}
}
return problems;
}
function report(name: string, env: Env) {
const problems = preflight(env);
console.log(`${name}: ${problems.length === 0 ? "ready to build" : "BLOCKED"}`);
problems.forEach((p) => console.log(` - ${p}`));
}
report("staging", {
DATABASE_URL: "postgres://app@db.internal:5432/notes",
AUTH_SECRET: "short",
NEXT_PUBLIC_API_TOKEN: "sk_live_123",
});
report("production", {
DATABASE_URL: "postgres://app@db.internal:5432/notes",
AUTH_SECRET: "x".repeat(40),
NEXT_PUBLIC_SITE_URL: "https://notes.example.com",
});Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- next build က route အားလုံးကို compile၊ type check လုပ်ပြီး static page တွေကို build time မှာ render လုပ်ပါတယ်
- pnpm build && pnpm start နဲ့ release မတိုင်ခင် production mode ကို ကိုယ့်စက်ပေါ်မှာ စမ်းပါ
- NEXT_PUBLIC_ variable တွေ build ထဲ အသေ ပါသွားလို့ ပြောင်းရင် ပြန် build လုပ်ရပါတယ်
- Backup၊ backward compatible migration နဲ့ ပြန်ဆုတ်မယ့် version ကို release တိုင်း ကြိုစီစဉ်ပါ
Next.js — Production Checklist — Next.js
Next.js — Production Checklist — Next.js