Mastering Next.js: The Ultimate Guide to Structuring Large-Scale Projects in 2024
Vishal Yadav

Vishal Yadav @vyan

About: I Post Daily Blogs About Full-Stack Development

Location:
India
Joined:
May 3, 2024

Mastering Next.js: The Ultimate Guide to Structuring Large-Scale Projects in 2024

Publish Date: Aug 2 '24
515 58

Introduction: Taming the Next.js Jungle

Hey there, code wranglers and Next.js enthusiasts! 👋 Are you feeling like Indiana Jones, hacking through a dense jungle of components, hooks, and config files? Don't worry, you're not alone in this adventure. I've been there, machete in hand, trying to carve a path through the wilderness of a large-scale Next.js project.

But here's the thing: with the right map and tools, your Next.js jungle can become a well-organized, thriving ecosystem. In this comprehensive guide, I'll share my hard-earned wisdom on structuring large-scale Next.js projects. Whether you're scaling up an existing app or starting a new behemoth from scratch, this guide is your trusty compass.

Why Your Next.js Project Structure Can Make or Break You

Before we dive into the nitty-gritty, let's talk about why spending time on your project structure is like investing in a good pair of coding shoes – it'll take you far and keep you comfortable:

  1. Developer Sanity: A good structure means less time playing "Where's Waldo?" with your components and more time actually coding.
  2. Team Harmony: When your team can navigate the project blindfolded, collaboration becomes a breeze, not a battle.
  3. Scalability: A well-structured project grows organically, like a happy plant, instead of mutating into a code monster.
  4. Performance Boost: Next.js optimization features work best when your project is organized logically.
  5. Maintainability: Future you (or the poor soul who inherits your project) will be eternally grateful for a clean, intuitive structure.

The Next.js Project Structure That'll Make You Want to Frame It

Alright, drum roll, please! 🥁 Here's a structure that's been battle-tested in the trenches of large-scale Next.js development:

📁 my-awesome-nextjs-project
|
|_ 📁 app
|  |_ 📁 (auth)
|  |  |_ 📁 login
|  |  |  |_ 📄 page.tsx
|  |  |  |_ 📄 layout.tsx
|  |  |_ 📁 register
|  |     |_ 📄 page.tsx
|  |     |_ 📄 layout.tsx
|  |_ 📁 dashboard
|  |  |_ 📄 page.tsx
|  |  |_ 📄 layout.tsx
|  |_ 📁 api
|  |  |_ 📁 users
|  |  |  |_ 📄 route.ts
|  |  |_ 📁 posts
|  |     |_ 📄 route.ts
|  |_ 📄 layout.tsx
|  |_ 📄 page.tsx
|
|_ 📁 components
|  |_ 📁 ui
|  |  |_ 📄 Button.tsx
|  |  |_ 📄 Card.tsx
|  |  |_ 📄 Modal.tsx
|  |_ 📁 forms
|  |  |_ 📄 LoginForm.tsx
|  |  |_ 📄 RegisterForm.tsx
|  |_ 📁 layouts
|     |_ 📄 Header.tsx
|     |_ 📄 Footer.tsx
|     |_ 📄 Sidebar.tsx
|
|_ 📁 lib
|  |_ 📄 api.ts
|  |_ 📄 utils.ts
|  |_ 📄 constants.ts
|
|_ 📁 hooks
|  |_ 📄 useUser.ts
|  |_ 📄 useAuth.ts
|  |_ 📄 usePosts.ts
|
|_ 📁 types
|  |_ 📄 user.ts
|  |_ 📄 post.ts
|  |_ 📄 api.ts
|
|_ 📁 styles
|  |_ 📄 globals.css
|  |_ 📄 variables.css
|
|_ 📁 public
|  |_ 📁 images
|  |  |_ 📄 logo.svg
|  |  |_ 📄 hero-image.png
|  |_ 📁 fonts
|     |_ 📄 custom-font.woff2
|
|_ 📁 config
|  |_ 📄 seo.ts
|  |_ 📄 navigation.ts
|
|_ 📄 next.config.js
|_ 📄 package.json
|_ 📄 tsconfig.json
|_ 📄 .env.local
|_ 📄 .gitignore
Enter fullscreen mode Exit fullscreen mode

