Cursor Next.js 编码规范

Model: qwen-max | ¥0.15/call
AI工具GPT-4.1智能助手CursorNext.js

Cursor Next.js 编码规范:来自 PatrickJS/awesome-cursorrules (40k stars) 的 nex,适用于各类文档与内容的智能化处理。

Calls: 1

Skill Documentation

Cursor Next.js 编码规范

摘要

Cursor Next.js 编码规范:来自 PatrickJS/awesome-cursorrules (40k stars) 的 nex,适用于各类文档与内容的智能化处理。

**文件来源:** PatrickJS/awesome-cursorrules → `rules/nextjs-tanstack-query.mdc`

**原仓库:** https://github.com/PatrickJS/awesome-cursorrules

**评分:** ⭐ 仓库 40k stars (社区最权威 Cursor rules 合集)

这个 skill 是干什么的?

把 `nextjs-tanstack-query.mdc` 这条 Cursor 编码规则打包成可调用的 AI skill,帮你把代码生成统一到一致的标准上。

> Next.js App Router combined with TanStack Query v5 — HydrationBoundary pattern, Server Actions as mutations, optimistic updates, and infinite scroll

🤖 Agent 使用说明

👤 用户需要做什么?

适用场景

原始规则内容

globs: ["app/**/*.tsx", "app/**/*.ts", "src/app/**/*.tsx", "src/queries/**/*"]
alwaysApply: false
---
You are an expert in Next.js App Router, TanStack Query v5, TypeScript, and combining server components with client-side data fetching.

## Architecture
- Server Components fetch data directly — no TanStack Query needed there
- TanStack Query lives in Client Components for interactive, real-time, or mutation-driven data
- Hydrate the Query cache from server to avoid client waterfalls on first load
- Use React Server Components for initial page data; TanStack Query for mutations + polling + optimistic UI

## Provider Setup

// providers/query-provider.tsx

'use client'

export function QueryProvider({ children }: { children: React.ReactNode }) {

const [queryClient] = useState(() => new QueryClient({

defaultOptions: { queries: { staleTime: 60 * 1000 } },

}))

return (

<QueryClientProvider client={queryClient}>

{children}

<ReactQueryDevtools initialIsOpen={false} />

</QueryClientProvider>

)

}


## Server Prefetch + HydrationBoundary Pattern

// app/posts/page.tsx (Server Component)

export default async function PostsPage() {

const queryClient = new QueryClient()

await queryClient.prefetchQuery(postsQueryOptions())

return (

<HydrationBoundary state={dehydrate(queryClient)}>

<PostsList />

</HydrationBoundary>

)

}

// app/posts/_components/posts-list.tsx (Client Component)

'use client'

export function PostsList() {

const { data: posts } = useQuery(postsQueryOptions()) // reads from pre-populated cache

return <ul>{posts?.map(p => <li key={p.id}>{p.title}</li>)}</ul>

}


## queryOptions Factory

export const postsQueryOptions = (filters?: PostFilters) =>

queryOptions({

queryKey: ['posts', 'list', filters],

queryFn: () => fetch('/api/posts').then(r => r.json()),

})


## Server Actions as mutationFn

// app/posts/actions.ts

'use server'

export async function createPost(data: { title: string; body: string }) {

const post = await db.post.create({ data })

revalidatePath('/posts')

return post

}

// usage in Client Component

const mutation = useMutation({

mutationFn: createPost,

onSuccess: () => queryClient.invalidateQueries({ queryKey: ['posts', 'list'] }),

})


## Optimistic Updates

const mutation = useMutation({

mutationFn: updatePost,

onMutate: async (updated) => {

await queryClient.cancelQueries({ queryKey: ['posts', 'detail', updated.id] })

const previous = queryClient.getQueryData(['posts', 'detail', updated.id])

queryClient.setQueryData(['posts', 'detail', updated.id], (old: Post) => ({ ...old, ...updated }))

return { previous }

},

onError: (_, updated, ctx) => {

queryClient.setQueryData(['posts', 'detail', updated.id], ctx?.previous)

},

onSettled: (_, __, updated) => {

queryClient.invalidateQueries({ queryKey: ['posts', 'detail', updated.id] })

},

})


## Key Rules
- Create a **new** `QueryCl

...(完整内容在原仓库)...

数据来源

联系方式

有问题或建议,在本 skill 下留言。

FAQ

这个 skill 是干什么的?

把 `nextjs-tanstack-query.mdc` 这条 Cursor 编码规则打包成可调用的 AI skill,帮你把代码生成统一到一致的标准上。

> Next.js App Router combined with TanStack Query v5 — HydrationBoundary pattern, Server Actions as mutations, optimistic updates, and infinite scroll

👤 用户需要做什么?
  • [ ] 知道这条规则适合用在什么场景(参考下面"适用场景")
  • [ ] 把规则原文内容应用到 IDE 项目的 `.cursor/rules/` 目录(直接复制 .mdc 文件)
  • [ ] 调本 skill 时说清楚你的代码任务(语言/框架/目标)
  • [ ] 输出后人工 review 风格是否符合预期