Next.js 14 memperkenalkan App Router, React Server Components, dan streaming SSR yang lebih cepat. Ikuti tutorial step‑by‑step ini untuk membuat proyek modern, mengonfigurasi lingkungan, menulis kode contoh, serta menerapkan best practice keamanan dan performa.
1. Prasyarat & Persiapan Lingkungan
Pastikan Anda sudah menginstall Node.js 20.10+ dan npm 10+ (atau pnpm/yarn versi terbaru). Jika belum, unduh dari nodejs.org. Verifikasi instalasi:
node -v
npm -v
Selain itu, siapkan Git untuk version control dan Docker (opsional) bila ingin menjalankan container.
2. Membuat Proyek Next.js 14
npx create-next-app@latest my-next14-app --experimental-app
cd my-next14-app
Flag --experimental-app secara otomatis mengaktifkan struktur app/ (App Router). Pada saat penulisan (Mei 2026) ini sudah menjadi default, tetapi perintah di atas memastikan kompatibilitas.
3. Instalasi Dependensi Tambahan
Untuk contoh ini kita pakai TypeScript, Tailwind CSS, dan linting ESLint + Prettier.
# TypeScript
npm install --save-dev typescript @types/react @types/node
# Tailwind CSS
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p
# ESLint & Prettier
npm install -D eslint prettier eslint-config-prettier eslint-plugin-react eslint-plugin-react-hooks
Konfigurasikan tailwind.config.js:
module.exports = {
content: ["./app/**/*.{js,ts,jsx,tsx}", "./pages/**/*.{js,ts,jsx,tsx}"],
theme: { extend: {} },
plugins: [],
};
4. Struktur Dasar App Router
Folder app/ menggantikan pages/. Contoh struktur minimal:
app/
│ layout.tsx # Layout global
│ page.tsx # Root page (/)
│ loading.tsx # UI loading state
│ error.tsx # Error boundary
│
└── dashboard/
│ page.tsx # /dashboard
│ layout.tsx # Layout khusus dashboard
│ loading.tsx
4.1 Layout Global (app/layout.tsx)
import './globals.css';
export const metadata = {
title: 'Next.js 14 Demo',
description: 'App Router + Server Components',
};
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
{children}
);
}
4.2 Root Page (app/page.tsx) – Server Component
export default async function HomePage() {
// Contoh fetch data di server side
const res = await fetch('https://api.github.com/repos/vercel/next.js');
const repo = await res.json();
return (
Welcome to Next.js 14
Repository: {repo.full_name}
);
}
5. Konfigurasi TypeScript & ESLint
Jalankan:
npx tsc --init
Tambahkan "jsx": "preserve" dan "moduleResolution": "node" di tsconfig.json. Buat file .eslintrc.json:
{
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint", "react", "react-hooks"],
"extends": [
"eslint:recommended",
"plugin:react/recommended",
"plugin:@typescript-eslint/recommended",
"prettier"
],
"settings": {"react": {"version": "detect"}},
"rules": {"react/react-in-jsx-scope": "off"}
}
6. Membuat API Route dengan Edge Runtime
Next.js 14 memungkinkan app/api/ sebagai Edge Functions.
// app/api/hello/route.ts
export const runtime = 'edge';
export async function GET(request: Request) {
return new Response(JSON.stringify({ message: 'Hello from Edge!' }), {
headers: { 'Content-Type': 'application/json' },
});
}
Uji dengan curl http://localhost:3000/api/hello.
7. Optimasi Performance & Best Practice
- Cache API fetch: gunakan
fetch(..., { next: { revalidate: 60 } })untuk ISR otomatis. - Streaming SSR: dengan Server Components, data mengalir ke client secepatnya tanpa blocking.
- Lazy loading images lewat
next/image(v14 tetap mendukung). - Security headers: tambahkan middleware
app/middleware.tsuntuk CSP, X‑Frame‑Options.
7.1 Contoh Middleware CSP
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
export function middleware(request: NextRequest) {
const response = NextResponse.next();
response.headers.set(
'Content-Security-Policy',
"default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'"
);
return response;
}
export const config = { matcher: '/' };
8. Deploy ke Vercel (atau Platform Lain)
- Push repo ke GitHub.
- Buat project baru di Vercel dengan import repository.
- Pastikan build command
npm run builddan output directory.next. - Vercel otomatis mengaktifkan Edge Functions untuk API route.
Jika menggunakan Docker, buat Dockerfile sederhana:
FROM node:20-alpine AS builder
WORKDIR /app
COPY . .
RUN npm ci && npm run build
FROM node:20-alpine AS runner
WORKDIR /app
COPY --from=builder /app/.next ./.next
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package.json ./
EXPOSE 3000
CMD ["npm", "start"]
9. Testing & Debugging
Jalankan npm run dev dan buka http://localhost:3000. Untuk debugging server components gunakan console.log dalam fungsi async; hasil akan muncul di terminal. Untuk client side, gunakan Chrome DevTools.
Tambahkan unit test dengan jest & @testing-library/react:
npm install -D jest @testing-library/react @testing-library/jest-dom ts-jest
npx jest --init
10. Ringkasan & Next Steps
Anda kini memiliki proyek Next.js 14 dengan App Router, Server Components, Edge API, Tailwind UI, TypeScript, dan CI/CD dasar. Langkah selanjutnya dapat meliputi:
- Integrasi NextAuth.js untuk otentikasi.
- Menambahkan React Query atau TanStack Query untuk state management.
- Memanfaatkan Route Handlers untuk CRUD penuh.
- Menerapkan Internationalization (i18n) dengan built‑in Next.js support.
Next.js 14 membawa paradigma baru dalam pengembangan aplikasi React modern lewat App Router dan Server Components. Dengan mengikuti langkah‑step di atas, Anda dapat membangun aplikasi yang cepat, aman, dan mudah di‑scale, sambil menjaga kode tetap bersih melalui TypeScript, linting, dan best practice performa. Jangan ragu bereksperimen dengan Edge Functions, streaming SSR, dan integrasi AI untuk menambah nilai bisnis pada proyek Anda.
Tutorial lengkap setup Next.js 14 dengan App Router, React Server Components, TypeScript, Tailwind, dan best practice security serta performance untuk developer di 2026.
Programming,Software Engineering,Web Development,Next.js,React,Server Components,App Router,TypeScript,Tailwind CSS
#Programming #SoftwareEngineering #WebDev #Tech #Coding
0 Komentar