NEXTJS-PAGES-ROUTER DOCS
ROUTING-PAGES
Name
Description
Example
Output
pages/index.tsxDescription: Halaman utama (/) di folder pages.
Example:
// pages/index.tsx
export default function Home() { return <h1>Home</h1> }Output: localhost:3000/
pages/index.tsxHalaman utama (/) di folder pages.
// pages/index.tsx
export default function Home() { return <h1>Home</h1> }localhost:3000/
pages/about.tsxDescription: Halaman statis dengan nama file sebagai rute.
Example:
// pages/about.tsx
export default function About() { return <h1>About</h1> }Output: /about
pages/about.tsxHalaman statis dengan nama file sebagai rute.
// pages/about.tsx
export default function About() { return <h1>About</h1> }/about
pages/[slug].tsxDescription: Rute dinamis dengan parameter.
Example:
// pages/posts/[id].tsx
export default function Post({ id }) { return <p>Post {id}</p> }Output: /posts/123
pages/[slug].tsxRute dinamis dengan parameter.
// pages/posts/[id].tsx
export default function Post({ id }) { return <p>Post {id}</p> }/posts/123
pages/[...slug].tsxDescription: Catch-all route.
Example:
// pages/docs/[...params].tsx
export default function Docs({ params }) { return <p>{params.join('/')}</p> }Output: /docs/a/b/c
pages/[...slug].tsxCatch-all route.
// pages/docs/[...params].tsx
export default function Docs({ params }) { return <p>{params.join('/')}</p> }/docs/a/b/c
pages/api/hello.tsDescription: API route di Pages Router.
Example:
// pages/api/hello.ts
export default function handler(req, res) { res.status(200).json({ name: 'John' }) }Output: /api/hello
pages/api/hello.tsAPI route di Pages Router.
// pages/api/hello.ts
export default function handler(req, res) { res.status(200).json({ name: 'John' }) }/api/hello
DATA-FETCHING
Name
Description
Example
Output
getServerSidePropsDescription: Fetch data setiap request (SSR).
Example:
export async function getServerSideProps() { const data = await fetch(...); return { props: { data } } }Output: Props di halaman
getServerSidePropsFetch data setiap request (SSR).
export async function getServerSideProps() { const data = await fetch(...); return { props: { data } } }Props di halaman
getStaticPropsDescription: Fetch data saat build time (SSG).
Example:
export async function getStaticProps() { const data = await fetch(...); return { props: { data }, revalidate: 60 } }Output: Props statik + ISR
getStaticPropsFetch data saat build time (SSG).
export async function getStaticProps() { const data = await fetch(...); return { props: { data }, revalidate: 60 } }Props statik + ISR
getStaticPathsDescription: Tentukan path dinamis yang akan di-generate static.
Example:
export async function getStaticPaths() { const posts = await fetch(...); const paths = posts.map(p => ({ params: { id: p.id } })); return { paths, fallback: false } }Output: Path static
getStaticPathsTentukan path dinamis yang akan di-generate static.
export async function getStaticPaths() { const posts = await fetch(...); const paths = posts.map(p => ({ params: { id: p.id } })); return { paths, fallback: false } }Path static
getInitialPropsDescription: Cara lama (bisa digunakan di _app). Masih ada tapi tidak disarankan.
Example:
Page.getInitialProps = async (ctx) => { return { data: '...' } }Output: Props awal
getInitialPropsCara lama (bisa digunakan di _app). Masih ada tapi tidak disarankan.
Page.getInitialProps = async (ctx) => { return { data: '...' } }Props awal
RENDERING
Name
Description
Example
Output
Server-Side Rendering (SSR)Description: Halaman dirender di server per request menggunakan getServerSideProps.
Example:
export async function getServerSideProps() { ... }Output: HTML segar per request
Server-Side Rendering (SSR)Halaman dirender di server per request menggunakan getServerSideProps.
export async function getServerSideProps() { ... }HTML segar per request
Static Site Generation (SSG)Description: Halaman di-generate saat build dengan getStaticProps.
Example:
export async function getStaticProps() { ... }Output: HTML statik
Static Site Generation (SSG)Halaman di-generate saat build dengan getStaticProps.
export async function getStaticProps() { ... }HTML statik
Incremental Static Regeneration (ISR)Description: Perbarui halaman statis di background setelah build.
Example:
return { props: { ... }, revalidate: 60 }Output: Diperbarui tiap 60 detik
Incremental Static Regeneration (ISR)Perbarui halaman statis di background setelah build.
return { props: { ... }, revalidate: 60 }Diperbarui tiap 60 detik
Client-Side Rendering (CSR)Description: Fetch data dari client menggunakan useEffect atau SWR.
Example:
useEffect(() => { fetch('/api/data').then(...) }, [])Output: Data muncul setelah load
Client-Side Rendering (CSR)Fetch data dari client menggunakan useEffect atau SWR.
useEffect(() => { fetch('/api/data').then(...) }, [])Data muncul setelah load
COMPONENTS
Name
Description
Example
Output
next/headDescription: Komponen untuk mengisi tag <head>.
Example:
import Head from 'next/head'; <Head><title>My Page</title></Head>
Output: Judul halaman
next/headKomponen untuk mengisi tag <head>.
import Head from 'next/head'; <Head><title>My Page</title></Head>
Judul halaman
next/imageDescription: Komponen gambar yang dioptimasi (wajib properti width/height).
Example:
import Image from 'next/image';
<Image src="/hero.jpg" width={800} height={600} alt="Hero" />Output: Gambar optimal
next/imageKomponen gambar yang dioptimasi (wajib properti width/height).
import Image from 'next/image';
<Image src="/hero.jpg" width={800} height={600} alt="Hero" />Gambar optimal
next/linkDescription: Navigasi client-side dengan prefetch otomatis.
Example:
import Link from 'next/link'; <Link href="/about">About</Link>
Output: Navigasi SPA
next/linkNavigasi client-side dengan prefetch otomatis.
import Link from 'next/link'; <Link href="/about">About</Link>
Navigasi SPA
next/scriptDescription: Memuat script eksternal dengan strategi tertentu.
Example:
import Script from 'next/script'; <Script src="https://example.com/script.js" strategy="lazyOnload" />
Output: Script tertunda
next/scriptMemuat script eksternal dengan strategi tertentu.
import Script from 'next/script'; <Script src="https://example.com/script.js" strategy="lazyOnload" />
Script tertunda
ROUTER
Name
Description
Example
Output
useRouterDescription: Hook untuk mengakses router (push, replace, query, dll).
Example:
import { useRouter } from 'next/router';
const router = useRouter();
router.push('/login')Output: Navigasi programatik
useRouterHook untuk mengakses router (push, replace, query, dll).
import { useRouter } from 'next/router';
const router = useRouter();
router.push('/login')Navigasi programatik
withRouterDescription: HOC untuk menyuntikkan router ke komponen (class component).
Example:
import { withRouter } from 'next/router';
function Page({ router }) { ... }
export default withRouter(Page);Output: Props router
withRouterHOC untuk menyuntikkan router ke komponen (class component).
import { withRouter } from 'next/router';
function Page({ router }) { ... }
export default withRouter(Page);Props router
router.queryDescription: Mendapatkan query string dari URL.
Example:
const { id } = router.query;Output: Parameter URL
router.queryMendapatkan query string dari URL.
const { id } = router.query;Parameter URL
API-ROUTES
Name
Description
Example
Output
req.methodDescription: Cek HTTP method di API route.
Example:
if (req.method === 'POST') { ... }Output: GET, POST, dll
req.methodCek HTTP method di API route.
if (req.method === 'POST') { ... }GET, POST, dll
req.queryDescription: Akses query string di API.
Example:
req.query.id
Output: Nilai query
req.queryAkses query string di API.
req.query.id
Nilai query
req.bodyDescription: Body request (sudah di-parse).
Example:
const { name } = req.body;Output: Data yang dikirim
req.bodyBody request (sudah di-parse).
const { name } = req.body;Data yang dikirim
res.status(code).json(data)Description: Kirim respons JSON dengan status code.
Example:
res.status(200).json({ ok: true })Output: Respons JSON
res.status(code).json(data)Kirim respons JSON dengan status code.
res.status(200).json({ ok: true })Respons JSON
CONFIGURATION
Name
Description
Example
Output
next.config.jsDescription: File konfigurasi Next.js.
Example:
module.exports = { reactStrictMode: true, swcMinify: true }Output: Konfigurasi diaktifkan
next.config.jsFile konfigurasi Next.js.
module.exports = { reactStrictMode: true, swcMinify: true }Konfigurasi diaktifkan
Environment VariablesDescription: Gunakan .env.local untuk menyimpan variabel.
Example:
NEXT_PUBLIC_API_URL=https://api.example.com
Output: process.env.NEXT_PUBLIC_API_URL
Environment VariablesGunakan .env.local untuk menyimpan variabel.
NEXT_PUBLIC_API_URL=https://api.example.com
process.env.NEXT_PUBLIC_API_URL
Custom App (`_app.tsx`)Description: Komponen pembungkus semua halaman.
Example:
// pages/_app.tsx
export default function MyApp({ Component, pageProps }) { return <Layout><Component {...pageProps} /></Layout> }Output: Layout global
Custom App (`_app.tsx`)Komponen pembungkus semua halaman.
// pages/_app.tsx
export default function MyApp({ Component, pageProps }) { return <Layout><Component {...pageProps} /></Layout> }Layout global
Custom Document (`_document.tsx`)Description: Kustomisasi struktur HTML (misal menambah lang, font).
Example:
// pages/_document.tsx
export default function Document() { return <Html lang="id">...</Html> }Output: Struktur HTML
Custom Document (`_document.tsx`)Kustomisasi struktur HTML (misal menambah lang, font).
// pages/_document.tsx
export default function Document() { return <Html lang="id">...</Html> }Struktur HTML
MIDDLEWARE
Name
Description
Example
Output
Middleware (Pages Router)Description: File `_middleware.ts` di dalam folder pages untuk menjalankan logika sebelum request.
Example:
// pages/_middleware.ts
export function middleware(req) { return NextResponse.next() }Output: Middleware berjalan
Middleware (Pages Router)File `_middleware.ts` di dalam folder pages untuk menjalankan logika sebelum request.
// pages/_middleware.ts
export function middleware(req) { return NextResponse.next() }Middleware berjalan
ERROR-HANDLING
Name
Description
Example
Output
pages/404.tsxDescription: Halaman kustom untuk 404.
Example:
// pages/404.tsx
export default function NotFound() { return <h1>404 - Not Found</h1> }Output: Halaman 404
pages/404.tsxHalaman kustom untuk 404.
// pages/404.tsx
export default function NotFound() { return <h1>404 - Not Found</h1> }Halaman 404
pages/_error.tsxDescription: Halaman error kustom untuk server/client error.
Example:
// pages/_error.tsx
export default function Error({ statusCode }) { return <p>{statusCode} error</p> }Output: Halaman error
pages/_error.tsxHalaman error kustom untuk server/client error.
// pages/_error.tsx
export default function Error({ statusCode }) { return <p>{statusCode} error</p> }Halaman error
getStaticProps fallbackDescription: fallback: true atau 'blocking' untuk halaman yang belum di-generate.
Example:
return { paths, fallback: 'blocking' }Output: Halaman di-generate on-demand
getStaticProps fallbackfallback: true atau 'blocking' untuk halaman yang belum di-generate.
return { paths, fallback: 'blocking' }Halaman di-generate on-demand