This content originally appeared on DEV Community and was authored by Margish Patel
Welcome back developers! Its a chapter-2 of mastering the next.js 13
After your exciting first chapter ride, you’ve now hopped onto the
Next.js 13
train. Buckle up because the next stop is one of the most game-changing features: the app/
directory. Think of it as that friend who’s really good at organising trips. With the app/ directory by your side, routing and navigation just became a whole lot easier.
A New Era of Routing
In the past, routing in Next.js was like following an old treasure map —possible, but with a few “why-is-this-not-working” moments. Next.js 13 flips the script! With the
app/
directory, you’re no longer wandering in the dark.
Here’s why the app/
directory is your new best friend:
- File-Based Routing
: Each folder in the
app/
directory corresponds to a URL route. No need to define routes separately anymore—create a file, and boom, you have a route.
- Layouts, Pages, and Components
: The
app/
directory splits things into manageable pieces, like LEGO bricks. Want a universal layout? Done. Individual page structure? Easy. Component-based? You bet.
Now let’s see it in action with a simple project example.
Getting Started with the app/
Directory: A Simple Project
Let’s create a basic setup for a Next.js 13 project using the app/
directory. It’s so simple, it’s almost ridiculous how smooth it feels.
1.Create a new Next.js project:
npx create-next-app@latest nextjs-app-dir
cd nextjs-app-dir
2.Navigate to the app/
directory (it’s pre-built!):
You’ll notice a shiny new app/
directory sitting there waiting for you. Open it up and start making magic.
3.Create your first page in the app/
directory:
Inside app/
, create a new folder named about and within that, a file page.tsx
(this file represents your “About” page):
// app/about/page.tsx
export default function AboutPage() {
return (
<main>
<h1>Welcome to the About Page</h1>
<p>We're thrilled to have you here learning Next.js 13!</p>
</main>
);
}
4.Run your project:
npm run dev
5.Visit http://localhost:3000/about
to see your new page live:
Just like that, you’ve created an “About” page. No messy routes, no headaches—just simple, intuitive file-based routing.
Layouts in the app/ Directory: Consistency is Key
Okay, creating pages is easy, but what if you want the same layout across multiple pages? Maybe a consistent header or footer? The app/
directory has you covered.
1.Create a layout for all your pages:
Inside the app/
directory, create a new file called layout.tsx
:
// app/layout.tsx
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body>
<header>
<h1>My Awesome Next.js 13 Site</h1>
</header>
<main>{children}</main>
<footer>
<p>© 2024 My Next.js App</p>
</footer>
</body>
</html>
);
}
2.Now every page will share this layout — your “About” page just got a facelift! All pages inside the
app/
directory will now include this header and footer by default.
What’s the Point of Layouts?
Layouts in Next.js 13
give you ultimate control. You can create nested layouts for different sections of your site. Need a different layout for your blog? Easy! Want to keep the same footer across your entire app? Done!
Think of layouts like building blocks — they let you structure your app in a way that makes sense without duplicating code across different pages.
Nested Layouts: Let’s Get Fancy
Let’s say you want a special layout for your “About” page:
1.Inside the app/about/
folder, create a layout.tsx
file:
// app/about/layout.tsx
export default function AboutLayout({ children }: { children: React.ReactNode }) {
return (
<>
<h2>About Us</h2>
<section>{children}</section>
</>
);
}
Now, when you visit /about
, the new layout will be applied only to that section of your site. Cool, right?
Wrapping It Up: The Power of the app/
Directory
With the app/
directory, you’re not just getting easier routing—you’re getting a smoother development experience overall. Here’s a quick recap of the benefits:
- Cleaner, Simpler Routing: No need for complicated route management.
- Layouts on Steroids: Global and nested layouts let you organize your app like a pro.
- File-Based Navigation: Folders and files = routes. That’s it.
Your Next.js 13 Adventure Awaits
You’re now well on your way to mastering Next.js 13
, one chapter at a time. But this is just the beginning! You’ve unlocked the power of the app/
directory, and you’re already on track to create scalable, maintainable applications with ease.
In Chapter 3, we’ll dive into File/Folder based routing and best practices
and how they make your code maintainable and understandable. Until then, keep coding, and may your layouts always be in perfect harmony!
Happy coding forks
This content originally appeared on DEV Community and was authored by Margish Patel