321 words
2 minutes
Next.js - Why It's My Go-To Framework

Next.js - My Go-To Framework#

If I’m building anything serious, it’s probably Next.js.

Why Next.js?#

1. File-Based Routing#

pages/
├── index.js → /
├── about.js → /about
├── blog/
│ ├── index.js → /blog
│ └── [slug].js → /blog/:slug
└── api/
└── hello.js → /api/hello

No router configuration needed. Create file = Create route. Magic.

2. API Routes#

Full backend in the same project:

pages/api/user.js
export default async function handler(req, res) {
if (req.method === 'POST') {
const user = await createUser(req.body);
return res.status(201).json(user);
}
const users = await getUsers();
res.status(200).json(users);
}

3. Multiple Rendering Strategies#

// Static Site Generation (SSG)
export async function getStaticProps() {
const posts = await getPosts();
return { props: { posts } };
}
// Server-Side Rendering (SSR)
export async function getServerSideProps() {
const data = await fetchData();
return { props: { data } };
}
// Incremental Static Regeneration (ISR)
export async function getStaticProps() {
return {
props: { ... },
revalidate: 60 // Rebuild every 60 seconds
};
}

4. Image Optimization#

import Image from 'next/image';
<Image
src="/profile.jpg"
alt="LUKKID"
width={200}
height={200}
priority
/>

Automatic lazy loading, WebP conversion, and responsive images!

5. Built-in CSS Support#

// CSS Modules
import styles from './Button.module.css';
// Tailwind works perfectly
<div className="bg-blue-500 text-white p-4">

App Router (Next.js 13+)#

The new app/ directory is awesome:

app/
├── page.js → /
├── layout.js → Shared layout
├── loading.js → Loading UI
├── error.js → Error handling
└── blog/
└── [slug]/
└── page.js → /blog/:slug

Server Components#

// This runs on the server!
async function BlogPosts() {
const posts = await db.posts.findMany();
return (
<ul>
{posts.map(post => (
<li key={post.id}>{post.title}</li>
))}
</ul>
);
}

My Next.js Project Structure#

src/
├── app/
│ ├── (auth)/
│ │ ├── login/
│ │ └── register/
│ ├── dashboard/
│ └── api/
├── components/
├── lib/
├── hooks/
└── styles/

Deploy in Seconds#

Terminal window
# With Vercel
npx vercel
# That's it. Production ready.

When NOT to Use Next.js#

  • Simple static sites → Astro might be lighter
  • React learning → Start with Vite
  • Non-React teams → Consider alternatives

Next.js is powerful. Learn it well!

Next.js - Why It's My Go-To Framework
https://blog.lukkid.dev/posts/nextjs-framework/
Author
LUKKID
Published at
2024-06-10
License
CC BY-NC-SA 4.0