Understanding the Default File Structure
Understanding the Default File Structure
Section titled “Understanding the Default File Structure”Introduction
Section titled “Introduction”The Next.js project structure follows conventions that make development intuitive and predictable. Understanding each directory and file’s purpose is crucial for navigating and extending your application effectively.
Why do we need this?
Section titled “Why do we need this?”Without understanding the project structure, you might place files in incorrect locations, leading to confusion, unexpected behavior, or broken applications. Knowing where to put components, styles, and assets ensures your project remains maintainable.
Problem Statement
Section titled “Problem Statement”New developers often struggle to locate where to add new pages, components, or styles, resulting in time wasted searching through directories or putting files in the wrong place.
Real World Story
Section titled “Real World Story”You’re joining an existing Next.js project as a developer. During onboarding, you need to add a new feature. Understanding the project structure allows you to quickly locate where to add the new page, component, and styles without constantly asking teammates.
Real World Analogy
Section titled “Real World Analogy”Think of a Next.js project like a well-organized office:
- The
pages/directory is like the reception area where visitors (routes) are directed - The
components/directory is like the supply closet with reusable items - The
styles/directory is like the design team’s office - The
public/directory is like the public bulletin board
Visual Explanation
Section titled “Visual Explanation”When you create a new Next.js project, the framework generates a specific folder structure that separates concerns: routing, components, styles, static assets, and configuration. Each part has a distinct role in the application.
Mermaid Diagram 1: File-Based Routing Mapping
Section titled “Mermaid Diagram 1: File-Based Routing Mapping”flowchart LR A[pages/index.js] --> B[Route: /] C[pages/about.js] --> D[Route: /about] E[pages/blog/index.js] --> F[Route: /blog] G[pages/blog/[slug].js] --> H[Route: /blog/:slug] I[pages/blog/[...slug].js] --> J[Route: /blog/*] K[pages/api/hello.js] --> L[Route: /api/hello]Internal Working
Section titled “Internal Working”Next.js uses the file system to determine routes:
- Every file inside
pages/becomes a route based on its path pages/index.js→/pages/about.js→/aboutpages/blog/[slug].js→/blog/:slug(dynamic route)- Files starting with
_are exempt from routing (e.g.,_app.js,_document.js)
Mermaid Diagram 2: Special File Hierarchy
Section titled “Mermaid Diagram 2: Special File Hierarchy”flowchart TD A[_document.js] --> B[Customizes <html>, <body>] A --> C[Only for pages rendered on server] D[_app.js] --> E[Wrapper around all pages] D --> F[Persists state between page changes] D --> G[Custom error boundaries] E[pages/*.js] --> H[Individual page components] H --> I[Receive props from getServerSideProps/getStaticProps]Architecture
Section titled “Architecture”A Next.js application follows a convention-over-configuration approach. The core directories are:
pages/- Contains page components that become routes via file-system routingpublic/- Serves static assets at the root URL (e.g.,/images/logo.png)styles/- Holds CSS files (supports CSS Modules, Sass, etc.)- Additional directories like
components/,lib/,hooks/are conventional for organizing code Special files like_app.js(custom App component) and_document.js(custom HTML document) allow for global customization.
Mermaid Diagram 3: Typical File Structure Hierarchy
Section titled “Mermaid Diagram 3: Typical File Structure Hierarchy”graph TD A[Project Root] --> B[pages/] A --> C[public/] A --> D[styles/] A --> E[components/] A --> F[lib/] A --> G[hooks/] A --> H[next.config.js] A --> I[package.json] B --> B1[index.js] B --> B2[about.js] B --> B3[blog/] B3 --> B3a[index.js] B3 --> B3b[slug.js] C --> C1[favicon.ico] C --> C2[images/] D --> D1[globals.css] E --> E1[header.js] E --> E2[button.js] F --> F1[api.js] G --> G1[useAuth.js]Step-by-Step Flow
Section titled “Step-by-Step Flow”- Examine the root: Look at the top-level directories after creating a new project.
- Identify routes: Check the
pages/directory to see what routes are available. - Locate static assets: Verify that images, icons, and other static files are in
public/. - Review styles: Check
styles/for global CSS and component-specific styles. - Explore components: Look in
components/for reusable UI elements. - Check configuration: Review
next.config.jsandpackage.jsonfor project settings. - Understand special files: Note the presence of
_app.jsand_document.jsfor customizations. - Verify API routes: Look in
pages/api/for backend endpoints.
Mermaid Diagram 4: Development Workflow
Section titled “Mermaid Diagram 4: Development Workflow”sequenceDiagram Developer->>Editor: Edit file in pages/ or components/ Editor->>FileSystem: Save changes FileSystem->>Next.js: Detect file change Next.js->>Next.js: Recompile affected modules Next.js->>Browser: Send updated HTML/CSS/JS via HMR Browser->>User: Reflect changes instantly User->>Browser: Interact with updated UI Browser->>Next.js: API request (if applicable) Next.js->>Database: Query data Database-->>Next.js: Return results Next.js->>Browser: Send JSON responseSyntax
Section titled “Syntax”Importing a Component
Section titled “Importing a Component”// In any component or pageimport Header from '../components/Header';Exporting a Page
Section titled “Exporting a Page”export default function AboutPage() { return <div>About Us</div>;}Linking Between Pages
Section titled “Linking Between Pages”import Link from 'next/link';
<Link href="/about"> <a>About</a></Link>Basic Example
Section titled “Basic Example”Creating a new page and linking to it:
- Create
pages/contact.js:
export default function Contact() { return <div>Contact Us</div>;}- Add a link in
pages/index.js:
import Link from 'next/link';
export default function Home() { return ( <div> <h1>Home</h1> <Link href="/contact"> <a>Contact</a> </Link> </div> );}Intermediate Example
Section titled “Intermediate Example”Using dynamic routes for blog posts:
- Create
pages/posts/[id].js:
export default function Post({ params }) { return <div>Post ID: {params.id}</div>;}- Link to a specific post:
import Link from 'next/link';
<Link href="/posts/1"> <a>First Post</a></Link>Advanced Example
Section titled “Advanced Example”Setting up an API route:
- Create
pages/api/user.js:
export default function handler(req, res) { if (req.method === 'GET') { res.status(200).json({ name: 'John Doe', email: 'john@example.com' }); } else { res.status(405).end(); // Method Not Allowed }}- Fetch from a component:
import { useEffect, useState } from 'react';
export default function UserProfile() { const [user, setUser] = useState(null);
useEffect(() => { fetch('/api/user') .then(res => res.json()) .then(data => setUser(data)); }, []);
return <div>{user ? user.name : 'Loading...'}</div>;}Production Example
Section titled “Production Example”In a production build:
- The
.next/directory contains optimized server and client builds - Static assets in
public/are served with efficient caching - CSS is extracted and minified
- API routes become serverless functions when deployed to platforms like Vercel
Folder Structure
Section titled “Folder Structure”A typical Next.js project after setting up common directories:
my-app/├── node_modules/├── pages/│ ├── index.js # Homepage│ ├── about.js # About page│ ├── blog/│ │ ├── index.js # Blog list│ │ └── [slug].js # Single post│ └── api/│ └── hello.js # API route├── public/│ ├── images/│ │ └── logo.png│ └── favicon.ico├── styles/│ ├── globals.css│ └── modules/│ ├── Header.module.css│ └── Button.module.css├── components/│ ├── layout/│ │ ├── Header.js│ │ └── Footer.js│ └── ui/│ ├── Button.js│ └── Input.js├── lib/│ ├── api.js│ └── utils.js├── hooks/│ ├── useAuth.js│ └── useFetch.js└── next.config.jsBest Practices
Section titled “Best Practices”- Keep
pages/lean: only place page components here; move reusable logic tocomponents/orlib/ - Use consistent naming: PascalCase for components (
Button.js), camelCase for hooks (useAuth.js) - Group related files: create feature-based folders (e.g.,
components/profile/) - Leverage the
public/directory: place images, icons, androbots.txthere - Use
_app.jsfor wrappers: providers, global CSS, layout components - Avoid deep nesting: keep directory structure shallow for easier navigation
- Follow convention-over-configuration: trust Next.js defaults unless you have a specific reason to change
Common Mistakes
Section titled “Common Mistakes”- Placing components directly in
pages/(they become unintended routes) - Forgetting that files/folders starting with
_are exempt from routing - Putting styles in
pages/instead ofstyles/or alongside components - Not understanding that
public/serves at root URL (so/images/logo.pngnot/public/images/logo.png) - Misplacing API routes (must be in
pages/api/, notapi/at root) - Assuming all JavaScript files in the project become routes (only those in
pages/)
Performance Notes
Section titled “Performance Notes”- Next.js automatically code-splits by page, so each page loads only its necessary JavaScript
- Large components should be moved to
components/and imported only where needed - Dynamic imports (
next/dynamic) can further split code for large libraries - The
public/directory is served efficiently with proper caching headers (when deployed to Vercel or similar) - Use
next-bundle-analyzerto visualize what’s in each page’s bundle and optimize accordingly
Security Notes
Section titled “Security Notes”- Files in
pages/are server-rendered by default (unless using dynamic imports withssr: false) - Validate and sanitize dynamic route parameters to prevent injection attacks
- The
public/directory is publicly accessible—never store sensitive files like.envor private keys here - API routes in
pages/api/follow standard backend security practices (authentication, input validation, etc.) - Regularly update dependencies with
npm auditto address known vulnerabilities
SEO Considerations
Section titled “SEO Considerations”- Proper file structure ensures search engine crawlers can correctly index your application
- Dynamic routes (e.g.,
[slug].js) are crawled when used withgetStaticPaths/getServerSideProps - Use the
next/headcomponent to set meta tags (title, description) for each page - Generate a
sitemap.xmlinpublic/to help search engines discover your content - Ensure fast loading times by optimizing images, leveraging browser caching, and minimizing render-blocking resources
Interview Questions
Section titled “Interview Questions”- What is the purpose of the
pagesdirectory in a Next.js project? - How does Next.js convert files in
pages/to routes? - What happens if you place a component file directly in the
pagesdirectory? - What is the difference between
pages/index.jsandpages/blog/index.js? - Where should you place static assets like images and favicons?
- What special files can you override in a Next.js project (e.g.,
_app.js)? - How are API routes structured in Next.js?
- Explain the difference between
getStaticPropsandgetServerSideProps. - What is the role of the
publicdirectory? - How would you organize a large Next.js project for maintainability?
-
Which file becomes the homepage route (
/)? a)pages/home.jsb)pages/index.jsc)app/index.jsd)routes/index.jsAnswer
-
Where should you place a reusable Button component? a)
pages/Button.jsb)components/Button.jsc)styles/Button.jsd)public/Button.jsAnswer
-
What does the
publicdirectory serve? a) Files at/public/*b) Files at the root URL (/*) c) Files at/static/*d) Files at/assets/*Answer
-
Which file allows you to customize the
<html>and<body>tags? a)_app.jsb)_document.jsc)custom-html.jsd)next-config.jsAnswer
-
How do you create a dynamic route for a blog post ID? a)
pages/blog/id.jsb)pages/blog/[id].jsc)pages/blog/{id}.jsd)pages/blog/:id.jsAnswer
-
What is the purpose of the
_app.jsfile? a) Defines the root layout b) Configures the build process c) Stores environment variables d) Handles API routingAnswer
Practice Exercise
Section titled “Practice Exercise”- Create a new Next.js project called
structure-exercise. - Examine the default file structure created by
create-next-app. - Create a
components/directory and add aHeader.jscomponent. - Create a
styles/directory (if not present) and add aglobals.cssfile. - Create a
pages/about.jspage that uses your Header component. - Add an image to the
public/images/directory and display it on the About page. - Run the development server to verify everything works.
Mini Project
Section titled “Mini Project”Build a structured blog layout:
- Create a
components/directory with:Layout.js(wrapper component)Header.js(site navigation)Footer.js(site footer)PostCard.js(component to display blog posts)
- Create a
styles/directory with:globals.css(global styles)Layout.module.css(styles for Layout)Header.module.css(styles for Header)PostCard.module.css(styles for PostCard)
- Create pages:
pages/index.js(homepage with list of posts)pages/blog/[slug].js(individual post page)pages/about.js(about page)
- Add sample data (can be hardcoded initially):
const posts = [{ id: 1, slug: 'first-post', title: 'First Post', date: '2023-01-01', content: 'Hello world!' },{ id: 2, slug: 'second-post', title: 'Second Post', date: '2023-01-02', content: 'Another post.' }];
- Style the components using CSS Modules.
- Add images to
public/images/for blog posts (e.g.,post1.jpg,post2.jpg). - Implement linking from the homepage to individual posts.
- Run the development server and verify the blog displays correctly.
Summary
Section titled “Summary”In this topic, you explored the default Next.js file structure and learned the purpose of each directory and special file. You understand how file-based routing works, where to place different types of files, and how to organize your project for maintainability and scalability. This knowledge is essential for efficient development as you build more complex applications.
Cheat Sheet
Section titled “Cheat Sheet”# Key Directories/pages/ # Routes (each file = route)/public/ # Static assets (served at root)/styles/ # CSS files/components/# Reusable components/lib/ # Utility functions/hooks/ # Custom React hooks
# Special Files_pages/_app.js # Wrapper for all pages_pages/_document.js # Custom HTML document_next.config.js # Next.js configuration_.env.local # Environment variables
# Routing Examplespages/index.js → /pages/about.js → /aboutpages/blog/index.js → /blogpages/blog/[slug].js → /blog/:slugpages/api/hello.js → /api/hello
# Common Directories/components/ui/ # Reusable UI elements (Button, Input)/components/layout/ # Layout components (Header, Footer)/lib/api.js # API helper functions/hooks/useAuth.js # Custom authentication hookRelated Topics
Section titled “Related Topics”- Creating Pages and Navigation (Next Topic)
- Styling in Next.js (CSS Modules, Tailwind)
- API Routes
- Dynamic Routes
- Custom _app.js and _document.js
- Environment Variables