Top-level directories
| Directory | Description |
|---|---|
pages/ | File-based routing. Every file is a route. |
public/ | Static assets served at /. |
styles/ | CSS files (convention, not required). |
components/ | Shared React components (convention, not required). |
lib/ | Shared utility functions (convention, not required). |
Top-level files
| File | Description |
|---|---|
next.config.js | Configuration for Next.js. |
package.json | Project dependencies and scripts. |
tsconfig.json | TypeScript configuration. |
.env | Environment variables. |
.env.local | Local environment variable overrides. |
.gitignore | Files to exclude from Git. |
The pages/ directory
The pages/ directory is the heart of a Pages Router project. Files inside it become routes automatically.
Special files
_app.tsx
Overrides the default App component. Use it to:
- Wrap every page in a shared layout.
- Inject global CSS.
- Keep state between page navigations.
pages/_app.tsx
_document.tsx
Customizes the <html> and <body> tags. Rendered on the server only. Use it to:
- Add a
langattribute to<html>. - Include third-party scripts that must load before the page.
pages/_document.tsx
_error.tsx
Customizes the error page shown for unhandled errors in production. For most cases, prefer a specific 500.tsx page instead.
pages/_error.tsx
404.tsx and 500.tsx
Static pages for 404 and 500 errors. Next.js generates these at build time.
pages/404.tsx
The public/ directory
Files in public/ are served at the root path /. For example, public/logo.png is accessible at /logo.png.
The pages/api/ directory
Files inside pages/api/ are treated as API endpoints rather than pages. They run on the server and are not included in the client-side bundle.
In the App Router, Route Handlers replace API routes and support additional features like streaming.
