返回顶部
n

nextjs-expertNext.js专家

Use when building Next.js 14/15 applications with the App Router. Invoke for routing, layouts, Server Components, Client Components, Server Actions, Route Handlers, authentication, middleware, data fetching, caching, revalidation, streaming, Suspense, loading states, error boundaries, dynamic routes, parallel routes, intercepting routes, or any Next.js architecture question.

作者: admin | 来源: ClawHub
源自
ClawHub
版本
V 1.0.0
安全检测
已通过
8,449
下载量
免费
免费
12
收藏
概述
安装方式
版本历史

nextjs-expert

Next.js 专家

全面的 Next.js 15 App Router 专家。改编自 Dave Poon 的 buildwithclaude(MIT 许可证)。

角色定义

您是资深 Next.js 工程师,专精于 App Router、React 服务端组件以及使用 TypeScript 构建生产级全栈应用。

核心原则

  1. 1. 服务端优先:组件默认为服务端组件。仅在需要使用钩子、事件处理器或浏览器 API 时才添加 use client。
  2. 将客户端边界下推:尽可能将 use client 放在组件树的最底层。
  3. 异步参数:在 Next.js 15 中,params 和 searchParams 是 Promise 类型——始终使用 await。
  4. 就近组织:将组件、测试和样式文件放在其路由附近。
  5. 全面类型化:严格使用 TypeScript。

App Router 文件约定

路由文件

文件用途
page.tsx路由的唯一 UI,使其可公开访问
layout.tsx
共享 UI 包装器,在导航间保持状态 | | loading.tsx | 使用 React Suspense 的加载 UI | | error.tsx | 路由段的错误边界(必须为 use client) | | not-found.tsx | 404 响应的 UI | | template.tsx | 类似布局,但导航时会重新渲染 | | default.tsx | 并行路由的备用 UI | | route.ts | API 端点(路由处理器) |

文件夹约定

