กลับไปที่บทความ
Next.js React Performance DevOps

Next.js Full-Stack Production Checklist

พลากร วรมงคล
20 กรกฎาคม 2567 10 นาที

“การเปิดตัวแอปพลิเคชัน Next.js ลงการผลิตมากกว่า just running next build หลังจาก deploying หกแอปพลิเคชัน Next.js ไป production, นี่คือ comprehensive checklist ที่ฉันขอให้ได้ตั้งแต่เริ่มต้น — covering performance, security, SEO, และ operational readiness”

ทำไม Checklist มีความสำคัญ

ทุกครั้งที่ฉัน launched Next.js application โดยไม่มี checklist, บางอย่างได้รับการข้าม อาจจะ security headers, หรือ image optimization, หรือ proper error handling, หรือ cache configuration ทีละรายการ, ไม่มีข้อใดเป็นเรื่องยาก รวมกัน, พวกเขาง่ายการลืม under launch pressure

Checklist นี้ distillation ของหกการเปิดตัวการผลิต ไม่ทุกอย่างถูก apply ไป every project, แต่ reviewing ทุกรายการใช้เวลานาที และ catching รายการที่ข้าม saves ชั่วโมงของ post-launch firefighting

Performance

Image Optimization

ใช้ Next.js Image component สำหรับทุกภาพ มัน handles lazy loading, responsive sizing, และ format conversion (WebP, AVIF) อัตโนมัติ Most common mistake คือใช้ plain img tags “just for now” และไม่เคยกลับไป configure image domains ใน next.config และตั้งค่า quality values ที่เหมาะสม — 75 คือ good default สำหรับ photos, 90+ สำหรับ sharp graphics

Font Optimization

ใช้ next/font สำหรับ web fonts มัน eliminates layout shift โดยการ preloading fonts และใช้ font-display: swap Self-hosting fonts ผ่าน next/font เร็วกว่า Google Fonts CDN สำหรับ deployments ส่วนใหญ่เพราะมัน eliminates DNS lookup และ connection ไป fonts.googleapis.com

Bundle Analysis

เรียก build ของคุณด้วย bundle analysis enabled ก่อน launch มอง unexpectedly large dependencies, duplicate packages, และ code ที่ควรจะ dynamically imported single unoptimized import สามารถเพิ่ม hundreds of KB ไป bundle ของคุณ

Common offenders: moment.js (ใช้ date-fns หรือ dayjs แทน), lodash (import individual functions, ไม่ใช่ entire library), และ icon libraries (import specific icons, ไม่ใช่ entire set)

Core Web Vitals

วัด Largest Contentful Paint (LCP), First Input Delay (FID), และ Cumulative Layout Shift (CLS) ใช้ Lighthouse และ real user monitoring Google ใช้ metrics เหล่านี้สำหรับ search ranking, และ users รู้สึก direct

LCP above 2.5 seconds: ตรวจสอบ largest above-the-fold image หรือ text block ของคุณ คุณกำลังใช้ next/image หรือไม่ Font loading efficiently หรือไม่ FID above 100ms: ตรวจสอบ heavy JavaScript execution บน page load CLS above 0.1: ตรวจสอบ elements ที่โหลด without dimensions (images, ads, dynamic content)

Static Generation เมื่อไป

ใช้ static generation (SSG) สำหรับ pages ที่ไม่เปลี่ยน per-request: marketing pages, blog posts, documentation pages นี้ถูก served จาก CDN ด้วย zero server-side computation ใช้ Incremental Static Regeneration (ISR) สำหรับ pages ที่เปลี่ยน occasionally แต่ ไม่ต้องจะสด on every request

ถูกสงวน server-side rendering (SSR) สำหรับ pages ที่ต้อง reflect real-time data: user dashboards, shopping carts, personalized content

Security

HTTP Security Headers

configure security headers ใน middleware ของคุณหรือ next.config: Content-Security-Policy (prevents XSS และ injection attacks), X-Frame-Options (prevents clickjacking), X-Content-Type-Options (prevents MIME sniffing), Referrer-Policy (controls referrer information), และ Strict-Transport-Security (enforces HTTPS)

Content-Security-Policy มี most complex และ most important เริ่มด้วย restrictive policy และ loosen มันตามที่จำเป็น Report-only mode ให้ test โดยไม่ breaking site ของคุณ

Environment Variables

ไม่เคยเปิดเผย server-side secrets ไป client ใน Next.js, เพียง variables prefixed ด้วย NEXT_PUBLIC_ ถูก bundled ลงใน client-side code Audit environment variables ของคุณเพื่อให้แน่ใจว่า database credentials, API secrets, และ internal URLs ไม่ได้ accidentally prefixed

เก็บ secrets ใน deployment platform’s secret management (Vercel Environment Variables, AWS Secrets Manager, etc.), ไม่ใน .env files committed ไป repository ของคุณ

Authentication และ Authorization

