Panduan Lengkap Setup Next.js 14 dengan TypeScript, Tailwind CSS, dan Deployment ke Vercel (2026)


Next.js 14 telah menjadi standar de‑facto untuk aplikasi web React modern. Tutorial ini membimbing Anda langkah demi langkah mulai dari instalasi, konfigurasi TypeScript dan Tailwind CSS, hingga deployment otomatis ke Vercel, lengkap dengan best practice terbaru untuk keamanan dan performa.

1. Prasyarat

Pastikan Anda memiliki:

  • Node.js v20.x atau lebih tinggi (download)
  • npm v10 atau Yarn v4 (pilih salah satu)
  • Akun Vercel (gratis) untuk deployment
  • Editor kode modern, contoh Visual Studio Code

2. Membuat Proyek Next.js 14 Baru

npx create-next-app@latest my-next14-app --ts --app
cd my-next14-app

Perintah di atas akan menghasilkan struktur folder baru dengan app/ router, TypeScript terkonfigurasi, dan file next.config.mjs yang sudah siap.

3. Menambahkan Tailwind CSS

Tailwind CSS tetap menjadi pilihan utama untuk styling utility‑first. Ikuti langkah berikut:

npm install -D tailwindcss@latest postcss@latest autoprefixer@latest
npx tailwindcss init -p

File tailwind.config.cjs akan dibuat. Edit menjadi:

/** @type {import('tailwindcss').Config} */
module.exports = {
  content: [
    "./app/**/*.{js,ts,jsx,tsx}",
    "./pages/**/*.{js,ts,jsx,tsx}",
    "./components/**/*.{js,ts,jsx,tsx}"
  ],
  theme: {
    extend: {},
  },
  plugins: [],
};

Selanjutnya, buka app/globals.css dan tambahkan directive Tailwind:

@tailwind base;
@tailwind components;
@tailwind utilities;

4. Konfigurasi TypeScript Lanjutan

Next.js 14 sudah menyertakan tsconfig.json standar. Untuk meningkatkan developer experience, tambahkan opsi berikut:

{
  "compilerOptions": {
    "strict": true,
    "noImplicitAny": true,
    "forceConsistentCasingInFileNames": true,
    "moduleResolution": "Node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "incremental": true,
    "paths": {
      "@/*": ["./*" ]
    }
  },
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
  "exclude": ["node_modules"]
}

Dengan paths di atas, Anda dapat meng‑import modul menggunakan alias @/components/Button tanpa relatif path yang berbelit.

5. Struktur Folder yang Clean (Clean Architecture)

Organisasi folder yang konsisten membantu skalabilitas. Contoh struktur rekomendasi:

/app          # Next.js App Router
  /dashboard  # Halaman utama dashboard
  /api        # Route Handlers API
/components   # UI reusable
  /ui          # Atoms, Molecules (Button, Card)
  /layout      # Layout wrapper
/lib           # Utility functions, API client
/services      # Business logic (fetch, mutate)
/types         # TypeScript tipe global

Pastikan semua file UI berada di /components/ui dan tidak mencampur logika data.

6. Membuat Halaman Dashboard Pertama

File: app/dashboard/page.tsx

import React from "react";
import Card from "@/components/ui/Card";

export default function Dashboard() {
  return (
    
); }

Komponen Card berada di components/ui/Card.tsx dan menggunakan Tailwind untuk styling.

7. Menambahkan API Route dengan Server Actions (Next.js 14)

File: app/api/submit/route.ts

import { NextResponse } from "next/server";

export async function POST(request: Request) {
  const { name, email } = await request.json();
  // Simulasi penyimpanan ke DB (contoh Prisma)
  // await prisma.user.create({ data: { name, email } });
  return NextResponse.json({ success: true, message: "Data tersimpan" }, { status: 201 });
}

Dengan app/api Anda langsung memanfaatkan server actions tanpa konfigurasi tambahan.