Now, let's break this down and see why each piece is crucial to your Next.js masterpiece.

The Heart of Your Next.js App: The app Directory

The app directory is where the magic happens. It's the core of your Next.js 13+ project, leveraging the new App Router:

📁 app
|_ 📁 (auth)
|  |_ 📁 login
|  |_ 📁 register
|_ 📁 dashboard
|_ 📁 api
|_ 📄 layout.tsx
|_ 📄 page.tsx
Enter fullscreen mode Exit fullscreen mode

Route Grouping with (auth)

The (auth) folder is a clever way to group related routes without affecting the URL structure. It's perfect for organizing authentication-related pages.

// app/(auth)/login/page.tsx
export default function LoginPage() {
  return <h1>Welcome to the Login Page</h1>;
}
Enter fullscreen mode Exit fullscreen mode

API Routes: Your Backend in Disguise

Keep your backend logic tidy in the api directory. Each file becomes an API route:

// app/api/users/route.ts
import { NextResponse } from 'next/server';

export async function GET() {
  // Fetch users logic
  return NextResponse.json({ users: ['Alice', 'Bob'] });
}
Enter fullscreen mode Exit fullscreen mode

Layouts and Pages: The Building Blocks of Your UI

Use layout.tsx to create consistent designs across pages:

// app/layout.tsx
export default function RootLayout({ children }) {
  return (
    <html lang="en">
      <body>{children}</body>
    </html>
  );
}
Enter fullscreen mode Exit fullscreen mode

Each page.tsx represents a unique route in your application:

// app/page.tsx
export default function HomePage() {
  return <h1>Welcome to our awesome Next.js app!</h1>;
}
Enter fullscreen mode Exit fullscreen mode

Components: Your Next.js LEGO Set

Think of components as LEGO bricks. Organized well, they're easy to find and fun to use:

📁 components
|_ 📁 ui
|_ 📁 forms
|_ 📁 layouts
Enter fullscreen mode Exit fullscreen mode

UI Components: The Building Blocks

Create reusable UI elements that maintain consistency across your app:

// components/ui/Button.tsx
export default function Button({ children, onClick }) {
  return (
    <button onClick={onClick} className="bg-blue-500 text-white py-2 px-4 rounded">
      {children}
    </button>
  );
}
Enter fullscreen mode Exit fullscreen mode

Form Components: Making Data Entry a Breeze

Encapsulate form logic for cleaner, more maintainable code:

// components/forms/LoginForm.tsx
import { useState } from 'react';
import Button from '../ui/Button';

export default function LoginForm({ onSubmit }) {
  const [email, setEmail] = useState('');
  const [password, setPassword] = useState('');

  return (
    <form onSubmit={(e) => {
      e.preventDefault();
      onSubmit(email, password);
    }}>
      <input type="email" value={email} onChange={(e) => setEmail(e.target.value)} />
      <input type="password" value={password} onChange={(e) => setPassword(e.target.value)} />
      <Button type="submit">Log In</Button>
    </form>
  );
}
Enter fullscreen mode Exit fullscreen mode

Layout Components: The Framework of Your UI

Create consistent page structures with reusable layout components:

// components/layouts/Header.tsx
import Link from 'next/link';

export default function Header() {
  return (
    <header>
      <nav>
        <Link href="/">Home</Link>
        <Link href="/dashboard">Dashboard</Link>
        <Link href="/profile">Profile</Link>
      </nav>
    </header>
  );
}
Enter fullscreen mode Exit fullscreen mode

The Supporting Cast: lib, hooks, and types

These directories are the unsung heroes of your project:

lib: Your Utility Belt

Store helper functions and constants here:

// lib/utils.ts
export function formatDate(date: Date): string {
  return date.toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' });
}

// lib/constants.ts
export const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || 'https://api.example.com';
Enter fullscreen mode Exit fullscreen mode

hooks: Custom React Superpowers

Create custom hooks to encapsulate complex logic:

// hooks/useUser.ts
import { useState, useEffect } from 'react';
import { fetchUser } from '../lib/api';

