๐ Nuxt.js & Server-Side Rendering (SSR) untuk Vue.js
Nuxt.js adalah framework berbasis Vue untuk membangun aplikasi web modern dengan fitur seperti Server-Side Rendering (SSR), Static Site Generation (SSG), dan File-Based Routing. Nuxt menyederhanakan pengembangan Vue untuk proyek skala menengah hingga besar.
๐ 1. Kelebihan Nuxt.jsโ
- โ SEO-friendly (berkat SSR/SSG)
- โ File-based routing (tanpa config routing manual)
- โ Zero config setup
- โ Built-in middleware & auth
- โ Dukungan TypeScript dan Tailwind
โ๏ธ 2. Instalasi Proyek Nuxtโ
npx nuxi init nuxt-app
cd nuxt-app
npm install
npm run dev
nuxi
adalah CLI terbaru untuk Nuxt 3.
๐ 3. Struktur Direktori Nuxtโ
nuxt-app/
โโโ pages/ # โฌ
๏ธ Halaman otomatis jadi route
โ โโโ index.vue # โฌ
๏ธ /
โ โโโ about.vue # โฌ
๏ธ /about
โโโ components/ # Komponen Vue biasa
โโโ layouts/ # Layout utama dan kustom
โโโ plugins/ # Plugin seperti Axios, VueUse
โโโ middleware/ # Auth dan guard route
โโโ nuxt.config.ts # Konfigurasi Nuxt
โโโ app.vue # Root App (opsional)
๐ 4. Routing Otomatisโ
File: pages/index.vue
<template>
<h1>Beranda</h1>
</template>
File: pages/about.vue
<template>
<h1>Tentang Kami</h1>
</template>
Halaman otomatis tersedia di URL /
dan /about
.
๐ฆ 5. SSR, SSG, dan SPAโ
Mode | Keterangan |
---|---|
SSR | Server-rendered per request. SEO-friendly & real-time |
SSG | Static site. Build sekali โ cepat & ringan |
SPA | Seperti Vue biasa, tanpa SSR |
Konfigurasi di nuxt.config.ts
export default defineNuxtConfig({
ssr: true, // false = SPA, true = SSR/SSG
});
๐ค 6. Fetching Data (useFetch)โ
<script setup>
const { data, error } = await useFetch('/api/posts');
</script>
<template>
<div v-if="data">
<h2>Artikel:</h2>
<ul>
<li v-for="post in data" :key="post.id">{{ post.title }}</li>
</ul>
</div>
</template>
๐ ๏ธ 7. Konfigurasi Nuxt (nuxt.config.ts)โ
export default defineNuxtConfig({
modules: ['@nuxtjs/tailwindcss', '@pinia/nuxt'],
app: {
head: {
title: 'Nuxt SSR Vue App',
meta: [
{ name: 'description', content: 'Website menggunakan Nuxt.js dan SSR' }
]
}
}
});
๐งช 8. Deployment SSRโ
Nuxt SSR bisa dideploy ke:
-
Node.js Server (Express, Koa)
-
Vercel (SSR mode)
-
Netlify (untuk SSG mode)
-
VPS / Docker
Untuk build SSR:
npm run build
npm run start
๐จ 9. Tailwind + Pinia + TypeScriptโ
Semua bisa digabung dengan mudah:
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init
npm install pinia
Pinia otomatis terhubung jika menggunakan @pinia/nuxt
.