Menu

Project Structure and Special Files

Lesson 5: Project Structure and Special Files

What is in a new project?

learnhub/
├─ app/
│  ├─ layout.tsx      # Root layout: <html> and <body>
│  ├─ page.tsx        # Home page at /
│  ├─ globals.css     # Global styles
│  └─ favicon.ico
├─ public/           # Static files served as-is
├─ next.config.ts    # Next.js settings
├─ tsconfig.json     # TypeScript settings
├─ package.json      # Scripts and dependencies
└─ eslint.config.mjs # Lint rules
Folder or file Role
app/ All routes, layouts, and route-level UI
public/ Images, fonts, and files served from / (e.g. public/logo.png → /logo.png)
next.config.ts Framework options: images, redirects, caching features
.env.local Secret environment variables (never committed)
.next/ Build output, generated automatically

Special file names in app/

Next.js gives meaning to certain file names inside route folders:

File Purpose Covered in
page.tsx The UI for a route; makes the route public Module 2
layout.tsx Shared UI that wraps pages and stays mounted Module 2
loading.tsx Loading UI shown while a route loads Module 2
error.tsx Error UI for a route segment Modules 2, 10
not-found.tsx UI for missing pages Module 2
route.ts An API endpoint (Route Handler) Module 5
template.tsx Like a layout, but re-mounts on navigation Module 2
default.tsx Fallback for parallel routes Module 8

At the project root, proxy.ts (Module 8) and instrumentation.ts (Module 11) are also recognised.

Example

The root layout created for you:

// app/layout.tsx
import type { Metadata } from 'next';
import './globals.css';

export const metadata: Metadata = {
  title: 'LearnHub',
  description: 'Online courses for beginners',
};

export default function RootLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  return (
    <html lang="en">
      <body>{children}</body>
    </html>
  );
}

How it works

  • The root layout is required. It must contain <html> and <body>.
  • children is the current page. When the URL changes, only children changes.
  • metadata sets the page <title> and description for search engines (Module 4).
  • Any other file in app/, such as components/Button.tsx, is not a route unless it is named page or route.

Common mistakes

Mistake Effect Fix
Adding <html> in a nested layout Invalid HTML, hydration errors Only the root layout renders <html> and <body>
Naming a page Page.tsx or index.tsx Route does not appear Use lowercase page.tsx
Putting secrets in next.config.ts Exposed in the repository Use .env.local
Referencing /public/logo.png in code 404 error Drop public: use /logo.png

Key takeaways

  • The app folder defines routes; special file names define behaviour.
  • Files that are not page or route are safe to keep next to routes.

Try it: Add a components folder, create a Header component, and render it above children in the root layout.


Nextjs Tutorial

J

Jebasta





Get in Touch For Details