Skip to main content
This page covers the structure of a Next.js Pages Router project—what each top-level directory and special file does.

Top-level directories

Top-level files

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 lang attribute to <html>.
  • Include third-party scripts that must load before the page.
pages/_document.tsx
Do not add application logic or event handlers to _document.tsx. It runs on the server only and is not hydrated on the client.

_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.