Rule
0 installs · 0 said it worked
Cursor rules for Next.js App Router data fetching with TanStack Query
Rules file that makes the assistant follow a Next.js App Router pattern using TanStack Query v5: server prefetch with HydrationBoundary, Server Actions as mutations, optimistic updates, and query option factories.
People use this for
- nextjs app router tanstack query
- tanstack query hydration nextjs
- nextjs server prefetch client cache
- how do i hydrate tanstack query in next app router
- write nextjs data fetching with react query and server actions
- Name
cursor-rules-for-nextjs-app-router-data-fetching-with-tanstack-q- Works with
- Cursor
- License
- CC0(as observed at the source)
- Version
- v1 · updated Nov 29, 2026
Imported on Nov 29, 2026 from github.com . License observed at the source on Nov 29, 2026: CC0.
Are you the author?Claim this profileRequest removal
Use this rule
$ curl -fsSL "https://promptcommunity.dev/api/v1/posts/cursor-rules-for-nextjs-app-router-data-fetching-with-tanstack-query-fp68/export/mdc" -o .cursor/rules/cursor-rules-for-nextjs-app-router-data-fetching-with-tanstack-q.mdcThen open a new session; Cursor picks it up from .cursor/rules/.
Manual install
Save cursor-rules-for-nextjs-app-router-data-fetching-with-tanstack-q.mdc as .cursor/rules/cursor-rules-for-nextjs-app-router-data-fetching-with-tanstack-q.mdc for this project or ~/.cursor/rules/cursor-rules-for-nextjs-app-router-data-fetching-with-tanstack-q.mdc for every project, then start a new session.
Rule
description: "Next.js App Router combined with TanStack Query v5 — HydrationBoundary pattern, Server Actions as mutations, optimistic updates, and infinite scroll" 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
QueryClientper request in Server Components — never reuse across requests - Create one
QueryClientper browser session viauseStatein the provider - Always wrap server-prefetched subtrees in
HydrationBoundary - Mark all components using TanStack Query hooks with
'use client' - Never call
fetchdirectly in Client Components — always go throughqueryFn
No result recorded yet. Added it? File a Tried-it report so the next person knows how it went in your editor.
Tried it
Reports from people who added this rule to their editor. They roll up into the badges above.
No one has filed a report yet. Try the prompt and be the first.
Related rules
- Cursor rules for building Next.js App Router apps with TanStack Query v5
Cursor rules guiding the assistant on Next.js App Router data flow with TanStack Query v5: server prefetch with HydrationBoundary, Server Actions as mutations, optimistic updates, infinite queries, and when to use Server Components versus client queries.
Coding · @patrickjs
- Security guardrails that keep an AI email assistant from being hijacked by malicious messages
A full system prompt giving an AI assistant that reads or manages email a security mindset: treat email as untrusted data, require approval for outward actions, avoid leaking private data, and report phishing and injection attempts calmly.
Productivity · @thanos0000
- Convex backend coding rules for Cursor AI assistant
A Cursor rules file (.mdc) that guides the AI to write Convex backend code with current best practices: new function syntax, validators, function registration, references, pagination, schema, and query and action rules.
Coding · @patrickjs
- Nginx config security review with fixes and checker script
Agent skill that reviews nginx configs for TLS, header, alias traversal, admin exposure and rate-limit problems, runs a stdlib checker, and returns a minimal route-preserving fixed config with reload and curl test steps.
Coding · @f
- Cursor rules for writing TanStack Query v5 data-fetching code in React
Cursor rules that make the assistant an expert in TanStack Query v5 and TypeScript, covering query keys, queryOptions, mutations, optimistic updates, infinite queries, Suspense, prefetching, and error handling.
Coding · @patrickjs
License: CC0v1 · published Nov 29, 2026
Comments
No comments yet. If you tried this prompt, say how it went.