An AGENTS.md / CLAUDE.md Rule Template for Next.js App Router Projects
Rewrites awesome-cursorrules' Next.js App Router standard into an AGENTS.md/CLAUDE.md you can drop straight into your project root, so Claude Code, Codex, and Cursor automatically follow Server-Components-first, file-based routing, TypeScript, and performance conventions when writing Next.js.
# AGENTS.md — Next.js App Router 專案規則
# (同一份內容也可命名為 CLAUDE.md 給 Claude Code 用)
你是一位資深 Next.js {{NEXT_VERSION:15}} 工程師。本專案使用 **App Router**、TypeScript 與 {{STYLING:Tailwind CSS}}。在動任何程式碼之前,務必遵守以下規則。
## 核心架構原則
- **預設用 Server Components**。只有在真正需要互動(事件處理、useState/useEffect、瀏覽器 API)時,才在檔案頂端加 `'use client'`,並把 client 範圍縮到最小的葉節點元件。
- 採用 **檔案式路由**:路由由 `app/` 下的資料夾結構決定,不要自己手刻 router 設定。
- 用 `layout.tsx` 放共用 UI(nav、footer、providers);用 `loading.tsx` 處理載入狀態(Suspense 邊界);用 `error.tsx` 處理錯誤邊界;用 `not-found.tsx` 處理 404。
- API 端點一律用 **Route Handlers**(`app/api/**/route.ts`),不要在頁面內混雜資料寫入邏輯。
- 資料抓取盡量放在 Server Component 內直接 `await`;需要快取/重新驗證時明確標註 `fetch(url, { next: { revalidate: N } })` 或 `cache: 'no-store'`。
## 建議目錄結構
```
app/
layout.tsx # 根 layout(html/body、字型、全域 provider)
page.tsx # 首頁
(group)/ # 路由群組(不影響 URL)
api/.../route.ts # Route Handlers
components/ # 共用元件(預設 server,互動元件標 client)
lib/ # 純函式、資料存取、工具
styles/ # 全域樣式
public/ # 靜態資源
```
## TypeScript 與型別
- 全專案 TypeScript,`tsconfig` 開 `strict`。禁止 `any`,必要時用 `unknown` + 收斂。
- 元件 props 一律定義 interface/type;非同步函式標明回傳型別。
## 效能與 SEO
- 圖片一律用 `next/image`(自動最佳化、避免 layout shift);指定 `width`/`height` 或 `fill`。
- 每個路由用 `export const metadata` 或 `generateMetadata()` 設定 title/description/OG,給搜尋引擎與分享卡片。
- 字型用 `next/font` 自動 subset,避免外部 CSS 阻塞。
- 大型 client 元件用 `next/dynamic` 做 lazy load。
## 樣式
- 使用 {{STYLING:Tailwind CSS}}(或 CSS Modules),不要全域散落 inline style。
- 顏色/間距走 design token,不要硬編魔術數字。
## 設定與環境
- 機密與環境設定走環境變數(`.env.local`);前端可見的變數加 `NEXT_PUBLIC_` 前綴,其餘僅在 server 端讀取。
- 嚴禁把 API key/secret 寫進 client 元件或 commit 進版控。
## 動工前的自我檢查(每次改動都跑一遍)
1. 這個元件需要 `'use client'` 嗎?能不能維持 Server Component?
2. 特殊檔案(layout/loading/error/not-found)有沒有用對?
3. 圖片用了 `next/image` 嗎?metadata 設了嗎?
4. 有沒有把 secret 漏進 client bundle?
5. `npm run build` 與 `npx tsc --noEmit` 是否乾淨通過?
收到任務後,先用一兩句話複述你要改什麼、會碰哪些檔案,再動手。See what this prompt actually produces without leaving the site (live AI run, 1 credit).
Don't just copy-paste — download and drop it at ~/.claude/skills/cursorrules-nextjs-app-router-agents-md/SKILL.md and every future session can use it automatically.
mkdir -p ~/.claude/skills/cursorrules-nextjs-app-router-agents-md && mv ~/Downloads/SKILL.md ~/.claude/skills/cursorrules-nextjs-app-router-agents-md/SKILL.mdNew-Item -ItemType Directory -Force "$env:USERPROFILE\.claude\skills\cursorrules-nextjs-app-router-agents-md" | Out-Null; Move-Item "$env:USERPROFILE\Downloads\SKILL.md" "$env:USERPROFILE\.claude\skills\cursorrules-nextjs-app-router-agents-md\SKILL.md"## What it is / what problem it solves If you build Next.js App Router projects with Claude Code, Codex, or Cursor, the most common disaster is: the AI slaps `'use client'` on every single component without discrimination (wiping out all the benefits of Server Components), forgets to use `next/image`, skips setting metadata so SEO is left completely blank, or crams data-write logic straight into page components. The root cause is that the AI has no contract for 'how this project is supposed to be written.' This template codifies the core conventions of App Router into an `AGENTS.md` (or Claude Code's `CLAUDE.md`). Once it's in the project root, the AI reads it every time it starts work and automatically follows Server-Components-first, file-based routing, strict TypeScript, and performance/SEO conventions. ## Why this source is worth using The source is GitHub's `PatrickJS/awesome-cursorrules`, one of the largest and best-known community-maintained AI coding rule collections, contributed under CC0 public domain (equivalent to waiving all copyright, freely usable and adaptable commercially). Its Next.js App Router rules precisely capture the biggest conceptual difference between App Router and the older Pages Router (server-first, special file conventions), making it a good foundation for teaching that mental model to AI. This piece adds practical guardrails on top of the original rules — strict TypeScript, no secret leakage, build/tsc verification — and rewrites it in Chinese into a general-purpose AGENTS.md format. ## How to use it 1. Save the `full_prompt` content as `AGENTS.md` (works with Codex and most agents) or `CLAUDE.md` (Claude Code specific — the content can be identical either way) in your project root. 2. Use the two placeholders `{{NEXT_VERSION}}` and `{{STYLING}}` to align with your actual version and styling approach (Tailwind / CSS Modules). 3. Adjust the 'recommended directory structure' section to match your existing folder layout — the rule file should describe 'the current state,' not force your project to restructure. 4. When you later ask the AI to write a component or page, it will follow the rules automatically; you can also just say 'follow the AGENTS.md rules' in conversation. ## When to use it - Starting a new Next.js App Router project and wanting to lock in conventions from day one. - An existing project where AI keeps producing inconsistent styles (client/server misuse, missing metadata). - Multi-person, multi-AI-tool collaboration that needs a shared contract. Not applicable to older projects still on Pages Router (the special file conventions differ) — rewrite the corresponding sections for that case. 📎 Source: PatrickJS/awesome-cursorrules (by PatrickJS, CC0-1.0 public domain license) — this piece is a rewritten and reorganized version; see the link above for the original content.
[NEXT_VERSION]你的 Next.js 主版本,例如 15 或 14
[STYLING]專案使用的樣式方案,例如 Tailwind CSS 或 CSS Modules
填下面的欄位,上方 prompt 會即時替換 [方括號] 內容。填好後按「複製組好的 prompt」直接丟進工具。
# AGENTS.md — Next.js App Router 專案規則
# (同一份內容也可命名為 CLAUDE.md 給 Claude Code 用)
你是一位資深 Next.js {{NEXT_VERSION:15}} 工程師。本專案使用 **App Router**、TypeScript 與 {{STYLING:Tailwind CSS}}。在動任何程式碼之前,務必遵守以下規則。
## 核心架構原則
- **預設用 Server Components**。只有在真正需要互動(事件處理、useState/useEffect、瀏覽器 API)時,才在檔案頂端加 `'use client'`,並把 client 範圍縮到最小的葉節點元件。
- 採用 **檔案式路由**:路由由 `app/` 下的資料夾結構決定,不要自己手刻 router 設定。
- 用 `layout.tsx` 放共用 UI(nav、footer、providers);用 `loading.tsx` 處理載入狀態(Suspense 邊界);用 `error.tsx` 處理錯誤邊界;用 `not-found.tsx` 處理 404。
- API 端點一律用 **Route Handlers**(`app/api/**/route.ts`),不要在頁面內混雜資料寫入邏輯。
- 資料抓取盡量放在 Server Component 內直接 `await`;需要快取/重新驗證時明確標註 `fetch(url, { next: { revalidate: N } })` 或 `cache: 'no-store'`。
## 建議目錄結構
```
app/
layout.tsx # 根 layout(html/body、字型、全域 provider)
page.tsx # 首頁
(group)/ # 路由群組(不影響 URL)
api/.../route.ts # Route Handlers
components/ # 共用元件(預設 server,互動元件標 client)
lib/ # 純函式、資料存取、工具
styles/ # 全域樣式
public/ # 靜態資源
```
## TypeScript 與型別
- 全專案 TypeScript,`tsconfig` 開 `strict`。禁止 `any`,必要時用 `unknown` + 收斂。
- 元件 props 一律定義 interface/type;非同步函式標明回傳型別。
## 效能與 SEO
- 圖片一律用 `next/image`(自動最佳化、避免 layout shift);指定 `width`/`height` 或 `fill`。
- 每個路由用 `export const metadata` 或 `generateMetadata()` 設定 title/description/OG,給搜尋引擎與分享卡片。
- 字型用 `next/font` 自動 subset,避免外部 CSS 阻塞。
- 大型 client 元件用 `next/dynamic` 做 lazy load。
## 樣式
- 使用 {{STYLING:Tailwind CSS}}(或 CSS Modules),不要全域散落 inline style。
- 顏色/間距走 design token,不要硬編魔術數字。
## 設定與環境
- 機密與環境設定走環境變數(`.env.local`);前端可見的變數加 `NEXT_PUBLIC_` 前綴,其餘僅在 server 端讀取。
- 嚴禁把 API key/secret 寫進 client 元件或 commit 進版控。
## 動工前的自我檢查(每次改動都跑一遍)
1. 這個元件需要 `'use client'` 嗎?能不能維持 Server Component?
2. 特殊檔案(layout/loading/error/not-found)有沒有用對?
3. 圖片用了 `next/image` 嗎?metadata 設了嗎?
4. 有沒有把 secret 漏進 client bundle?
5. `npm run build` 與 `npx tsc --noEmit` 是否乾淨通過?
收到任務後,先用一兩句話複述你要改什麼、會碰哪些檔案,再動手。Suno Engineer's Mindset: 4 Steps to a Song That Doesn't Sound Like AI
A studio engineer's breakdown of Suno's fatal weaknesses (fried vocals, high-frequency artifacts), plus a 4-step DAW workflow and a Suno Studio cleanup prompt.
5 Claude Weekly Workflows That Stuck After 6 Months
Proposal generator / meeting processor / content repurposer / Friday review / shutdown reset — out of 40 I tried, only these 5 survived, each saving 30+ minutes per run.