Skip to content

fix(templates): create TanStack Query client per request in Next and SvelteKit - #1295

Merged
AmanVarshney01 merged 7 commits into
mainfrom
fix/per-request-query-client
Oct 22, 2026
Merged

AmanVarshney01 merged 7 commits into
mainfrom
fix/per-request-query-client

Conversation

@AmanVarshney01

@AmanVarshney01 AmanVarshney01 commented Oct 22, 2026 •

Copy link
Copy Markdown
Owner

Summary

Generated SSR apps shared one module-level TanStack QueryClient between server renders, so cached query data could leak across users' requests. The server now creates a new client for each request. The browser still keeps a single client.

Fixes #895

Changes

  • Next + tRPC: utils/trpc.ts now exports TRPCProvider/useTRPC (createTRPCContext), makeTRPCClient(), and getQueryClient(), following tRPC's App Router setup. Server code gets a new client on every call; the browser reuses one singleton (environmentManager.isServer()). providers.tsx creates the tRPC client once with useState(makeTRPCClient), wraps children in TRPCProvider, and pages call useTRPC(). This matches the TanStack Start template.
  • Next + oRPC (self and separate backend): utils/orpc.ts exports getQueryClient() in place of the singleton queryClient, and providers.tsx uses it.
  • SvelteKit + oRPC: lib/orpc.ts exports createQueryClient() with enabled: browser. The client is created in +layout.svelte, so each server render gets its own client.
  • No change for TanStack Start, Solid or Nuxt, which already create a client per request. React Router (ssr: false), TanStack Router and native apps run only on the client, so their singleton stays.

Docs followed: TanStack Query's Advanced SSR guide (Next.js App Router getQueryClient), tRPC's TanStack React Query setup and server-components docs, oRPC's TanStack Query integration docs, and TanStack Query's Svelte SSR docs.

Verification

  • Generated with better-auth, todo and SQLite: next+hono+trpc, next+hono+orpc, next self+trpc, next self+orpc, svelte+hono+orpc and svelte self+orpc. All passed bun install, check-types and build.
  • bun run test:live run: next/self orpc, next/self trpc, next/hono trpc and svelte/hono orpc passed. svelte/self orpc failed on its first run with one aborted SvelteKit __data.json invalidation request (net::ERR_ABORTED). It passed on retry and on 3 later runs from fresh run directories.
  • Generated-build samples: next 9/9 passed and svelte 14/14 passed.
  • bun run check and test:ci (112 tests) passed.

Summary by CodeRabbit

  • Improvements
    • Generated Next.js apps now create query clients appropriately for server and browser rendering and use updated tRPC provider and client access.
    • Generated Svelte apps create query clients on demand and keep query execution disabled outside the browser.
    • Dashboard, todo, and health-check examples use the updated tRPC client access pattern.
  • Bug Fixes
    • Svelte todo and health-check examples now use the pending state to display loading status.

@vercel

vercel Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
create-better-t-stack-web Ready Ready Preview Oct 22, 2026 7:51pm UTC

Request Review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-22T08:15:05.708382Z 9cafbd9 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@AmanVarshney01

Copy link
Copy Markdown
Owner Author

Follow-up on the one SvelteKit fullstack live-test failure (net::ERR_ABORTED on a page-data request): I re-ran svelte/self + oRPC + Better Auth + todo with a fresh run directory each time.

  • This branch: 6 of 6 fresh runs passed (10 of 11 counting the earlier runs).
  • main: 5 of 5 fresh runs passed.

The single failure was a SvelteKit page-data fetch that a newer navigation cancelled. It hasn't recurred in 10 later runs, which points to timing rather than this change. Still, it's a 1-in-11 data point, so I'm not calling it resolved.

@coderabbitai

coderabbitai Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Important

Review skipped

Review was skipped as selected files did not have any reviewable changes.

⛔ Files ignored due to path filters: 1
  • packages/template-generator/src/templates.generated.ts is excluded by !**/*.generated.*
⚙️ Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 2b9ce48d-1997-4663-a70f-2c6fe8013cd4
📥 Commits

Reviewing files that changed from the base of the PR and between 0d65d0c and e3bfbd1.

⛔ Files ignored due to path filters: 1
  • packages/template-generator/src/templates.generated.ts is excluded by !**/*.generated.*

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 5ae48651-bf73-471c-8450-f58cd3726371
📥 Commits

Reviewing files that changed from the base of the PR and between ba66a85 and b6d2b30.

