Next.js 14 menjadi standar baru untuk pengembangan web React modern. Tutorial ini menuntun Anda step‑by‑step melakukan instalasi, konfigurasi, menulis kode contoh, serta best practice untuk produksi, lengkap dengan integrasi TypeScript dan Tailwind CSS.
1. Prasyarat
Sebelum memulai, pastikan Anda memiliki:
- Node.js v20.10 atau lebih baru (
node -v) - npm v10 atau Yarn v4
- Git untuk version control
- Editor kode, disarankan VS Code dengan ekstensi
ESLint,Prettier, danTailwind CSS IntelliSense
2. Instalasi Next.js 14
npx create-next-app@latest my-next14-app \
--ts \
--experimental-app \
--eslint \
--use-npm
cd my-next14-app
Perintah di atas menghasilkan proyek Next.js dengan:
- App Router (fitur
app/directory) - TypeScript terkonfigurasi
- ESLint bawaan
3. Menambahkan Tailwind CSS
npm install -D tailwindcss@latest postcss@latest autoprefixer@latest
npx tailwindcss init -p
Ubah tailwind.config.js 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: [],
};
Tambahkan direktif Tailwind ke ./app/globals.css:
@tailwind base;
@tailwind components;
@tailwind utilities;
4. Menyiapkan Struktur Direktori
Next.js 14 menganjurkan app/ sebagai entry point. Buat layout dasar dan halaman contoh:
mkdir -p app/(components) app/dashboard
# app/layout.tsx
cat > app/layout.tsx <<'EOF'
import './globals.css';
import type { ReactNode } from 'react';
export const metadata = {
title: 'Next.js 14 Starter',
description: 'Demo app dengan App Router, TypeScript, Tailwind',
};
export default function RootLayout({ children }: { children: ReactNode }) {
return (
{children}
);
}
EOF
# app/page.tsx (home)
cat > app/page.tsx <<'EOF'
export default function Home() {
return (
Welcome to Next.js 14
Built with TypeScript, Tailwind CSS, and the new App Router.
);
}
EOF
# app/dashboard/page.tsx
cat > app/dashboard/page.tsx <<'EOF'
export default function Dashboard() {
return (
Dashboard
This is a protected route example (later we add auth).
);
}
EOF
Struktur ini sudah siap untuk pengembangan berkelanjutan.
5. Konfigurasi ESLint & Prettier
Gunakan konfigurasi yang disarankan komunitas 2026:
npm install -D eslint prettier eslint-config-prettier eslint-plugin-react eslint-plugin-react-hooks @typescript-eslint/parser @typescript-eslint/eslint-plugin
# .eslintrc.js
cat > .eslintrc.js <<'EOF'
module.exports = {
root: true,
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint', 'react', 'react-hooks'],
extends: [
'eslint:recommended',
'plugin:react/recommended',
'plugin:@typescript-eslint/recommended',
'plugin:react-hooks/recommended',
'prettier'
],
settings: { react: { version: 'detect' } },
env: { browser: true, node: true, es2022: true },
rules: {
'react/react-in-jsx-scope': 'off', // Next.js auto‑imports React
'@typescript-eslint/no-unused-vars': ['warn', { argsIgnorePattern: '^_' }],
},
};
EOF
# .prettierrc
cat > .prettierrc <<'EOF'
{
"singleQuote": true,
"trailingComma": "es5",
"printWidth": 100,
"tabWidth": 2
}
EOF
Tambahkan script lint di package.json:
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "eslint . --ext .js,.ts,.jsx,.tsx"
}
6. Menambahkan API Route dengan Edge Runtime
Next.js 14 memperkenalkan app/api dengan Edge Runtime secara default. Buat endpoint health check:
mkdir -p app/api/health/route.ts
cat > app/api/health/route.ts <<'EOF'
import { NextResponse } from 'next/server';
export const runtime = 'edge'; // menjalankan di Vercel Edge atau Cloudflare Workers
export async function GET() {
return NextResponse.json({ status: 'ok', timestamp: Date.now() });
}
EOF
7. Optimasi Produksi – Image, Font dan Internationalization
- next/image: Pastikan
next.config.mjsberisi domain yang di‑allow. - Font optimal: Gunakan
@next/fontuntuk self‑hosting Google Fonts. - i18n: Tambahkan
i18ndi konfigurasi jika aplikasi multilingual.
// next.config.mjs
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
export default {
images: {
remotePatterns: [{ protocol: 'https', hostname: 'assets.example.com' }],
},
i18n: {
locales: ['en', 'id'],
defaultLocale: 'en',
},
experimental: { appDir: true },
};
8. Deploy ke Vercel (Free Tier 2026)
- Push repository ke GitHub.
- Buka vercel.com dan klik New Project.
- Pilih repo, biarkan preset
Next.js. - Pastikan variable
NODE_VERSION=20di Settings > Environment Variables. - Deploy! Vercel otomatis meng‑run
npm run builddan menyajikan/.nextsebagai serverless.
Setelah deploy, cek /api/health untuk memastikan Edge Function bekerja.
9. Best Practice untuk Skalabilitas
- Folder
app/(components): gunakan tanda kurung untuk grouping tanpa menambah route. - Lazy Loading: gunakan
next/dynamicpada komponen berat. - Cache Control: Return
Cache‑Control: public, max-age=31536000, immutablepada static assets melaluinext.config.mjs. - Type Safety: aktifkan
strictNullChecksditsconfig.jsondan gunakanzoduntuk validasi payload API. - Security: tambahkan header CSP melalui Vercel Edge Middleware.
// app/middleware.ts import { NextResponse } from 'next/server'; export function middleware(request) { const response = NextResponse.next(); response.headers.set('Content-Security-Policy', "default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com"); return response; }
10. Testing dengan Jest & React Testing Library
npm install -D jest @types/jest ts-jest @testing-library/react @testing-library/jest-dom
# jest.config.ts
export default {
preset: 'ts-jest',
testEnvironment: 'jsdom',
moduleNameMapper: {
'^@/([^.]*)
Next.js 14 dengan App Router, TypeScript, dan Tailwind CSS menawarkan fondasi kuat untuk aplikasi web berperforma tinggi. Tutorial di atas tidak hanya memberi Anda instalasi dan konfigurasi dasar, tetapi juga menambahkan lapisan keamanan, testing, serta observability yang penting dalam lingkungan produksi 2026. Terapkan best practice yang disebutkan, dan Anda akan siap menghadapi tantangan skala besar serta integrasi dengan layanan cloud modern.
Panduan lengkap step-by-step setup Next.js 14 dengan App Router, TypeScript, Tailwind CSS, best practice, testing, dan deployment ke Vercel – tutorial terbaru 2026 untuk Programming, Software Engineering, dan Web Development.
Programming,Software Engineering,Web Development,Next.js 14,TypeScript,Tailwind CSS,App Router,DevOps
#Programming #SoftwareEngineering #WebDev #Tech #Coding
: '/$1',
},
setupFilesAfterEnv: ['/jest.setup.ts'],
};
# jest.setup.ts
import '@testing-library/jest-dom';
Contoh test untuk Home:
// app/__tests__/home.test.tsx
import { render, screen } from '@testing-library/react';
import Home from '@/app/page';
test('renders welcome message', () => {
render( );
expect(screen.getByRole('heading', { name: /welcome to next\.js 14/i })).toBeInTheDocument();
});
11. Monitoring & Observability
Integrasikan Vercel Analytics atau gunakan OpenTelemetry SDK untuk mengirim trace ke Datadog/Elastic. Contoh kode singkat:
npm install @opentelemetry/api @opentelemetry/sdk-node @opentelemetry/instrumentation-nextjs
// otel.ts
import { NodeSDK } from '@opentelemetry/sdk-node';
import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';
const sdk = new NodeSDK({
instrumentations: [getNodeAutoInstrumentations()],
});
sdk.start();
Import ./otel di next.config.mjs atau di entry server untuk meng‑capture request latency.
12. Ringkasan Step
- Install Node.js & create‑next‑app dengan flag
--experimental-app. - Add Tailwind CSS dan konfigurasikan
tailwind.config.jssertaglobals.css. - Bangun struktur
app/(layout, page, nested routes). - Setup ESLint & Prettier untuk code quality.
- Buat API Route Edge untuk health check.
- Optimasi produksi (image, font, i18n, CSP).
- Deploy ke Vercel dengan satu klik.
- Implementasikan best practice (lazy load, cache, type safety, security).
- Tambahkan testing dengan Jest & React Testing Library.
- Integrasikan observability menggunakan OpenTelemetry.
Dengan mengikuti langkah ini, Anda memiliki basis proyek Next.js 14 yang modern, siap skala, dan terjaga kualitasnya.
{{ $json.conclusion }}
{{ $json.seo.meta_description }}
{{ $json.seo.keywords }}
{{ $json.hashtags }}
0 Komentar