export function useUser(userId: string) {
  const [user, setUser] = useState(null);
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    fetchUser(userId).then(userData => {
      setUser(userData);
      setLoading(false);
    });
  }, [userId]);

  return { user, loading };
}
Enter fullscreen mode Exit fullscreen mode

types: TypeScript's Best Friend

Define your TypeScript interfaces and types:

// types/user.ts
export interface User {
  id: string;
  name: string;
  email: string;
  role: 'admin' | 'user';
}

// types/post.ts
export interface Post {
  id: string;
  title: string;
  content: string;
  authorId: string;
  createdAt: Date;
}
Enter fullscreen mode Exit fullscreen mode

Styling Your Next.js Masterpiece

Keep your styles organized in the styles directory:

/* styles/globals.css */
@tailwind base;
@tailwind components;
@tailwind utilities;

/* Your custom global styles here */
body {
  font-family: 'Arial', sans-serif;
}

/* styles/variables.css */
:root {
  --primary-color: #3490dc;
  --secondary-color: #ffed4a;
  --text-color: #333333;
}
Enter fullscreen mode Exit fullscreen mode

Public Assets: The Face of Your App

The public directory is home to your static assets. Optimize images and use custom fonts to make your app shine:

import Image from 'next/image';

export default function Logo() {
  return <Image src="/images/logo.svg" alt="Company Logo" width={200} height={50} />;
}
Enter fullscreen mode Exit fullscreen mode

Configuration: The Backbone of Your Project

Don't forget about these crucial files in your root directory:

// next.config.js
module.exports = {
  images: {
    domains: ['example.com'],
  },
  // Other Next.js config options
};

// .env.local
DATABASE_URL=postgresql://username:password@localhost:5432/mydb
NEXT_PUBLIC_API_URL=https://api.example.com
Enter fullscreen mode Exit fullscreen mode

Pro Tips for Large-Scale Next.js Success

  1. Embrace the App Router: It's not just new; it's a game-changer for performance and nested layouts.
  2. Code Splitting is Your Friend: Use dynamic imports to keep your app snappy:
   import dynamic from 'next/dynamic';

   const DynamicComponent = dynamic(() => import('../components/HeavyComponent'));
Enter fullscreen mode Exit fullscreen mode
  1. Optimize Those Images: Next.js's Image component is like a personal trainer for your images:
   import Image from 'next/image';

   export default function Hero() {
     return <Image src="/hero-image.png" alt="Hero" width={1200} height={600} priority />;
   }
Enter fullscreen mode Exit fullscreen mode
  1. Server Components FTW: Use them to slash your client-side JavaScript:
   // This component will be rendered on the server by default in Next.js 13+
   export default async function UserProfile({ userId }) {
     const user = await fetchUser(userId);
     return <div>Welcome, {user.name}!</div>;
   }
Enter fullscreen mode Exit fullscreen mode
  1. API Routes for the Win: Keep your server-side logic secure and separated:
   // pages/api/posts.ts
   import type { NextApiRequest, NextApiResponse } from 'next';

   export default async function handler(req: NextApiRequest, res: NextApiResponse) {
     if (req.method === 'GET') {
       const posts = await fetchPosts();
       res.status(200).json(posts);
     } else {
       res.status(405).end(); // Method Not Allowed
     }
   }
Enter fullscreen mode Exit fullscreen mode

Wrapping Up: Your Next.js Project, Organized and Ready to Scale

There you have it – a structure that'll make your large-scale Next.js project feel like a well-oiled machine. Remember, this isn't a one-size-fits-all solution. Feel free to tweak it to fit your project's unique needs.

By following this structure, you'll spend less time scratching your head over where things go and more time building awesome features. Your code will be cleaner, your team will be happier, and your project will scale like a dream.

So, what are you waiting for? Give this structure a spin in your next project. Your future self (and your teammates) will high-five you for it!

Happy coding, and may your Next.js projects always be organized and bug-free! 🚀


Remember, the key to a successful large-scale Next.js project isn't just in the initial setup – it's in how you maintain and evolve your structure as your project grows. Stay flexible, keep learning, and don't be afraid to refactor when needed. You've got this!