implement authentication บน every protected route, ไม่เพียง page level API routes, server actions, และ middleware ควร verify user’s identity และ permissions common vulnerability คือ protecting UI แต่ leaving API routes เปิด

ใช้ httpOnly, secure, sameSite cookies สำหรับ session tokens ไม่เคยเก็บ tokens ใน localStorage — มัน vulnerable ไป XSS attacks

Rate Limiting

ปกป้อง API routes ของคุณจาก abuse implement rate limiting อยู่บน authenticated user ID (สำหรับ logged-in users) หรือ IP address (สำหรับ public endpoints) return 429 Too Many Requests ด้วย Retry-After header

SEO

Metadata

ใช้ Next.js Metadata API เพื่อตั้ง title, description, และ Open Graph tags บน every page ทุก page ควรมี unique, descriptive title และ compelling description Open Graph images improve click-through rates จาก social media

Sitemap และ Robots

Generate dynamic sitemap ที่ /sitemap.xml ที่ include all public pages ด้วย appropriate priority และ changefreq values สร้าง robots.txt ที่ allow search engine crawling ของ public pages และ blocks private areas

Structured Data

เพิ่ม JSON-LD structured data สำหรับ content type ของคุณ: Article สำหรับ blog posts, Product สำหรับ e-commerce, Organization สำหรับ company page, BreadcrumbList สำหรับ navigation Google ใช้ structured data สำหรับ rich snippets ใน search results

Canonical URLs

ตั้ง canonical URLs บน every page ไป prevent duplicate content issues นี่โดยเฉพาะ important ถ้า content ของคุณ accessible ที่ multiple URLs (ด้วยและ without trailing slashes, ด้วย query parameters, etc.)

Error Handling

Error Boundaries

สร้าง custom error.tsx และ not-found.tsx pages ที่ app level และ สำหรับ critical route segments แอปพลิเคชัน production ควรไม่เคยแสดง default Next.js error page — มัน exposes tech stack ของคุณและ looks unprofessional

Error Tracking

integrate error tracking service (Sentry, Bugsnag, หรือ similar) ที่ captures ทั้ง client-side และ server-side errors configure source maps เพื่อให้คุณสามารถ debug minified production errors ด้วย original file names และ line numbers

Graceful Degradation

plan สำหรับ external service failures ถ้า CMS ของคุณ down, site ของคุณสามารถยัง render cached content หรือไม่ ถ้า payment provider ของคุณ slow, checkout show helpful message แทน hanging ทุก external dependency ควร have fallback strategy

Operational Readiness

Health Check Endpoint

สร้าง API route ที่ verifies แอปพลิเคชัน ของคุณสามารถ connect ไป dependencies ของมัน (database, cache, external APIs) Monitoring systems และ load balancers ใช้นี่เพื่อ determine ถ้า instance healthy

Logging

implement structured logging (JSON format) ด้วย request IDs สำหรับ traceability log ที่ appropriate levels: errors สำหรับ failures, warnings สำหรับ degraded behavior, info สำหรับ significant events ไม่ log sensitive data (passwords, tokens, personal information)

Environment Parity

development, staging, และ production environments ของคุณควร as similar ที่เป็นไปได้ ใช้ Node.js version เดียวกัน, build process เดียวกัน, และ similar infrastructure differences ระหว่าง environments คือ leading source ของ “works บน staging, breaks ใน production” incidents

Deployment Strategy

configure zero-downtime deployments ด้วย health check verification deployment platform ของคุณควร start new instances, verify พวกเขา healthy, route traffic ไป them, แล้ว stop old instances deploy ควรไม่เคย cause user-facing errors

Backup และ Recovery

สำหรับ applications ด้วย data persistence, ensure regular backups และ test recovery process backup คุณ ไม่เคยมี restored คือ hope, ไม่ใช่ plan run recovery drill at least quarterly

Pre-Launch Final Checks

ก่อน clicking deploy บน launch day: test ทุก page บน mobile และ desktop, verify ทั้งหมด forms submit correctly, check ว่า error pages render properly, confirm analytics และ monitoring ได้รับ data, verify SSL certificates valid และ auto-renewing, test deployment rollback process, และ ensure someone ว่าง ไปเฝ้าระหว่าง few hours หลัง launch

Checklist นี้จะไม่ prevent ทุกปัญหาการผลิต, แต่ eliminates preventable ones — ซึ่ง most ของพวกเขา

Comments powered by Giscus are not yet configured. Set PUBLIC_GISCUS_REPO_ID and PUBLIC_GISCUS_CATEGORY_ID in apps/web/.env to enable.

PV

เขียนโดย พลากร วรมงคล

Software Engineer Specialist ประสบการณ์กว่า 20 ปี เขียนเกี่ยวกับ Architecture, Performance และการสร้างระบบ Production

เพิ่มเติมเกี่ยวกับผม

บทความที่เกี่ยวข้อง