Next.js Project Structure — Understanding Every File & Folder
app/ folder, Next.js App Router lo routing system core. Ee folder lopala unna prathi sub-folder, oka URL segment ni represent chestundi, and aa folder lopala unna page.tsx file, aa route ki publicly accessible page ni create chestundi.
Idi "file-system based routing" — separate router library (react-router లాంటిది) install cheyాల్సిన అవసరం ledu, folders create చేయడమే routing.
| File | Purpose |
|---|---|
page.tsx | Route ki UI define chestundi — publicly accessible page create chestundi |
layout.tsx | Multiple pages madhya shared UI (header, footer, sidebar) define chestundi |
globals.css | Whole app ki apply ayye global CSS styles |
loading.tsx | Route load avuthunnappudu chupinche loading UI |
error.tsx | Route lo error వచ్చినప్పుడు chupinche fallback UI |
not-found.tsx | 404 page — route దొరకనప్పుడు chupinche UI |
Prathi Next.js App Router project ki, app/layout.tsx compulsory ga undali — idi <html> and <body> tags ni define chestundi, mొత్తం app ki wrap chestundi:
export default function RootLayout({
children,
}: {
children: React.ReactNode;
}) {
return (
<html lang="en">
<body>
<header>Our Site Header</header>
{children}
<footer>Our Site Footer</footer>
</body>
</html>
);
}
{children} prop — idi current active page (page.tsx) ni represent chestundi. Header, Footer prathi page lo repeat avvakunda, layout okte sari define chesthe chalu.
public/ folder lopala pెట్టిన files (images, fonts, favicon), direct ga root URL nundి access avuthayi — public/logo.png file ni browser lo /logo.png ani access cheyachu.
next.config.ts: Image domains, redirects, environment settings lాంటి Next.js-specific configuration.package.json: Project dependencies, scripts (dev,build,start) list chestundi.tsconfig.json: TypeScript compiler options, import aliases (@/components) define chestundi..env.local: Secret keys, API URLs lాంటి environment variables — Git lo commit cheyakudadu.
Setup lo "src directory" ki Yes cheste, app/ folder root level nundi src/app/ loki move avuthundi. Idi purely organizational — config files (next.config, package.json) ni app code నుండి visually separate chేయడానికి use chestaru, functionality change avvadu.
app/ folder lopala ఏదైనా folder లో page.tsx pెడితే, adi automatic ga oka public route avuthundi. Component files ni app/ lopala కాకుండా, separate components/ folder లో pెట్టడం safest — లేకపోతే accidental ga unwanted URLs create avvachu.
Create a components/Button.tsx file (not inside app/) that exports a reusable button component accepting a label prop.
export default function Button({ label }: { label: string }) {
return <button>{label}</button>;
}
Q app/ folder tappaninisari pages/ folder use cheyacha?
Next.js 13+ lo rendu routers unnayi — legacy 'Pages Router' (pages/ folder) and modern 'App Router' (app/ folder). Ee course entirely App Router meedha focus chestundi, ఇదే official recommended approach 2026 lo.
Q layout.tsx prathi folder lo compulsory ah?
Root level (app/layout.tsx) matrame compulsory. Nested folders lo layout.tsx optional — pెడితే, aa specific section ki matrame apply ayye shared UI create avuthundi.
Q components/ folder ఎక్కడ pెట్టాలి — app/ లోపల ah, బయట ah?
Best practice: app/ folder బయట, root level లో pెట్టడం (లేదా src/ vాడితే src/components/). Deeని valla accidental routing avvadు, and imports (@/components/Button) clean ga untayi.