Next.js 14 menjadi pilihan utama untuk membangun aplikasi React modern dengan rendering hybrid, server actions, dan integrasi edge runtime. Tutorial ini mengajak Anda menginstal, mengkonfigurasi, dan mengoptimasi proyek Next.js 14 secara step‑by‑step, lengkap dengan contoh kode, best practice, serta tips debugging yang relevan pada Mei 2026.

1. Persiapan Lingkungan Pengembangan

Pastikan sistem Anda memenuhi persyaratan berikut:

  • Node.js v20.12.x atau lebih baru (LTS)
  • npm 10.x atau yarn 4.x
  • Git untuk version control
  • Docker (opsional, untuk containerization)

Verifikasi instalasi dengan perintah:

node -v
npm -v

2. Membuat Proyek Next.js 14 Baru

Gunakan template resmi yang sudah termasuk app/ router dan TypeScript.

npx create-next-app@latest my-next14-app \
  --ts \
  --experimental-app-router
cd my-next14-app

Perintah di atas menghasilkan struktur folder berikut:

my-next14-app/
├─ app/
│  ├─ layout.tsx
│  ├─ page.tsx
│  └─ ...
├─ public/
├─ src/
│  └─ ...
├─ next.config.mjs
├─ tsconfig.json
└─ package.json

3. Konfigurasi Dasar next.config.mjs

Aktifkan fitur terbaru dan atur edge runtime untuk API routes.

import { defineConfig } from 'next';

export default defineConfig({
  reactStrictMode: true,
  experimental: {
    appDir: true,
    serverActions: true,
    typedRoutes: true,
  },
  images: {
    remotePatterns: [{ hostname: 'images.unsplash.com' }],
  },
  // Enable edge runtime for API routes
  runtime: 'edge',
});

4. Menyiapkan TypeScript Strict Mode

Update tsconfig.json untuk meningkatkan keamanan tipe.

{
  "compilerOptions": {
    "target": "es2022",
    "module": "esnext",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": false,
    "skipLibCheck": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve"
  },
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
  "exclude": ["node_modules"]
}

5. Membuat Layout Global dengan Server Components

File app/layout.tsx menjadi kerangka aplikasi.

import './globals.css';
import type { Metadata } from 'next';

export const metadata: Metadata = {
  title: 'Next.js 14 Demo',
  description: 'Contoh aplikasi dengan App Router & Server Actions',
};

export default function RootLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  return (
    
      
        {children}
      
    
  );
}

6. Membuat Halaman Utama dengan Server Actions

Halaman app/page.tsx menampilkan formulir yang memanggil server action untuk menyimpan data ke mock API.

"use server";
import { revalidatePath } from 'next/cache';
import { redirect } from 'next/navigation';

async function submitMessage(formData: FormData) {
  const name = formData.get('name') as string;
  const message = formData.get('message') as string;

  // Simulasi POST ke endpoint internal
  await fetch('https://dummyjson.com/comments/add', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ body: message, userId: 1, postId: 1, name }),
  });

  // Revalidate cache agar UI menampilkan data terbaru
  revalidatePath('/messages');
  redirect('/messages');
}

export default function HomePage() {
  return (
    

Kirim Pesan