Skip to content

Commit c16f478

Browse files
authored
Add Next.js Best Practices for LLMs (2025) (#120)
Introduced a new document outlining best practices for building, structuring, and maintaining Next.js applications. This guide is intended for LLMs and developers to ensure code quality, maintainability, and scalability, covering project structure, component integration, naming conventions, API routes, and general best practices.
1 parent f24139a commit c16f478

File tree

2 files changed

+144
-0
lines changed

2 files changed

+144
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ Team and project-specific instructions to enhance GitHub Copilot's behavior for
5050
| [Memory Bank](instructions/memory-bank.instructions.md) | Bank specific coding standards and best practices | [![Install in VS Code](https://img.shields.io/badge/VS_Code-Install-0098FF?style=flat-square&logo=visualstudiocode&logoColor=white)](https://vscode.dev/redirect?url=vscode%3Achat-instructions%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Finstructions%2Fmemory-bank.instructions.md) [![Install in VS Code](https://img.shields.io/badge/VS_Code_Insiders-Install-24bfa5?style=flat-square&logo=visualstudiocode&logoColor=white)](https://insiders.vscode.dev/redirect?url=vscode-insiders%3Achat-instructions%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Finstructions%2Fmemory-bank.instructions.md) |
5151
| [MS-SQL DBA Chat Mode Instructions](instructions/ms-sql-dba.instructions.md) | Instructions for customizing GitHub Copilot behavior for MS-SQL DBA chat mode. | [![Install in VS Code](https://img.shields.io/badge/VS_Code-Install-0098FF?style=flat-square&logo=visualstudiocode&logoColor=white)](https://vscode.dev/redirect?url=vscode%3Achat-instructions%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Finstructions%2Fms-sql-dba.instructions.md) [![Install in VS Code](https://img.shields.io/badge/VS_Code_Insiders-Install-24bfa5?style=flat-square&logo=visualstudiocode&logoColor=white)](https://insiders.vscode.dev/redirect?url=vscode-insiders%3Achat-instructions%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Finstructions%2Fms-sql-dba.instructions.md) |
5252
| [Next.js + Tailwind Development Instructions](instructions/nextjs-tailwind.instructions.md) | Next.js + Tailwind development standards and instructions | [![Install in VS Code](https://img.shields.io/badge/VS_Code-Install-0098FF?style=flat-square&logo=visualstudiocode&logoColor=white)](https://vscode.dev/redirect?url=vscode%3Achat-instructions%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Finstructions%2Fnextjs-tailwind.instructions.md) [![Install in VS Code](https://img.shields.io/badge/VS_Code_Insiders-Install-24bfa5?style=flat-square&logo=visualstudiocode&logoColor=white)](https://insiders.vscode.dev/redirect?url=vscode-insiders%3Achat-instructions%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Finstructions%2Fnextjs-tailwind.instructions.md) |
53+
| [Next.js Best Practices for LLMs (2025)](instructions/nextjs.instructions.md) | (2025) specific coding standards and best practices | [![Install in VS Code](https://img.shields.io/badge/VS_Code-Install-0098FF?style=flat-square&logo=visualstudiocode&logoColor=white)](https://vscode.dev/redirect?url=vscode%3Achat-instructions%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Finstructions%2Fnextjs.instructions.md) [![Install in VS Code](https://img.shields.io/badge/VS_Code_Insiders-Install-24bfa5?style=flat-square&logo=visualstudiocode&logoColor=white)](https://insiders.vscode.dev/redirect?url=vscode-insiders%3Achat-instructions%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Finstructions%2Fnextjs.instructions.md) |
5354
| [Code Generation Guidelines](instructions/nodejs-javascript-vitest.instructions.md) | Guidelines for writing Node.js and JavaScript code with Vitest testing | [![Install in VS Code](https://img.shields.io/badge/VS_Code-Install-0098FF?style=flat-square&logo=visualstudiocode&logoColor=white)](https://vscode.dev/redirect?url=vscode%3Achat-instructions%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Finstructions%2Fnodejs-javascript-vitest.instructions.md) [![Install in VS Code](https://img.shields.io/badge/VS_Code_Insiders-Install-24bfa5?style=flat-square&logo=visualstudiocode&logoColor=white)](https://insiders.vscode.dev/redirect?url=vscode-insiders%3Achat-instructions%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Finstructions%2Fnodejs-javascript-vitest.instructions.md) |
5455
| [Performance Optimization Best Practices](instructions/performance-optimization.instructions.md) | The most comprehensive, practical, and engineer-authored performance optimization instructions for all languages, frameworks, and stacks. Covers frontend, backend, and database best practices with actionable guidance, scenario-based checklists, troubleshooting, and pro tips. | [![Install in VS Code](https://img.shields.io/badge/VS_Code-Install-0098FF?style=flat-square&logo=visualstudiocode&logoColor=white)](https://vscode.dev/redirect?url=vscode%3Achat-instructions%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Finstructions%2Fperformance-optimization.instructions.md) [![Install in VS Code](https://img.shields.io/badge/VS_Code_Insiders-Install-24bfa5?style=flat-square&logo=visualstudiocode&logoColor=white)](https://insiders.vscode.dev/redirect?url=vscode-insiders%3Achat-instructions%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Finstructions%2Fperformance-optimization.instructions.md) |
5556
| [Playwright Typescript](instructions/playwright-typescript.instructions.md) | Playwright test generation instructions | [![Install in VS Code](https://img.shields.io/badge/VS_Code-Install-0098FF?style=flat-square&logo=visualstudiocode&logoColor=white)](https://vscode.dev/redirect?url=vscode%3Achat-instructions%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Finstructions%2Fplaywright-typescript.instructions.md) [![Install in VS Code](https://img.shields.io/badge/VS_Code_Insiders-Install-24bfa5?style=flat-square&logo=visualstudiocode&logoColor=white)](https://insiders.vscode.dev/redirect?url=vscode-insiders%3Achat-instructions%2Finstall%3Furl%3Dhttps%3A%2F%2Fraw.githubusercontent.com%2Fgithub%2Fawesome-copilot%2Fmain%2Finstructions%2Fplaywright-typescript.instructions.md) |
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
---
2+
applyTo: '**'
3+
---
4+
5+
# Next.js Best Practices for LLMs (2025)
6+
7+
_Last updated: July 2025_
8+
9+
This document summarizes the latest, authoritative best practices for building, structuring, and maintaining Next.js applications. It is intended for use by LLMs and developers to ensure code quality, maintainability, and scalability.
10+
11+
---
12+
13+
## 1. Project Structure & Organization
14+
15+
- **Use the `app/` directory** (App Router) for all new projects. Prefer it over the legacy `pages/` directory.
16+
- **Top-level folders:**
17+
- `app/` — Routing, layouts, pages, and route handlers
18+
- `public/` — Static assets (images, fonts, etc.)
19+
- `lib/` — Shared utilities, API clients, and logic
20+
- `components/` — Reusable UI components
21+
- `contexts/` — React context providers
22+
- `styles/` — Global and modular stylesheets
23+
- `hooks/` — Custom React hooks
24+
- `types/` — TypeScript type definitions
25+
- **Colocation:** Place files (components, styles, tests) near where they are used, but avoid deeply nested structures.
26+
- **Route Groups:** Use parentheses (e.g., `(admin)`) to group routes without affecting the URL path.
27+
- **Private Folders:** Prefix with `_` (e.g., `_internal`) to opt out of routing and signal implementation details.
28+
29+
- **Feature Folders:** For large apps, group by feature (e.g., `app/dashboard/`, `app/auth/`).
30+
- **Use `src/`** (optional): Place all source code in `src/` to separate from config files.
31+
32+
## 2.1. Server and Client Component Integration (App Router)
33+
34+
**Never use `next/dynamic` with `{ ssr: false }` inside a Server Component.** This is not supported and will cause a build/runtime error.
35+
36+
**Correct Approach:**
37+
- If you need to use a Client Component (e.g., a component that uses hooks, browser APIs, or client-only libraries) inside a Server Component, you must:
38+
1. Move all client-only logic/UI into a dedicated Client Component (with `'use client'` at the top).
39+
2. Import and use that Client Component directly in the Server Component (no need for `next/dynamic`).
40+
3. If you need to compose multiple client-only elements (e.g., a navbar with a profile dropdown), create a single Client Component that contains all of them.
41+
42+
**Example:**
43+
44+
```tsx
45+
// Server Component
46+
import DashboardNavbar from '@/components/DashboardNavbar';
47+
48+
export default async function DashboardPage() {
49+
// ...server logic...
50+
return (
51+
<>
52+
<DashboardNavbar /> {/* This is a Client Component */}
53+
{/* ...rest of server-rendered page... */}
54+
</>
55+
);
56+
}
57+
```
58+
59+
**Why:**
60+
- Server Components cannot use client-only features or dynamic imports with SSR disabled.
61+
- Client Components can be rendered inside Server Components, but not the other way around.
62+
63+
**Summary:**
64+
Always move client-only UI into a Client Component and import it directly in your Server Component. Never use `next/dynamic` with `{ ssr: false }` in a Server Component.
65+
66+
---
67+
68+
## 2. Component Best Practices
69+
70+
- **Component Types:**
71+
- **Server Components** (default): For data fetching, heavy logic, and non-interactive UI.
72+
- **Client Components:** Add `'use client'` at the top. Use for interactivity, state, or browser APIs.
73+
- **When to Create a Component:**
74+
- If a UI pattern is reused more than once.
75+
- If a section of a page is complex or self-contained.
76+
- If it improves readability or testability.
77+
- **Naming Conventions:**
78+
- Use `PascalCase` for component files and exports (e.g., `UserCard.tsx`).
79+
- Use `camelCase` for hooks (e.g., `useUser.ts`).
80+
- Use `snake_case` or `kebab-case` for static assets (e.g., `logo_dark.svg`).
81+
- Name context providers as `XyzProvider` (e.g., `ThemeProvider`).
82+
- **File Naming:**
83+
- Match the component name to the file name.
84+
- For single-export files, default export the component.
85+
- For multiple related components, use an `index.ts` barrel file.
86+
- **Component Location:**
87+
- Place shared components in `components/`.
88+
- Place route-specific components inside the relevant route folder.
89+
- **Props:**
90+
- Use TypeScript interfaces for props.
91+
- Prefer explicit prop types and default values.
92+
- **Testing:**
93+
- Co-locate tests with components (e.g., `UserCard.test.tsx`).
94+
95+
## 3. Naming Conventions (General)
96+
97+
- **Folders:** `kebab-case` (e.g., `user-profile/`)
98+
- **Files:** `PascalCase` for components, `camelCase` for utilities/hooks, `kebab-case` for static assets
99+
- **Variables/Functions:** `camelCase`
100+
- **Types/Interfaces:** `PascalCase`
101+
- **Constants:** `UPPER_SNAKE_CASE`
102+
103+
## 4. API Routes (Route Handlers)
104+
105+
- **Prefer API Routes over Edge Functions** unless you need ultra-low latency or geographic distribution.
106+
- **Location:** Place API routes in `app/api/` (e.g., `app/api/users/route.ts`).
107+
- **HTTP Methods:** Export async functions named after HTTP verbs (`GET`, `POST`, etc.).
108+
- **Request/Response:** Use the Web `Request` and `Response` APIs. Use `NextRequest`/`NextResponse` for advanced features.
109+
- **Dynamic Segments:** Use `[param]` for dynamic API routes (e.g., `app/api/users/[id]/route.ts`).
110+
- **Validation:** Always validate and sanitize input. Use libraries like `zod` or `yup`.
111+
- **Error Handling:** Return appropriate HTTP status codes and error messages.
112+
- **Authentication:** Protect sensitive routes using middleware or server-side session checks.
113+
114+
## 5. General Best Practices
115+
116+
- **TypeScript:** Use TypeScript for all code. Enable `strict` mode in `tsconfig.json`.
117+
- **ESLint & Prettier:** Enforce code style and linting. Use the official Next.js ESLint config.
118+
- **Environment Variables:** Store secrets in `.env.local`. Never commit secrets to version control.
119+
- **Testing:** Use Jest, React Testing Library, or Playwright. Write tests for all critical logic and components.
120+
- **Accessibility:** Use semantic HTML and ARIA attributes. Test with screen readers.
121+
- **Performance:**
122+
- Use built-in Image and Font optimization.
123+
- Use Suspense and loading states for async data.
124+
- Avoid large client bundles; keep most logic in Server Components.
125+
- **Security:**
126+
- Sanitize all user input.
127+
- Use HTTPS in production.
128+
- Set secure HTTP headers.
129+
- **Documentation:**
130+
- Write clear README and code comments.
131+
- Document public APIs and components.
132+
133+
# Avoid Unnecessary Example Files
134+
135+
Do not create example/demo files (like ModalExample.tsx) in the main codebase unless the user specifically requests a live example, Storybook story, or explicit documentation component. Keep the repository clean and production-focused by default.
136+
137+
# Always use the latest documentation and guides
138+
- For every nextjs related request, begin by searching for the most current nextjs documentation, guides, and examples.
139+
- Use the following tools to fetch and search documentation if they are available:
140+
- `resolve_library_id` to resolve the package/library name in the docs.
141+
- `get_library_docs` for up to date documentation.
142+
143+

0 commit comments

Comments
 (0)