Communitygithub.com

ASoudy/AGS

Next.js App Router 原则。服务器组件、数据获取、路由模式。

AGS 是什么?

此技能提供了 Next.js App Router 开发的全面最佳实践指南。它详细说明了服务器组件与客户端组件的决策过程,并强调了默认使用服务器组件的重要性。该技能涵盖了数据获取模式、路由原则、API 路由、性能优化、元数据管理、缓存策略和服务器操作。此外,它还列出了需要避免的反模式,并提供了一个推荐的项目结构。该技能适用于 Claude Code、Cursor、Codex 等 AI 助手,帮助开发者构建高效、可维护的 Next.js 应用程序。

兼容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/ASoudy/AGS/tree/HEAD/AGAS/plugins/antigravity-awesome-skills-claude/skills/nextjs-best-practices

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

Next.js 最佳实践

Next.js App Router 开发原则。


1. 服务器组件与客户端组件

决策树

是否需要...?
│
├── useState, useEffect, 事件处理函数
│   └── 客户端组件 ("use client")
│
├── 直接数据获取,无需交互
│   └── 服务器组件(默认)
│
└── 两者都需要?
    └── 拆分:服务器父组件 + 客户端子组件

默认情况

类型用途
服务器数据获取、布局、静态内容
客户端表单、按钮、交互式 UI

2. 数据获取模式

获取策略

模式用途
默认静态(构建时缓存)
RevalidateISR(基于时间的刷新)
No-store动态(每次请求)

数据流

来源模式
数据库服务器组件 fetch
API使用缓存进行 fetch
用户输入客户端状态 + 服务器操作

3. 路由原则

文件约定

文件用途
page.tsx路由 UI
layout.tsx共享布局
loading.tsx加载状态
error.tsx错误边界
not-found.tsx404 页面

路由组织

模式用途
路由组 (name)组织而不影响 URL
并行路由 @slot多个同级页面
拦截路由 (.)模态叠加

4. API 路由

路由处理器

方法用途
GET读取数据
POST创建数据
PUT/PATCH更新数据
DELETE删除数据

最佳实践

  • 使用 Zod 验证输入
  • 返回正确的状态码
  • 优雅地处理错误
  • 尽可能使用 Edge 运行时

5. 性能原则

图片优化

  • 使用 next/image 组件
  • 为首屏图片设置 priority
  • 提供模糊占位符
  • 使用响应式尺寸

打包优化

  • 对重型组件使用动态导入
  • 基于路由的代码拆分(自动)
  • 使用打包分析器进行分析

6. 元数据

静态与动态

类型用途
静态导出固定的元数据
generateMetadata每个路由的动态元数据

必要标签

  • title(50-60 字符)
  • description(150-160 字符)
  • Open Graph 图片
  • 规范 URL

7. 缓存策略

缓存层

层控制
请求fetch 选项
数据revalidate/tags
完整路由路由配置

重新验证

方法用途
基于时间revalidate: 60
按需revalidatePath/Tag
无缓存no-store

8. 服务器操作

使用场景

  • 表单提交
  • 数据变更
  • 重新验证触发器

最佳实践

  • 使用 "use server" 标记
  • 验证所有输入
  • 返回类型化响应
  • 处理错误

9. 反模式

❌ 不要✅ 要做
到处使用 "use client"默认使用服务器组件
在客户端组件中获取数据在服务器组件中获取数据
跳过加载状态使用 loading.tsx
忽略错误边界使用 error.tsx
大型客户端包动态导入

10. 项目结构

app/
├── (marketing)/     # 路由组
│   └── page.tsx
├── (dashboard)/
│   ├── layout.tsx   # 仪表盘布局
│   └── page.tsx
├── api/
│   └── [resource]/
│       └── route.ts
└── components/
    └── ui/

记住: 服务器组件是默认选项自有其原因。从服务器组件开始,仅在需要时添加客户端组件。

使用时机

本技能适用于执行概述中描述的工作流或操作。

限制

  • 仅当任务明确符合上述范围时,才使用此技能。
  • 不要将输出视为特定环境验证、测试或专家评审的替代品。
  • 如果缺少必需的输入、权限、安全边界或成功标准,请停止并要求澄清。

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

ASoudy/AGS

Advanced JavaScript animation library skill for creating complex, high-performance web animations.

ASoudy/AGS

Astropy is the core Python package for astronomy, providing essential functionality for astronomical research and data analysis.

ASoudy/AGS

Build content-focused websites with Astro — zero JS by default, islands architecture, multi-framework components, and Markdown/MDX support.

ASoudy/AGS

One sentence - what this skill does and when to invoke it

ASoudy/AGS

Audit and fix animation performance issues including layout thrashing, compositor properties, scroll-linked motion, and blur effects. Use when animations stutter, transitions jank, or reviewing CSS/JS animation performance.

ASoudy/AGS

Internationalization and localization patterns. Detecting hardcoded strings, managing translations, locale files, RTL support.

ASoudy/AGS

Pull a YouTube video transcript into a queryable markdown vault with yt-dlp subtitle discovery, VTT cleanup, metadata frontmatter, and capture-seed stubs.

ASoudy/AGS

Expert in building portfolios that actually land jobs and clients - not just showing work, but creating memorable experiences. Covers developer portfolios, designer portfolios, creative portfolios, and portfolios that convert visitors into opportunities.

ASoudy/AGS

Generates high-converting Next.js/React landing pages with Tailwind CSS. Uses PAS, AIDA, and BAB frameworks for optimized copy/components (Heroes, Features, Pricing). Focuses on Core Web Vitals/SEO.

ASoudy/AGS

AI-powered animation tool for creating motion in logos, UI, icons, and social media assets.

ASoudy/AGS

CRITICAL: Use for Makepad animation system. Triggers on: makepad animation, makepad animator, makepad hover, makepad state, makepad transition, "from: { all: Forward", makepad pressed, makepad 动画, makepad 状态, makepad 过渡, makepad 悬停效果

ASoudy/AGS

Comprehensive patterns for Next.js 14+ App Router architecture, Server Components, and modern full-stack React development.

ASoudy/AGS

Expert integration of Supabase Auth with Next.js App Router

ASoudy/AGS

React and Next.js 14+ application development with App Router, Server Components, TypeScript, Tailwind CSS, and modern frontend patterns.

ASoudy/AGS

Best practices for Remotion - Video creation in React

ASoudy/AGS

Generate walkthrough videos from Stitch projects using Remotion with smooth transitions, zooming, and text overlays

ASoudy/AGS

Seek and analyze video content using Memories.ai Large Visual Memory Model for persistent video intelligence

ASoudy/AGS

Writes complete, structured landing pages optimized for SEO ranking, AEO citation, and visitor conversion. Activate when the user wants to write or generate a landing page for a product, service, or offer.

ASoudy/AGS

Three.js animation - keyframe animation, skeletal animation, morph targets, animation mixing. Use when animating objects, playing GLTF animations, creating procedural motion, or blending animations.

ASoudy/AGS

Skill for discovering and researching autonomous AI agents, tools, and ecosystems using the AgentFolio directory.

相关技能