data-fetching

data-fetching is a cursor rule for coding agents from kensaurus/cursor-kenji. It costs 0 tokens per session (563 once invoked), scanned A, original, MIT.

Data-fetching rules explain when to query Supabase or Prisma directly, when to use TanStack Query, and how to handle forms, mutations, caching, and real-time updates. TanStack Query is a library for managing data loaded by a web app.

In plain words
What is it for?
Use them when loading data in server or client components, submitting forms, handling updates, subscribing to live changes, or defining query keys.
Why use it?
They prevent each part of an application from fetching and caching data in a different way.

Cursor rule

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add rules/kensaurus/cursor-kenji/data-fetching
Clone the repo
git clone --depth 1 https://github.com/kensaurus/cursor-kenji

Wrote this? Show the measurements

A badge with what this costs and how it scanned, read live from this page, so it follows the numbers instead of freezing them. Markdown for a README, HTML for a documentation site or a project page.

agentmods badge for data-fetching

README.md
[![agentmods](https://agentmods.dev/badge/rules/kensaurus/cursor-kenji/data-fetching.svg)](https://agentmods.dev/rules/kensaurus/cursor-kenji/data-fetching)
Your own site
<a href="https://agentmods.dev/rules/kensaurus/cursor-kenji/data-fetching"><img src="https://agentmods.dev/badge/rules/kensaurus/cursor-kenji/data-fetching.svg" alt="Measured on agentmods" height="20"></a>
Per session 0 Nothing until a file matches its globs; then the whole rule loads.
When invoked 563 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00000 $0.00563
Opus 5 $0.00000 $0.00282
Sonnet 5 $0.00000 $0.00113
Haiku 4.5 $0.00000 $0.00056

Measured yesterday against content hash 12601761439d, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

data-fetching scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured yesterday.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

rules/project-starter/data-fetching.mdc · 78 lines

How it starts

The opening of the file, as written. The whole thing — 78 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Data Fetching Conventions

Decision Matrix

Context Pattern
Server Component (initial load) Direct Supabase/Prisma query
Client Component (read) TanStack Query useQuery
Client Component (mutation) TanStack Query useMutation or Server Action
Form submission Server Action with useActionState
Real-time updates Supabase Realtime subscription
URL-driven state nuqs useQueryStates

TanStack Query Patterns

// Query with proper key factory
const queryKeys = {
  all: ['features'] as const,
  lists: () => [...queryKeys.all, 'list'] as const,
  list: (filters: Filters) => [...queryKeys.lists(), filters] as const,
  details: () => [...queryKeys.all, 'detail'] as const,
  detail: (id: string) => [...queryKeys.details(), id] as const,
}

// Hook
export function useFeatures(filters: Filters) {
  return useQuery({
    queryKey: queryKeys.list(filters),
    queryFn: () => fetchFeatures(filters),
    staleTime: 5 * 60 * 1000, // 5 minutes
  })
}

Next.js 16 caching

Caching is explicit: 'use cache' + cacheComponents (static shell / dynamic holes). Do not assume implicit fetch caching. Turbopack is default for dev and build. React Compiler: reactCompiler: true.

Server Component Prefetching

// Prefetch in Server Component, hydrate in Client
import { HydrationBoundary, dehydrate } from '@tanstack/react-query'

export default async function Page() {
  const queryClient = new QueryClient()
  await queryClient.prefetchQuery({
    queryKey: ['features'],
    queryFn: fetchFeatures,
  })

  return (
    <HydrationBoundary state={dehydrate(queryClient)}>
      <FeatureList />
    </HydrationBoundary>
  )
}

Error Handling

  • Always handle error state from useQuery
  • Show <ErrorState> component with retry button
  • Mutations: use onSuccess for cache invalidation, onError for toast

Caching

  • Set staleTime based on data freshness needs
  • Use queryClient.invalidateQueries after mutations
  • Use revalidatePath / revalidateTag in Server Actions

Read the full file on GitHub · 78 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. yesterday First seen · 78 lines · 0 tokens per session scan A 12601761439d

Subscribe to this mod's changes

data-fetching is a cursor rule published in the GitHub repository kensaurus/cursor-kenji (9 stars, last pushed 7d ago), licensed MIT. It costs nothing until one of its globs matches a file; then it loads 563 tokens. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-09-03.