8. Mengoptimalkan Performansi (Image, Font, Edge Middleware)

  • Next/Image: Ganti <img> dengan next/image untuk lazy‑load otomatis.
  • Font Optimization: Tambahkan next/font/google di layout.tsx.
    import { Inter } from "next/font/google";
    const inter = Inter({ subsets: ["latin"] });
    export default function RootLayout({ children }: { children: React.ReactNode }) {
      return (
        
          {children}
        
      );
    }
    
  • Edge Middleware: Implementasikan cache‑first strategy untuk API publik.
    import { NextResponse } from "next/server";
    export async function middleware(request) {
      const response = NextResponse.next();
      response.headers.set("Cache-Control", "public, max-age=60");
      return response;
    }
    export const config = { matcher: "/api/:path*" };
    

9. Pengujian Unit dengan Jest & React Testing Library

npm install -D jest @types/jest ts-jest @testing-library/react @testing-library/jest-dom

Tambahkan jest.config.js:

module.exports = {
  preset: "ts-jest",
  testEnvironment: "jsdom",
  moduleNameMapper: {
    "^@/(.*)$": "/$1",
  },
  setupFilesAfterEnv: ["@testing-library/jest-dom/extend-expect"],
};

Contoh test untuk Card:

import { render, screen } from "@testing-library/react";
import Card from "@/components/ui/Card";

test("renders title and value", () => {
  render();
  expect(screen.getByText("Test")).toBeInTheDocument();
  expect(screen.getByText("123")).toBeInTheDocument();
});

10. CI/CD dengan GitHub Actions ke Vercel

Buat berkas .github/workflows/deploy.yml:

name: Deploy to Vercel
on:
  push:
    branches: [main]
jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: "20"
      - run: npm ci
      - run: npm run build
      - name: Deploy
        uses: amondnet/vercel-action@v20
        with:
          vercel-token: ${{ secrets.VERCEL_TOKEN }}
          vercel-args: "--prod"
          working-directory: .
          github-token: ${{ secrets.GITHUB_TOKEN }}

Pastikan Anda menambahkan VERCEL_TOKEN di Settings → Secrets.

11. Best Practice Keamanan

  • Gunakan next-auth untuk otentikasi berbasis JWT atau OAuth.
  • Aktifkan Content‑Security‑Policy lewat headers() di next.config.mjs.
  • Validasi semua input API dengan zod schema.

12. Monitoring & Logging

Integrasikan Vercel Analytics untuk Core Web Vitals dan gunakan logflare (atau Datadog) untuk log server‑side.

13. Deploy ke Vercel

Jika repositori sudah terhubung ke Vercel, setiap push ke main akan otomatis terdeploy melalui workflow di atas. Anda juga dapat melakukan deploy manual via CLI:

npx vercel --prod

Setelah selesai, aplikasi Anda tersedia di URL https://my-next14-app.vercel.app.

14. Ringkasan Langkah

  1. Install Next.js 14 dengan TypeScript.
  2. Tambahkan Tailwind CSS dan konfigurasikan.
  3. Atur TypeScript strict mode & path alias.
  4. Gunakan struktur folder Clean Architecture.
  5. Buat halaman, komponen UI, dan API route.
  6. Optimalkan gambar, font, dan middleware edge.
  7. Tambahkan unit test dengan Jest.
  8. Konfigurasi GitHub Actions untuk CI/CD ke Vercel.
  9. Implementasikan keamanan dasar & monitoring.
  10. Deploy dan pantau performa secara real‑time.

Dengan mengikuti tutorial ini Anda tidak hanya memiliki aplikasi Next.js 14 yang modern, type‑safe, dan responsive, tetapi juga pipeline CI/CD yang otomatis, keamanan yang terukur, dan monitoring performa yang siap mendukung skala produksi. Kombinasi TypeScript, Tailwind CSS, dan deployment Vercel menjadikan stack ini pilihan utama bagi tim Programming, Software Engineering, dan Web Development pada 2026.
Panduan lengkap setup Next.js 14 dengan TypeScript, Tailwind CSS, CI/CD GitHub Actions, dan deployment ke Vercel. Tutorial step‑by‑step untuk developer Programming, Software Engineering, dan Web Development.

Programming,Software Engineering,Web Development,Next.js,TypeScript,Tailwind CSS,Vercel,CI/CD

#Programming #SoftwareEngineering #WebDev #Tech #Coding

Posting Komentar

0 Komentar