跳到正文
Skillpedia
API 与服务器(后端)41k · #6699

nextjs-app-router-patterns

它能做什么

本指南介绍如何使用 Next.js 的最新特性来构建现代 Web 应用。内容包括如何在服务器端或浏览器端运行代码、如何在页面其他部分加载时先展示已就绪的部分,以及如何组织你的文件。

适用场景

  • ✓开始一个新的 Next.js 项目时。
  • ✓把旧项目迁移到 Next.js 的新特性时。
  • ✓你希望通过逐步展示内容,让页面加载得更快时。

技能链接

https://github.com/wshobson/agents/blob/main/plugins/frontend-mobile-development/skills/nextjs-app-router-patterns/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/wshobson/agents/blob/main/plugins/frontend-mobile-development/skills/nextjs-app-router-patterns/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“我该如何设置 Next.js 项目,让它支持服务端组件?”

每周 5 个新技能,1 分钟读完。

工作原理

文章会讲解不同类型的组件(服务端组件和客户端组件)以及它们的工作方式。它还会演示如何组织应用的文件结构,以符合 Next.js 的最佳实践。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Next.js App Router Patterns

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

When to Use This Skill

  • Building new Next.js applications with App Router
  • Migrating from Pages Router to App Router
  • Implementing Server Components and streaming
  • Setting up parallel and intercepting routes
  • Optimizing data fetching and caching
  • Building full-stack features with Server Actions

Core Concepts

1. Rendering Modes

Mode Where When to Use
Server Components Server only Data fetching, heavy computation, secrets
Client Components Browser Interactivity, hooks, browser APIs
Static Build time Content that rarely changes
Dynamic Request time Personalized or real-time data
Streaming Progressive Large pages, slow data sources

2. File Conventions

app/
├── layout.tsx       # Shared UI wrapper
├── page.tsx         # Route UI
├── loading.tsx      # Loading UI (Suspense)
├── error.tsx        # Error boundary
├── not-found.tsx    # 404 UI
├── route.ts         # API endpoint
├── template.tsx     # Re-mounted layout
├── default.tsx      # Parallel route fallback
└── opengraph-image.tsx  # OG image generation

Quick Start

// app/layout.tsx
import { Inter } from 'next/font/google'
import { Providers } from './providers'

const inter = Inter({ subsets: ['latin'] })

export const metadata = {
  title: { default: 'My App', template: '%s | My App' },
  description: 'Built with Next.js App Router',
}

export default function RootLayout({
  children,
}: {
  children: React.ReactNode
}) {
  return (
    <html lang="en" suppressHydrationWarning>
      <body className={inter.className}>
        <Providers>{children}</Providers>
      </body>
    </html>
  )
}

// app/page.tsx - Server Component by default
async function getProducts() {
  const res = await fetch('https://api.example.com/products', {
    next: { revalidate: 3600 }, // ISR: revalidate every hour
  })
  return res.json()
}

export default async function HomePage() {
  const products = await getProducts()

  return (
    <main>
      <h1>Products</h1>
      <ProductGrid products={products} />
    </main>
  )
}

Detailed patterns and worked examples

Detailed pattern documentation lives in references/details.md. Read that file when the navigation tier above is insufficient.

Best Practices

Do's

  • Start with Server Components - Add 'use client' only when needed
  • Colocate data fetching - Fetch data where it's used
  • Use Suspense boundaries - Enable streaming for slow data
  • Leverage parallel routes - Independent loading states
  • Use Server Actions - For mutations with progressive enhancement

Don'ts

  • Don't pass serializable data - Server → Client boundary limitations
  • Don't use hooks in Server Components - No useState, useEffect
  • Don't fetch in Client Components - Use Server Components or React Query
  • Don't over-nest layouts - Each layout adds to the component tree
  • Don't ignore loading states - Always provide loading.tsx or Suspense
常见问题

什么是 Server Components(服务端组件)?

这些是只在服务器上运行的代码部分,非常适合获取数据或处理敏感信息。

什么是“流式传输”(streaming)?

这是一种技术,让网页的各个部分在准备就绪后立即显示,而不必等待整个页面加载完成。

什么是“并行路由”(parallel routes)?

它们允许页面的不同部分独立加载,从而改善用户体验。

详情

作者的原始描述

Master Next.js 14+ App Router with Server Components, streaming, parallel routes, and advanced data fetching. Use when building Next.js applications, implementing SSR/SSG, or optimizing React Server Components.

repo: wshobson/agents

热度

GitHub 星标
41k
排名
#6699
许可证
MIT
更新时间
2026/10/15

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能