⛔ Files ignored due to path filters: 1
  • packages/template-generator/src/templates.generated.ts is excluded by !**/*.generated.*
📒 Files selected for processing: 2
  • packages/template-generator/templates/api/trpc/web/react/base/src/utils/trpc.ts.hbs
  • packages/template-generator/templates/frontend/react/next/src/components/providers.tsx.hbs

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 4 remain after this review.

Walkthrough

The templates change query-client creation in React and Svelte. Next.js tRPC consumers use context hooks and a provider. Svelte loading checks use isPending.

Changes

Query Client Lifecycle

Layer / File(s) Summary
React query-client setup
packages/template-generator/templates/api/orpc/web/react/base/src/utils/orpc.ts.hbs, packages/template-generator/templates/api/trpc/web/react/base/src/utils/trpc.ts.hbs
The Next.js oRPC template exports getQueryClient(), which creates a new server client and reuses a browser client. The tRPC template adds makeQueryClient(), getQueryClient(), and context provider and hook exports. It replaces the module-level tRPC client with makeTRPCClient().
Next.js tRPC integration
packages/template-generator/templates/frontend/react/next/src/components/providers.tsx.hbs, packages/template-generator/templates/auth/{better-auth,clerk}/web/react/next/src/app/..., packages/template-generator/templates/examples/todo/web/react/next/src/app/todos/page.tsx.hbs, packages/template-generator/templates/frontend/react/next/src/app/page.tsx.hbs
The provider obtains the query client and wraps children with TRPCProvider for tRPC. Dashboard, todo, and home templates obtain the tRPC client through useTRPC().
Svelte query-client factory and loading checks
packages/template-generator/templates/api/orpc/web/svelte/src/lib/orpc.ts.hbs, packages/template-generator/templates/frontend/svelte/src/routes/+layout.svelte.hbs, packages/template-generator/templates/examples/todo/web/svelte/src/routes/todos/+page.svelte.hbs, packages/template-generator/templates/frontend/svelte/src/routes/+page.svelte.hbs
The oRPC template replaces its exported singleton with createQueryClient(), which preserves the error handler and enables queries only in the browser. The layout calls the factory to initialize its provider client. Todo and API-status loading checks use isPending.

Priority: ➖ Normal

Severity of issue fixed: Medium

Merge Risk: ⚪ Minimal · up to b6d2b

The inspected template changes show no actionable issue blocking merge after normal checks.

Pre-merge checks: Passed 5
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: request-scoped TanStack Query clients for Next.js and SvelteKit templates.
Linked Issues check Passed Issue [#895] requires isolated QueryClient instances for SSR requests. The Next.js tRPC and oRPC templates now create a server client per request and reuse one browser client. The SvelteKit oRPC tem…
Out of Scope Changes check Passed The changes remain within issue [#895]. Provider changes and query call-site changes implement the new client lifecycle. The Svelte isPending changes support the loading state when SSR disables quer…
Docstring Coverage Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 0…
Full details: Linked Issues check

Explanation

Issue [#895] requires isolated QueryClient instances for SSR requests. The Next.js tRPC and oRPC templates now create a server client per request and reuse one browser client. The SvelteKit oRPC template now creates a client in +layout.svelte, so each server render receives a new client. The reported type checks, builds, generated-app checks, and repository tests passed. The unchanged templates are identified as already request-scoped or client-only.

Full details: Out of Scope Changes check

Explanation

The changes remain within issue [#895]. Provider changes and query call-site changes implement the new client lifecycle. The Svelte isPending changes support the loading state when SSR disables queries. The reported verification changes validate the affected generated templates and repository behavior.

Full details: Docstring Coverage

Explanation

No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 0 files. (2 skipped: 2 unsupported.)

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Queries are disabled during SvelteKit SSR, so isLoading is false and the
server HTML rendered "Disconnected" and "No todos yet." before hydration.
Use isPending so the server and first client render show the loading state.
Follows tRPC's App Router setup: utils/trpc exports makeTRPCClient()
and the Providers component creates the client once with useState,
next to the per-request query client.
@AmanVarshney01
AmanVarshney01 merged commit 517578b into main Oct 22, 2026
11 checks passed
@AmanVarshney01
AmanVarshney01 deleted the fix/per-request-query-client branch October 22, 2026 20:01

This branch was successfully deployed

1 active deployment
Preview — e3bfbd16 Deployed Oct 22, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] QueryClient singleton exports could cause data isolation issues with SSR

1 participant