
Exploring Go Route Handlers with Next.js
Explore architectural patterns for combining Next.js and Go, from BFF reverse proxy rewrites and containerized monorepos to single portable binaries.
The Rise of Go in the Next.js Ecosystem
Next.js ဟာ React Server Components (RSC), Dynamic Streaming နဲ့ ကောင်းမွန်တဲ့ Frontend Developer Experience (DX) တွေကြောင့် Modern Web Development မှာ လူကြိုက်များတဲ့ Framework တစ်ခု ဖြစ်ပါတယ်။
ဒါပေမဲ့ Web Application တစ်ခုမှာ UI Rendering နဲ့ Data Fetching အပြင် CPU-intensive Operations, High-throughput Data Processing နဲ့ High-concurrency Tasks တွေကိုပါ ကိုင်တွယ်ရတဲ့အခါ Backend Architecture ကို သီးခြားစဉ်းစားဖို့ လိုလာပါတယ်။
Node.js ဟာ Asynchronous I/O နဲ့ Event Loop ကို အခြေခံထားတာကြောင့် I/O-bound Workloads တွေကို ထိရောက်စွာ ကိုင်တွယ်နိုင်ပါတယ်။ ဒါပေမဲ့ Main Thread ပေါ်မှာ CPU-intensive Tasks တွေကို တိုက်ရိုက်လုပ်ဆောင်တဲ့အခါ Event Loop ပိတ်ဆို့သွားပြီး Application ရဲ့ Responsiveness ကို ထိခိုက်စေနိုင်ပါတယ်။
ဒီလိုအခြေအနေတွေမှာ Go (Golang) ကို Backend အတွက် အသုံးပြုဖို့ စဉ်းစားလာကြပါတယ်။ Go ရဲ့ Goroutines, Built-in Concurrency Support နဲ့ Compiled Binary တွေကြောင့် Backend Services တွေကို ထိရောက်စွာ တည်ဆောက်နိုင်ပါတယ်။
Next.js ကို Frontend နှင့် UI Rendering အတွက် အသုံးပြုပြီး Go ကို Backend Services အတွက် အသုံးပြုတဲ့ Polyglot Architecture ဟာ စိတ်ဝင်စားစရာ ရွေးချယ်မှုတစ်ခု ဖြစ်လာပါတယ်။
Vercel's Experimental Native Go Route Handlers
Next.js Ecosystem မှာ စိတ်ဝင်စားစရာ အယူအဆတစ်ခုကတော့ Native Go Route Handlers ဖြစ်ပါတယ်။
ဒီ Prototype ရဲ့ ရည်ရွယ်ချက်က Next.js App Router ထဲမှာ TypeScript ရဲ့ route.ts လိုမျိုး Go Source Code (route.go) တွေကို တိုက်ရိုက်ရေးသားနိုင်ဖို့ ဖြစ်ပါတယ်။ Next.js Build Pipeline က Go Toolchain ကို အသုံးပြုပြီး Go Code တွေကို Compile လုပ်ကာ Route Handler အဖြစ် ချိတ်ဆက်ပေးနိုင်ဖို့ ရည်ရွယ်ထားပါတယ်။
ဒါပေမဲ့ ဒီလို Native Go Integration ကို Experimental Feature အဆင့်အနေနဲ့သာ သဘောထားသင့်ပါတယ်။ Production Project တွေမှာ အသုံးပြုမယ်ဆိုရင် လက်ရှိ တည်ငြိမ်ပြီးသား Architecture Patterns ၃ မျိုးကို အဓိကထား စဉ်းစားသင့်ပါတယ်။
Pattern 1: BFF and Reverse Proxy via Next.js Rewrites
လက်တွေ့မှာ အသုံးများဆုံး နည်းလမ်းတစ်ခုကတော့ Next.js ကို Frontend နဲ့ Backend-for-Frontend (BFF) အဖြစ်ထားပြီး Go ကို သီးခြား Backend API Server အဖြစ် Run ထားတာ ဖြစ်ပါတယ်။
Go Backend ကို net/http, Gin ဒါမှမဟုတ် Chi လို Framework တွေ အသုံးပြုပြီး REST API သို့မဟုတ် gRPC Services တွေ တည်ဆောက်နိုင်ပါတယ်။ Next.js ဘက်မှာတော့ next.config.mjs ထဲက rewrites() ကို အသုံးပြုပြီး Browser ကနေ ဝင်လာတဲ့ /api/* Requests တွေကို Go Backend ဆီ လမ်းကြောင်းလွှဲပေးနိုင်ပါတယ်။
// next.config.mjs
const nextConfig = {
async rewrites() {
return [
{
source: '/api/:path*',
destination: `${process.env.GO_BACKEND_URL}/api/:path*`,
},
]
},
}
export default nextConfig
ဒီ Configuration မှာ Browser က /api/users ကို Request လုပ်လိုက်တဲ့အခါ Next.js က Go Backend ဆီ Forward လုပ်ပေးမှာ ဖြစ်ပါတယ်။ Browser ဘက်ကနေ ကြည့်ရင် Request ဟာ မူလ Domain တစ်ခုတည်းကိုပဲ သွားနေတာဖြစ်လို့ Cross-Origin Request မဖြစ်တော့ဘဲ CORS Configuration လိုအပ်ချက်ကို လျှော့ချနိုင်ပါတယ်။
Next.js Server နှင့် Go Backend ကြား Network Connectivity, Server-side Environment Variables စနစ်တကျ သတ်မှတ်ထားရှိမှုနှင့် Timeout စီမံမှုများကို ဂရုပြုရန် လိုအပ်ပါတယ်။ ဒါ့အပြင် HttpOnly Session Cookies များနှင့် Authorization Header များကို Reverse Proxy မှတစ်ဆင့် Backend ဆီ မပျောက်ပျက်ဘဲ တိုက်ရိုက် လွှဲပြောင်းပေးနိုင်ရန် Proxy Headers (X-Forwarded-For, X-Forwarded-Proto) များကိုလည်း စနစ်တကျ Forward လုပ်ပေးသင့်ပါတယ်။ Streaming Responses နှင့် Server-Sent Events (SSE) အသုံးပြုလိုပါက Next.js Rewrites အစား Custom Route Handler ထဲမှ Node.js ReadableStream ဖြင့် Pipeline ချိတ်ဆက်ရန် လိုအပ်နိုင်ပါတယ်။
Pattern 2: Containerized Polyglot Monorepo with Docker
Enterprise Applications တွေ ဒါမှမဟုတ် Services အများအပြား ပါဝင်တဲ့ Project တွေမှာ Next.js နဲ့ Go ကို Monorepo တစ်ခုတည်းအတွင်း စီမံခန့်ခွဲပြီး Docker နဲ့ Deploy လုပ်နိုင်ပါတယ်။
my-platform/
├── apps/
│ ├── web/ # Next.js Frontend
│ └── api/ # Go Backend
├── docker-compose.yml
└── README.md
ဒီပုံစံမှာ Frontend, Backend နဲ့ Database တို့ကို သီးခြား Containers တွေအဖြစ် သတ်မှတ်ကာ Docker Compose ဖြင့် လွယ်ကူစွာ ချိတ်ဆက်နိုင်ပါတယ်။
services:
web:
build: ./apps/web
ports: ["3000:3000"]
environment:
GO_BACKEND_URL: http://backend:8080
depends_on: [backend]
backend:
build: ./apps/api
expose: ["8080"]
depends_on: [database]
database:
image: postgres:17
volumes: [postgres_data:/var/lib/postgresql/data]
Next.js Server ကနေ Go Backend ကို http://backend:8080 ဆိုတဲ့ Internal Service Name နဲ့ တိုက်ရိုက် Request လုပ်နိုင်ပါတယ်။ Frontend Team က TypeScript/React ကို အသုံးပြုနိုင်ပြီး Backend Team က Go ကို သီးခြားကိုင်တွယ်နိုင်ကာ Service တစ်ခုချင်းစီကို သီးခြား Scale လုပ်နိုင်ပါတယ်။
Pattern 3: Single Portable Binary with //go:embed
နောက်ထပ် စိတ်ဝင်စားစရာ Architecture တစ်ခုကတော့ Next.js Static Assets တွေကို Go Binary တစ်ခုတည်းထဲ Embed လုပ်ထားတဲ့ Single Binary Architecture ဖြစ်ပါတယ်။
Go 1.16 ကစပြီး //go:embed ကို အသုံးပြုနိုင်တာကြောင့် Next.js ကို Static Export (output: 'export') အဖြစ် Build လုပ်ပြီး ရရှိလာတဲ့ out/ ဖိုင်များကို Compiled Go Binary ထဲ တိုက်ရိုက် ထည့်သွင်းနိုင်ပါတယ်။
//go:embed out/*
var nextFS embed.FS
func main() {
distFS, _ := fs.Sub(nextFS, "out")
mux := http.NewServeMux()
mux.HandleFunc("/api/health", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`{"status":"ok","engine":"go"}`))
})
mux.Handle("/", http.FileServer(http.FS(distFS)))
log.Fatal(http.ListenAndServe(":8080", mux))
}
ဒီ Architecture တွင် Node.js Runtime ကို Production Server ပေါ် သီးခြား Install လုပ်စရာမလိုဘဲ Single Executable အဖြစ် ဖြန့်ချိနိုင်ပါတယ်။ ဒါပေမဲ့ Static Export ဖြစ်သောကြောင့် SSR, Dynamic Route Handlers ကဲ့သို့ Runtime Features များကို အသုံးမပြုနိုင်ဘဲ Desktop Utilities, Internal Dashboards နှင့် IoT Edge Devices များအတွက်သာ သင့်တော်ပါတယ်။
Architectural Comparison of Next.js + Go Setups
Architecture တစ်ခုချင်းစီမှာ အသုံးပြုနိုင်တဲ့ Features နဲ့ Deployment Requirements တွေကို အောက်ပါအတိုင်း နှိုင်းယှဉ်နိုင်ပါတယ်။
| Architecture Pattern | SSR / RSC Support | Hosting Model | Deployment Complexity | Best Use Case |
|---|---|---|---|---|
| Experimental Go Handler | Prototype-dependent | Platform-specific | Potentially Low | Experimental Integration |
| BFF via Next.js Rewrites | Fully Supported | Next.js + Go Server | Moderate | Production Web Apps, SaaS |
| Containerized Monorepo | Fully Supported | Docker / Cloud Run | Moderate to High | Enterprise, Microservices |
| Single Portable Binary | Static Rendering Only | Any OS Binary | Low at Runtime | IoT, Internal Tools, CLI Tools |
Goroutines vs Node.js Event Loop
Next.js နဲ့ Go ကို တွဲဖက်အသုံးပြုဖို့ စဉ်းစားရခြင်း၏ အဓိက အကြောင်းရင်းမှာ Concurrency Model ကွာခြားချက် ဖြစ်ပါတယ်။
| Dimension | Node.js | Go |
|---|---|---|
| Concurrency Model | Event Loop | Goroutines + Scheduler |
| Execution | JavaScript Main Thread | Multiple OS Threads |
| I/O Handling | Non-blocking I/O | Goroutines + Runtime Network Poller |
| CPU-bound Tasks | Worker Threads / Separate Processes | Multiple Goroutines |
| Memory Management | V8 Garbage Collector | Go Garbage Collector |
Node.js က I/O-heavy Web Applications တွေအတွက် အလွန်ကောင်းမွန်တဲ့ ရွေးချယ်မှုတစ်ခု ဖြစ်ပေမဲ့ Go ကတော့ Concurrent Heavy Computations နဲ့ High-throughput Processing တွေကို သက်သာတဲ့ Memory Footprint နဲ့ စီမံရာမှာ အလွန်အားသာပါတယ်။ ဒါ့အပြင် Next.js နဲ့ Go ကြားမှာ OpenAPI (Swagger) Schema သို့မဟုတ် Protobuf/ConnectRPC တို့ကို အသုံးပြုပြီး TypeScript Interfaces နဲ့ Go Structs တွေကို Single Source of Truth တစ်ခုတည်းကနေ Auto-generate လုပ်ပေးခြင်းအားဖြင့်၊ Language နှစ်ခုကြားက API Contract ကွာဟမှုတွေကို Compile Time မှာကတည်းက အပြည့်အဝ ကာကွယ်နိုင်သလို၊ OpenTelemetry W3C traceparent Header ကိုလည်း Next.js Server Actions ကနေ Go HTTP Middleware ဆီသို့ လက်ဆင့်ကမ်းပေးခြင်းအားဖြင့် End-to-End Distributed Tracing ကို တစ်ဆက်တည်း ခြေရာခံနိုင်မှာ ဖြစ်ပါတယ်။
Final Thoughts
Next.js နဲ့ Go ကို ပေါင်းစပ်အသုံးပြုခြင်းဟာ Frontend နဲ့ Backend တို့အတွက် မတူညီတဲ့ နည်းပညာတွေရဲ့ အားသာချက်တွေကို အပြည့်အဝ ရယူနိုင်တဲ့ Architecture တစ်ခု ဖြစ်ပါတယ်။
Next.js က React Server Components, SSR, Streaming နဲ့ Modern UI Development ကို တာဝန်ယူနိုင်ပြီး Go ကတော့ API Services, Background Processing နဲ့ Concurrent Workloads တွေကို စိတ်ချရစွာ ကိုင်တွယ်ပေးနိုင်ပါတယ်။
Architecture ကောင်းတစ်ခုဆိုတာ နည်းပညာတွေ အများကြီး ပေါင်းစပ်ထားတာ မဟုတ်ပါဘူး။ Application ရဲ့ လက်ရှိလိုအပ်ချက်နဲ့ ကိုက်ညီတဲ့ နည်းပညာတွေကို သင့်တော်တဲ့နေရာမှာ ရိုးရှင်းထိရောက်စွာ အသုံးပြုထားနိုင်ခြင်းပဲ ဖြစ်ပါတယ်။