Next.js colocation means placing a component, hook, or utility file directly inside a route's folder in the app directory, right next to the page that uses it. This works safely because only a specific set of file names actually becomes a route.
app/
blog/
page.tsx
PostCard.tsx
format-date.tspage.tsx is the only file here that Next.js treats as routable. PostCard.tsx and format-date.ts sit in the exact same folder but never become URLs, because their names do not match a reserved convention.
The exact names that matter
A route becomes public and routable only when a folder contains one of a short list of reserved file names, matched exactly. Everything else, no matter what it contains or how it's organized, stays a private project file.
| File name | Makes the folder routable |
|---|---|
page.tsx | Yes |
route.ts | Yes |
layout.tsx | Wraps children, but adds no page of its own |
| Any other name | No |
A component named page-header.tsx is safe, since it does not match page.tsx exactly. The match is on the full file name, not a substring.
Only rendered content reaches the browser
Colocating a file inside a route folder does not automatically send it to the client. Only the output a page or route file actually returns, including anything it imports and renders, reaches the browser.
// app/blog/page.tsx
import { PostCard } from "./PostCard";
export default function BlogPage() {
return <PostCard title="Hello" />;
}PostCard.tsx ships to the client here because the page imports and renders it. A colocated file the page never imports stays on disk and never reaches a visitor at all, whether or not it sits inside the route folder.
When to reach for extra structure
Plain colocation already keeps non-routing files safe, so private folders and route groups are optional extras rather than requirements. Reach for a private folder, covered in private folders and the underscore convention, when a naming collision with a future Next.js convention feels like a real risk. Reach for a route group instead, covered in route groups in Next.js, when the goal is separating sections of the app rather than hiding individual files from routing.
Common mistake
Naming a component file after a reserved convention by accident is the most common mistake, such as calling a loading spinner component loading.tsx inside a route folder. Next.js treats that name as the route's actual loading UI, not as a reusable component, which produces confusing behavior that has nothing to do with a typo in the code itself.
The safest habit is checking a new file's name against the reserved list before saving it inside a route folder, especially for common UI terms like loading, error, or template that also happen to be routing conventions. A quick rename to something like LoadingSpinner avoids the collision entirely while keeping the file exactly where it is most convenient to find.
Rune AI
Key Insights
- Only page, route, layout, and a handful of other reserved names are routable.
- Any other file name inside app is safe to colocate next to the routes that use it.
- Only the content a page or route file actually returns reaches the browser.
- Colocation is optional, not a framework requirement.
- A private folder or a folder outside app are both valid alternatives to colocation.
Frequently Asked Questions
Can I name a component file page.tsx if it isn't meant to be a route?
Do I have to colocate files inside app?
Does colocation affect what gets sent to the browser?
Conclusion
Colocation works because only a specific, short list of file names turns a folder into a route. Everything else, no matter how deeply nested inside app, stays a private project file unless something imports it.
More in this topic
`generateMetadata` Explained with Real Examples
What generateMetadata does, when it runs, and how to use it for real routes: awaited params, deduplicated data fetching, extending parent metadata, and returning a 404 from metadata.
Canonical URLs in Next.js: `metadataBase`, `alternates.canonical`, and Dynamic Pages
How canonical URLs work in the Next.js App Router: setting metadataBase once, writing alternates.canonical per route, handling dynamic segments, and what happens when the base URL is missing.
Open Graph and Twitter Card Metadata in Next.js
How to write Open Graph and Twitter card metadata in the Next.js App Router: the openGraph and twitter fields, automatic card defaults, article tags, and image merge rules.