Skip to content
Back to skills

Lazy Prefetch Pattern

ASecurity

Use the ChatJS React Query v5 lazy-prefetch pattern without blocking server rendering. Use when prefetching tRPC queries in Next.js Server Components or changing query dehydration and hydration behavior in apps/chat.

  • 1,206 stars
  • 0 votes
  • 0 copies
  • 4 views
  • Added September 2, 2026
ai-agentsreactnextjs

Works with

  • cursor
  • cli

Security analysis

A100/100

Scanned November 10, 2026

npx -y skills add FranciscoMoretti/chat-js --skill lazy-prefetch-pattern --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Lazy Prefetch Pattern?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Lazy Prefetch Pattern
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/franciscomoretti-lazy-prefetch-pattern-chat-js/badge)](https://www.skillsdirectory.com/skills/franciscomoretti-lazy-prefetch-pattern-chat-js)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: lazy-prefetch-pattern
description: Use the ChatJS React Query v5 lazy-prefetch pattern without blocking server rendering. Use when prefetching tRPC queries in Next.js Server Components or changing query dehydration and hydration behavior in apps/chat.
---

# Lazy prefetch pattern

Start optional server prefetches early without awaiting them so they can stream through React hydration without blocking rendering.

- Import `preloadQuery` from `@/trpc/preload-query` and use the request-cached `getQueryClient()` from `@/trpc/server` for consumers beneath `HydrateClient`.
- For normal queries, pass `queryClient.query(trpc.foo.bar.queryOptions(...))` to `preloadQuery`. For infinite queries, use `queryClient.infiniteQuery(trpc.foo.list.infiniteQueryOptions(...))`, including the procedure's cursor and `getNextPageParam` options.
- Start optional preloads with `void preloadQuery(...)`. The helper handles caller-facing rejection; the cache retains query errors. An already-dehydrated pending promise can still reject.
- Guard conditional preloads before invoking them. Imperative fetching does not honor `enabled: false` as a skip, and invoking `skipToken` options produces a missing-query-function error.
- Preserve existing awaited settings preloads and deliberately isolated query clients. A custom `HydrationBoundary` must dehydrate the same client used for its preloads.

```tsx
import { preloadQuery } from "@/trpc/preload-query";
import { getQueryClient, trpc } from "@/trpc/server";

const queryClient = getQueryClient();
void preloadQuery(
  queryClient.query(trpc.settings.getModelPreferences.queryOptions())
);
```

Render the consumers beneath the matching hydration boundary. `query()` applies any `select` callback while the cache retains the raw result; selector failures are handled by `preloadQuery` too.

This depends on `trpc/query-client.ts` dehydrating pending queries and using SuperJSON for serialization and hydration. Preserve those settings when using this pattern.

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in ai-agents →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.