Loading content…
Loading content…
Learn Next.js fundamentals and why it's essential for production React apps
Senior Developer Wisdom
app/
page.tsx → /
about/
page.tsx → /about
blog/
page.tsx → /blog
[slug]/
page.tsx → /blog/:slug
// app/page.tsx
export default function Home() {
return <h1>Welcome to Next.js</h1>;
}
// app/about/page.tsx
export default function About() {
return <h1>About Us</h1>;
}
// app/blog/[slug]/page.tsx
interface Props {
params: { slug: string };
}
export default function BlogPost({ params }: Props) {
return <h1>Post: {params.slug}</h1>;
}
// Generate static pages at build time
export async function generateStaticParams() {
return [
{ slug: "first-post" },
{ slug: "second-post" }
];
}
// app/server-component.tsx
export default async function ServerComponent() {
const data = await fetch("/api/data");
return <div>{data}</div>;
}
"use client"; // Mark as client component
import { useState } from "react";
export default function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>+</button>
</div>
);
}
Pro Tip
// Cached, generated at build time
export const revalidate = 3600; // Revalidate every hour
export default async function Page() {
const data = await fetch("/api/data");
return <div>{data}</div>;
}
// Generated on every request
export const dynamic = "force-dynamic";
export default async function Page() {
const data = await fetch("/api/data", {
cache: "no-store"
});
return <div>{data}</div>;
}
// Static but revalidate periodically
export const revalidate = 60; // Revalidate every 60 seconds
export default async function Page() {
const data = await fetch("/api/data");
return <div>{data}</div>;
}
Common Pitfall
import Image from "next/image";
export default function Page() {
return (
<Image
src="/image.png"
alt="Description"
width={800}
height={600}
priority // Load immediately
/>
);
}
// app/layout.tsx (root layout)
export default function RootLayout({
children
}: {
children: React.ReactNode
}) {
return (
<html>
<body>
<header>Navigation</header>
<main>{children}</main>
<footer>Footer</footer>
</body>
</html>
);
}
// app/blog/layout.tsx (blog-specific layout)
export default function BlogLayout({ children }) {
return (
<div className="blog-layout">
<aside>Categories</aside>
<main>{children}</main>
</div>
);
}
Next.js Fundamentals
Marking it complete updates your roadmap progress percentage.