November 10, 2024
A practical guide to file-based routing, layouts, and server components in Next.js 14 and beyond.

On this page
What is the App Router?File conventionsServer vs. Client componentsLayouts and nestingNext stepsThe App Router is Next.js's modern routing system, introduced in Next.js 13 and stable in Next.js 14. It replaces the pages/ directory with an app/ directory and brings server components, nested layouts, and streaming as first-class primitives.
Every folder inside app/ can contain a page.tsx to become a route. Special files like layout.tsx, loading.tsx, and error.tsx handle their respective roles automatically.
app/
layout.tsx ← root layout (always rendered)
page.tsx ← /
blog/
layout.tsx ← layout wrapping all /blog/* routes
page.tsx ← /blog
[slug]/
page.tsx ← /blog/:slug
By default, every component in the app/ directory is a Server Component. They run on the server, can access databases directly, and never ship their code to the browser.
Add "use client" at the top of a file to opt into a Client Component — necessary for interactivity, hooks, and browser APIs.
// Server Component — no directive needed
export default async function BlogPage() {
const posts = await db.query.posts.findMany();
return <PostList posts={posts} />;
}"use client";
// Client Component — runs in the browser
export const LikeButton = ({ postId }: { postId: string }) => {
const [liked, setLiked] = useState(false);
return <button onClick={() => setLiked(true)}>Like</button>;
};Layouts persist across route changes without remounting. A layout.tsx wraps all child routes within its folder, making it ideal for navigation, sidebars, and persistent UI.
export default function BlogLayout({ children }: { children: React.ReactNode }) {
return (
<div className="container mx-auto py-10">
<BlogNav />
{children}
</div>
);
}Your laboratory instruments should serve you, not the other way around. We're happy to help you.