Comments 58 total

  • Voyagergle
    VoyagergleAug 2, 2024

    oh no, is this the end?
    but i still give a sponsor

  • abbatyya
    abbatyyaAug 2, 2024

    Useful article, thanks

  • DHARSAN R
    DHARSAN RAug 2, 2024

    Very useful post keep sharing more like this 👏👏

  • Renterra
    RenterraAug 2, 2024

    Good post!

  • Emmanuel Awabil
    Emmanuel AwabilAug 2, 2024

    I love this

  • mohamed karim
    mohamed karimAug 2, 2024

    Thank for sharing

  • Bijay Das
    Bijay DasAug 3, 2024

    hey!!, this is exactly my structure

  • Vishal Yadav
    Vishal YadavAug 3, 2024

    Thanks!

  • Zakharov Vladyslav
    Zakharov VladyslavAug 3, 2024

    Where are components styles? I have a structure

    components/
    |__ component-name/
    |_____ component-name.module.scss
    |_____ index.tsx
    |__ index.ts

    in index.ts:

    export * from ‘./component-name’

    so after that I can import components like

    import { ComponentName } from ‘@/components’;

    • Prince Muel
      Prince MuelAug 4, 2024

      Barrel exports are easier to make but can increase your code import size. Also, they can leak client component code into server components and vice versa.

      • Zakharov Vladyslav
        Zakharov VladyslavAug 4, 2024

        Thanks a lot for this. Never knew and never met explanations for it

    • Mohammad Mahdi Bahrami
      Mohammad Mahdi BahramiAug 7, 2024

      I do it the same way! 😅

  • Anze
    AnzeAug 3, 2024

    Hey, what are the pros of dynamic importe i.e code splitting?

    • Nikolai Lehbrink
      Nikolai LehbrinkAug 4, 2024

      Imagine you have a <Dialog> component that heavily relies on JavaScript. If this component is loaded with the initial page render, it increases the initial bundle size unnecessarily, especially if the user never opens the dialog. With dynamic imports, such as in Next.js 13+ using const Dialog = dynamic(() => import('../components/Dialog')), you only load the component when it's needed. This means the JavaScript for the <Dialog> component is only loaded when the button to open it is clicked. Hope this helps. You can also take a look at the reference in the Next.js docs.

  • mohamed karim
    mohamed karimAug 3, 2024

    Thank for sharing

  • Sanwicklith MKALINGA
    Sanwicklith MKALINGAAug 3, 2024

    Am planning to delve into next.js and I must say this article has really convinced me and given me great perspective about the framework.

    Thanks

  • Sheraz Manzoor
    Sheraz ManzoorAug 3, 2024

    Great written boy. I am Senior Software Engineer and I appreciate this structure. One minor thing I will suggest to improve, the components is a different thing and forms is a different.

    For forms and big layouts, which are route related (means which are for a specific page) they should kept in modules directory..

    And only reusable components should kept in components directory. That it. I just wanted to see how others structure this and what are you teaching to other.

    With that minor improvement, I suggest all the juniors to follow this structure!!!

  • Wesley Chong
    Wesley ChongAug 3, 2024

    Cool

  • Ashok Kumar
    Ashok KumarAug 4, 2024

    Hi I need nextjs with swagger-autogen 3.0

    It's works but not able to get dynamic routes each endpoints of the top we need to add some swagger schema why

    I don't want like to add schema like swagger 2.0

    If any one solve it please add a blog and tag @ashok2406

  • Abdelrahman Mohamed
    Abdelrahman MohamedAug 4, 2024

    Berfact ✌️
    Keep going explain more

  • ugener
    ugenerAug 4, 2024

    thanks for sharing!

  • Nikolai Lehbrink
    Nikolai LehbrinkAug 4, 2024

    Nice article, but I think you should update the API Routes for the Win section @vyan, because that's not an API Route from Next.js 13+ :) It is more like:

    // app/api/posts/route.ts
    import { NextResponse } from 'next/server';
    
    export async function GET() {
      const posts = await fetchPosts();
      return NextResponse.json(posts);
    }
    
    Enter fullscreen mode Exit fullscreen mode
  • Boby Tiwari
    Boby TiwariAug 4, 2024

    Nice

  • Sumonta Saha Mridul
    Sumonta Saha MridulAug 4, 2024

    @vyan any GitHub link available? so that I can explore the structure more?

  • ANICHUKWU ISRAEL
    ANICHUKWU ISRAELAug 4, 2024

    Thank you for the knowledge.
    I will add it to my project development

  • Ashish Prajapati
    Ashish PrajapatiAug 4, 2024

    Such a good content. Mine is also matching but learned a few things from you.

  • developerkaushalkishor
    developerkaushalkishorAug 4, 2024

    I love to learn from you,after seeing this article

  • Codefolder
    CodefolderAug 4, 2024

    Codefolder best coding platform

  • Gomez
    GomezAug 4, 2024

    Thank you found this very helpful

  • Ogbole Divine
    Ogbole DivineAug 5, 2024

    All these folders do they have extra price of coding within them?
    If yes how do u write those and embed them on ur Next.js screen.....

    Secondly can I use vs.code to work on a next.js app?
    Can I also write them in pseudocode?

  • Michael Steven David
    Michael Steven DavidAug 5, 2024

    Where would you likely put server action files? Would it be inside its own folder within the root folder or inside the lib?

  • Vitaliy Potapov
    Vitaliy PotapovAug 5, 2024

    Regarding to auth, we have an opposite structure - keep most pages in (authorized) dir and only login/register on the root level. This is because for all authorized pages we have a header in layout, but it is not shown on login/register pages:

    |_ 📁 app
    |  |_ 📁 (authorized)
    |  |  |_ 📁 dashboard
    |  |  |  |_ 📄 page.tsx
    |  |  |_ 📄 layout.tsx
    |  |_ 📁 login
    |  |  |  |_ 📄 page.tsx
    |  |  |  |_ 📄 layout.tsx
    
    Enter fullscreen mode Exit fullscreen mode

    Could you advice how can we achieve the same with (auth) dir?

  • rmfloris
    rmflorisAug 5, 2024

    Where/how would you add tests/mocks to the structure?

  • Chris Leo
    Chris LeoAug 5, 2024

    Wow I woul not wait to use it lately thanks for this awesome advice

  • Rakesh Potnuru
    Rakesh PotnuruAug 5, 2024

    Nice! This is close to how I structure my Next.js apps.

  • Rehan Arif
    Rehan ArifAug 5, 2024

    "In the constantly changing field of web development.", the pursuit of speed, efficiency, and simplicity is ceaseless, much like the advances in AI-powered tools explored in our GitHub Copilot vs Chatbot article. Next, js 14, Vercel's latest innovation, has arrived to redefine the way we construct web applications. Equipped with an array of enhancements, including the groundbreaking TurboPack, robust Server Actions, and the eagerly anticipated Partial Pre-Rendering, Next js 14 is set to transform your web development experience, Read more at webzeto.com

  • Skiy News (Mainul Haque)
    Skiy News (Mainul Haque)Aug 5, 2024

    📱 iPhone 15 Pro Max Giveaway! 🎉

    📱 iPhone 15 Pro Max Giveaway! 🎉
    Certainly! Here is a template for an iPhone 15 Pro Max giveaway that you can use for your marketing campaign:
    📱 iPhone 15 Pro Max Giveaway! 🎉
    Enter for a chance to win the latest iPhone 15 Pro Max!
    We are excited to announce our iPhone 15 Pro Max Giveaway! Follow the steps below for your chance to win this incredible new device!
    How to Enter:
    Follow Us on [Instagram/Facebook/Twitter/Your Preferred Social Media Platform].
    Like this post.
    Tag 3 friends in the comments below.
    Share this post to your story/feed and tag us (@YourHandle).
    Bonus Entry:
    Subscribe to our newsletter at [Your Website URL].
    Giveaway Rules:
    The giveaway is open to residents of [Eligible Countries].
    Participants must be [Age Requirement] or older.
    Entries must be submitted by [Deadline Date & Time].
    The winner will be chosen randomly and announced on [Announcement Date] via
    our social media channels.
    Terms and Conditions:
    This giveaway is not sponsored, endorsed, or administered by, or associated with Instagram/Facebook/Twitter.
    The prize is non-transferable and no cash alternative will be offered.
    We reserve the right to disqualify any entries that do not comply with the rules.
    Date: 06-08-2024
    Good Luck!](sites.google.com/view/skiynews/home)
    Thank you for participating and supporting [Your Brand/Business Name]. Stay tuned for more exciting updates and giveaways! .......... See More

  • Skiy News (Mainul Haque)
    Skiy News (Mainul Haque)Aug 5, 2024

    Hi, I am Md Mainul Haque CEO, Translate Media Home.
    A Professional digital marketer and language translator expert.
    Contact me with details and I can provide you any kind of digital marketing services. Especially I do language translation work through which I can easily translate any other language into my own language or any other language with accurate information and correct grammar always. Through which you can convert the desired words or sentences into your own language. In addition to this, Adobe Photoshop, Adobe Illustrator - drawing art work,

  • Ramiro Wunsch
    Ramiro WunschAug 5, 2024

    Get $750 on your Cash App Instantly. Claim the link in the event!
    ↪️Collect here:
    ★Limited time offer
    ★Online payment available
    ★Read carefully all requirements for better chance to win
    ★More details and the application form are here.

    Claim Now..

  • Touchumind
    TouchumindAug 6, 2024

    And another tip or maybe personal preference: use named exports for your components. This makes sense for code intellisense.

  • Nazmul Hasan
    Nazmul HasanAug 6, 2024

    🎉 iPhone 15 Pro Max Giveaway! 🎉

    Image description
    ## 🎉 iPhone 15 Pro Max Giveaway! 🎉

    ### Win the Latest iPhone 15 Pro Max!

    We're giving away an iPhone 15 Pro Max to one lucky winner! Follow these simple steps to enter:

    ### How to Enter:

    Follow us on [Facebook]: Nazmul Hasan
    Like this post and share it on your story. Don't forget to tag us!
    Tag 3 friends in the comments below.
    Bonus Entry: Share why you love the iPhone 15 Pro Max in the comments.
    ### Giveaway Rules:

    Eligibility: Open to residents of [Any/Any].
    Age Requirement: Participants must be [No limit] or older.
    Entry Deadline: All entries must be submitted by [06/08/2024& 10.00AM].
    Winner Announcement: The winner will be announced on [08/8/2024] via [Social Media Platform].
    Terms & Conditions:
    This giveaway is not sponsored, endorsed, or administered by, or associated with Instagram/Facebook/Twitter.
    The winner will be selected randomly and contacted via direct message.
    If the winner does not respond within 48 hours, a new winner will be chosen.
    Good Luck! 🍀
    Additional Tips:
    Visuals: Use high-quality images of the iPhone 15 Pro Max.
    Hashtags: Include relevant hashtags to increase reach (#iPhone15Giveaway, #TechGiveaway, #@Nazmul1984).
    Engagement: Encourage participants to engage with your brand for a higher chance of winning.
    Legal Compliance: Ensure your giveaway complies with local laws and platform rules.
    [Win iphone 15 pro max]

  • lwooden
    lwoodenAug 6, 2024

    Great article! I did notice that you omitted an “actions” folder under lib, but this is probably because you are using /api as a backend instead of SAs. Additionally, you have reference components that are using client side hooks but you didn’t annotate the file like so: “use client”

  • Barack Okaka Obama
    Barack Okaka ObamaAug 7, 2024

    Next is a very clean language.

    • Thomas TS
      Thomas TSAug 7, 2024

      NextJS has JS in the name because is Javascript. And Javascript is a language.

      Why bother with a Microsoft (evil) tool as Typescript if we have JSDoc?

  • Alberts Kresi
    Alberts KresiAug 7, 2024

    Great

  • Jam Straw
    Jam StrawAug 8, 2024

    Fantastic article.

  • Akash
    AkashAug 8, 2024

    👍👍👍👍👍

  • Hadi Pribadi
    Hadi PribadiSep 22, 2024

    Thanks very insightful. Do you have an example in Github?

  • Artean
    ArteanOct 18, 2024

    Useful thanks

Add comment