模式用途示例
folder/路由段app/blog/ → /blog
[folder]/
动态段 | app/blog/[slug]/ → /blog/:slug | | [...folder]/ | 全捕获段 | app/docs/[...slug]/ → /docs/* | | [[...folder]]/ | 可选全捕获段 | app/shop/[[...slug]]/ → /shop 或 /shop/* | | (folder)/ | 路由组(无 URL) | app/(marketing)/about/ → /about | | @folder/ | 命名插槽(并行路由) | app/@modal/login/ | | folder/ | 私有文件夹(排除) | app/components/ |

文件层级(渲染顺序)

  1. 1. layout.tsx → 2. template.tsx → 3. error.tsx(边界)→ 4. loading.tsx(边界)→ 5. not-found.tsx(边界)→ 6. page.tsx

页面与路由

基础页面(服务端组件)

tsx
// app/about/page.tsx
export default function AboutPage() {
return (


关于我们


欢迎来到我们的公司。



)
}

动态路由

tsx
// app/blog/[slug]/page.tsx
interface PageProps {
params: Promise<{ slug: string }>
}

export default async function BlogPost({ params }: PageProps) {
const { slug } = await params
const post = await getPost(slug)
return

{post.content}

}

搜索参数

tsx
// app/search/page.tsx
interface PageProps {
searchParams: Promise<{ q?: string; page?: string }>
}

export default async function SearchPage({ searchParams }: PageProps) {
const { q, page } = await searchParams
const results = await search(q, parseInt(page || 1))
return
}

静态生成

tsx
export async function generateStaticParams() {
const posts = await getAllPosts()
return posts.map((post) => ({ slug: post.slug }))
}

// 允许不在 generateStaticParams 中的动态参数
export const dynamicParams = true



布局

根布局(必需)

tsx
// app/layout.tsx
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (

{children}

)
}

带数据获取的嵌套布局

tsx
// app/dashboard/layout.tsx
import { getUser } from @/lib/get-user

export default async function DashboardLayout({ children }: { children: React.ReactNode }) {
const user = await getUser()
return (



{children}


)
}

多个根布局的路由组

app/
├── (marketing)/
│ ├── layout.tsx # 营销布局,包含 /
│ └── about/page.tsx
└── (app)/
├── layout.tsx # 应用布局,包含 /
└── dashboard/page.tsx

元数据

tsx
// 静态
export const metadata: Metadata = {
title: 关于我们,
description: 了解更多关于我们公司的信息,
}

// 动态
export async function generateMetadata({ params }: PageProps): Promise {
const { slug } = await params
const post = await getPost(slug)
return {
title: post.title,
openGraph: { title: post.title, images: [post.coverImage] },
}
}

// 布局中的模板
export const metadata: Metadata = {
title: { template: %s | 仪表盘, default: 仪表盘 },
}



服务端组件 vs 客户端组件

决策指南

服务端组件(默认)适用场景:

  • - 获取数据或访问后端资源
  • 在服务端保留敏感信息(API 密钥、令牌)
  • 减少客户端 JavaScript 包体积
  • 无需交互性

客户端组件(use client)适用场景:

  • - 使用 useState、useEffect、useReducer
  • 使用事件处理器(onClick、onChange)
  • 使用浏览器 API(window、document)
  • 使用带状态的自定义钩子

组合模式

模式 1:服务端数据 → 客户端交互

tsx
// app/products/page.tsx(服务端)
export default async function ProductsPage() {
const products = await getProducts()
return
}

// components/product-filter.tsx(客户端)
use client
export function ProductFilter({ products }: { products: Product[] }) {
const [filter, setFilter] = useState()
const filtered = products.filter(p => p.name.includes(filter))
return (
<>
setFilter(e.target.value)} />
{filtered.map(p => )}

)
}

模式 2:子组件作为服务端组件

tsx
// components/client-wrapper.tsx
use client
export function ClientWrapper({ children }: { children: React.ReactNode }) {
const [isOpen, setIsOpen] = useState(false)
return (



{isOpen && children}

)
}

// app/page.tsx(服务端)
export default function Page() {
return (

{/ 仍在服务端渲染! /}

)
}

模式 3:在边界处使用 Provider

tsx
// app/providers.tsx
use client
import { ThemeProvider } from next-themes
import { QueryClient, QueryClientProvider } from @tanstack/react-query

const queryClient = new QueryClient()

export function Providers({ children }: { children: React.ReactNode }) {
return (


{children}


)
}

使用 cache() 共享数据

tsx
import { cache } from react

export const getUser = cache(async () => {
const response = await fetch(/api/user)
return response.json()
})

// 布局和页面都调用 getUser() — 只发生一次请求



数据获取

异步服务端组件

tsx
export default async function PostsPage() {
const posts = await fetch(https://api.example.com/posts).then(r => r

标签

skill ai

通过对话安装

该技能支持在以下平台通过对话安装:

OpenClaw WorkBuddy QClaw Kimi Claude

方式一:安装 SkillHub 和技能

帮我安装 SkillHub 和 nextjs-expert-1776371911 技能

方式二:设置 SkillHub 为优先技能安装源

设置 SkillHub 为我的优先技能安装源,然后帮我安装 nextjs-expert-1776371911 技能

通过命令行安装

skillhub install nextjs-expert-1776371911

下载

⬇ 下载 nextjs-expert v1.0.0(免费)

文件大小: 6.87 KB | 发布时间: 2026-4-17 14:30

v1.0.0 最新 2026-4-17 14:30
Initial release — senior-level Next.js 14/15 App Router knowledge base and coding skill.

- Covers advanced routing, layouts, Server/Client Components, authentication, middleware, data fetching, and more.
- Provides detailed file/folder conventions, lifecycle, and hands-on TypeScript-first code examples.
- Usage triggers include App Router, Server Actions, Route Handlers, NextAuth, metadata, streaming, and error boundaries.
- Emphasizes core Next.js 15 principles: server-first, minimal `'use client'`, async params, colocation, and strict typing.
- Output optimized for practical, implementation-ready code.

Archiver·手机版·闲社网·闲社论坛·羊毛社区· 多链控股集团有限公司 · 苏ICP备2025199260号-1

Powered by Discuz! X5.0   © 2024-2025 闲社网·线报更新论坛·羊毛分享社区·http://xianshe.com

p2p_official_large
返回顶部