Skip to content

fix(template-generator): isolate tanstack start auth state - #1060

Merged
AmanVarshney01 merged 1 commit into
mainfrom
aman/fix-tanstack-start-auth-ssr
Jun 3, 2026
Merged

fix(template-generator): isolate tanstack start auth state#1060
AmanVarshney01 merged 1 commit into
mainfrom
aman/fix-tanstack-start-auth-ssr

Conversation

@AmanVarshney01

@AmanVarshney01 AmanVarshney01 commented May 29, 2026

Copy link
Copy Markdown
Owner

Summary

  • create request-scoped TanStack Query clients for TanStack Start oRPC/tRPC SSR so auth/query state cannot be reused across requests
  • move TanStack Router and TanStack Start Better Auth + Clerk dashboards behind pathless _auth layouts while preserving the public /dashboard URL
  • keep split-backend Better Auth dashboard session checks client-side and run payment lookup only after auth is resolved
  • keep Clerk and Clerk + Convex dashboard access gated in the _auth layout for both TanStack Router and TanStack Start

Verification

  • cd packages/template-generator && bun run build
  • bun run check
  • bun test apps/cli/test/api.test.ts apps/cli/test/auth.test.ts apps/cli/test/clerk-matrix.test.ts apps/cli/test/deployment.test.ts
  • BTS_BUILD_SAMPLES=1 BTS_BUILD_SAMPLE_FILTER=hono-trpc-drizzle-todo-bun bun test apps/cli/test/generated-builds.test.ts
  • BTS_BUILD_SAMPLES=1 BTS_BUILD_SAMPLE_FILTER=convex-clerk-react bun test apps/cli/test/generated-builds.test.ts
  • generated and built a TanStack Start + Better Auth + tRPC + Polar sample
  • generated and inspected a TanStack Start sample route tree to confirm _auth is pathless and /dashboard remains the public route

Docs checked

  • TanStack Router pathless layout routes and authenticated route pattern
  • TanStack Query SSR request-scoped QueryClient guidance
  • tRPC TanStack React Query setup guidance for SSR/full-stack frameworks
  • oRPC TanStack Start adapter docs
  • Clerk TanStack Start and React auth rendering patterns

Summary by CodeRabbit

  • New Features

    • Request-scoped query client support for improved server-side rendering isolation
    • Auth layout route restructuring for TanStack projects with Better-Auth and Clerk integration
    • Enhanced Cloudflare deployment configuration with automatic server URL binding
  • Bug Fixes

    • Improved null-safety in payment subscription checks
  • Tests

    • Added comprehensive scaffolding validation tests for TanStack Start projects with various backend configurations
    • Added Cloudflare deployment configuration tests
    • Added authentication routing and session handling tests

@vercel

vercel Bot commented May 29, 2026

Copy link
Copy Markdown

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

Project Deployment Actions Updated (UTC)
create-better-t-stack-web Ready Ready Preview, Comment Jun 3, 2026 8:21am

Request Review

@coderabbitai

coderabbitai Bot commented May 29, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

Walkthrough

Refactors query client setup to a factory (createQueryClient), moves auth/dashboard under /_auth with backend-aware loaders and SSR toggles, makes Polar subscription checks null-safe, emits Cloudflare server Worker with url binding and wires frontend server-url envs, and updates CLI tests to validate these template outputs.

Changes

Query client factory, auth routing, Cloudflare infra, and tests

Layer / File(s) Summary
All template, infra and test edits
packages/template-generator/src/templates.generated.ts, packages/template-generator/templates/**, apps/cli/test/*
Adds createQueryClient() factory and conditional module export; updates TRPC/ORPC toast retry handlers to call void query.invalidate() inside an onClick callback; relocates auth layout to /_auth and moves dashboard to /_auth/dashboard across Better-Auth and Clerk templates; TanStack Start _auth route now conditions SSR and session/payment acquisition on backend (self vs external); Cloudflare server Worker now emitted earlier with url: true and frontend server URL envs resolve from server.url! when serverDeploy === "cloudflare"; adds/updates CLI Bun tests to assert these outputs.

Possibly Related PRs

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix(template-generator): isolate tanstack start auth state' accurately reflects the main objective: creating request-scoped TanStack Query clients and restructuring auth routing for TanStack Start.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


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 and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (1)
packages/template-generator/src/templates.generated.ts (1)

1376-1393: ⚡ Quick win

Align ORPC QueryClient staleTime with the tRPC variant (to avoid SSR hydration refetch).

packages/template-generator/src/templates.generated.ts (lines 1376-1393) shows the ORPC createQueryClient() creates new QueryClient({ queryCache: ... }) without defaultOptions.queries.staleTime. In contrast, the tanstack-start tRPC factory sets defaultOptions: { queries: { staleTime: 60 * 1000 } } in packages/template-generator/templates/frontend/react/tanstack-start/src/router.tsx.hbs. Since both ORPC/tRPC use setupRouterSsrQueryIntegration, ORPC’s queries are treated as immediately stale and can refetch on hydration. Add the same defaultOptions.queries.staleTime in packages/template-generator/templates/api/orpc/web/react/base/src/utils/orpc.ts.hbs (or otherwise coordinate with the tanstack-start preload settings).


ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: b287053d-a653-4870-8912-457a8501e3d5

📥 Commits

Reviewing files that changed from the base of the PR and between 1a4a761 and 4154851.

📒 Files selected for processing (7)
  • apps/cli/test/api.test.ts
  • apps/cli/test/deployment.test.ts
  • packages/template-generator/src/templates.generated.ts
  • packages/template-generator/templates/api/orpc/web/react/base/src/utils/orpc.ts.hbs
  • packages/template-generator/templates/auth/better-auth/web/react/tanstack-start/src/routes/dashboard.tsx.hbs
  • packages/template-generator/templates/frontend/react/tanstack-start/src/router.tsx.hbs
  • packages/template-generator/templates/packages/infra/alchemy.run.ts.hbs

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: ce15e36a-e566-49b4-8590-fad9ed654c66

📥 Commits

Reviewing files that changed from the base of the PR and between 4154851 and 5a0a15b.

📒 Files selected for processing (9)
  • apps/cli/test/api.test.ts
  • apps/cli/test/auth.test.ts
  • apps/cli/test/deployment.test.ts
  • packages/template-generator/src/templates.generated.ts
  • 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
  • packages/template-generator/templates/auth/better-auth/web/react/tanstack-start/src/routes/dashboard.tsx.hbs
  • packages/template-generator/templates/frontend/react/tanstack-start/src/router.tsx.hbs
  • packages/template-generator/templates/packages/infra/alchemy.run.ts.hbs
🚧 Files skipped from review as they are similar to previous changes (6)
  • packages/template-generator/templates/api/orpc/web/react/base/src/utils/orpc.ts.hbs
  • apps/cli/test/api.test.ts
  • packages/template-generator/templates/frontend/react/tanstack-start/src/router.tsx.hbs
  • apps/cli/test/deployment.test.ts
  • packages/template-generator/templates/packages/infra/alchemy.run.ts.hbs
  • packages/template-generator/src/templates.generated.ts

Comment thread apps/cli/test/auth.test.ts Outdated

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
packages/template-generator/templates/auth/better-auth/web/react/tanstack-router/src/routes/_auth/dashboard.tsx.hbs (1)

31-32: ⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Remove the subscription debug log.

customerState?.activeSubscriptions is account/payment state, and this console.log will ship in every generated app.

Suggested fix
  const hasProSubscription = customerState?.activeSubscriptions?.length! > 0
-    console.log("Active subscriptions:", customerState?.activeSubscriptions)
♻️ Duplicate comments (1)
apps/cli/test/auth.test.ts (1)

216-218: ⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Assert marker presence before comparing order.

Line 216-218 can pass when one marker is missing because indexOf() may return -1. Add existence checks before the ordering assertion.

Suggested fix
-      expect(authRouteFile.indexOf("if (!session)")).toBeLessThan(
-        authRouteFile.indexOf("const customerState = await getPayment();"),
-      );
+      const guardIndex = authRouteFile.indexOf("if (!session)");
+      const paymentIndex = authRouteFile.indexOf(
+        "const customerState = await getPayment();",
+      );
+
+      expect(guardIndex).toBeGreaterThanOrEqual(0);
+      expect(paymentIndex).toBeGreaterThanOrEqual(0);
+      expect(guardIndex).toBeLessThan(paymentIndex);

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: e3c2b8ca-3f64-47bb-8b88-5c7ba5a23e29

📥 Commits

Reviewing files that changed from the base of the PR and between 5a0a15b and 904f30e.

📒 Files selected for processing (27)
  • apps/cli/test/api.test.ts
  • apps/cli/test/auth.test.ts
  • apps/cli/test/clerk-matrix.test.ts
  • apps/cli/test/deployment.test.ts
  • packages/template-generator/src/templates.generated.ts
  • 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
  • packages/template-generator/templates/auth/better-auth/convex/web/react/tanstack-router/src/routes/_auth/dashboard.tsx.hbs
  • packages/template-generator/templates/auth/better-auth/convex/web/react/tanstack-router/src/routes/_auth/route.tsx.hbs
  • packages/template-generator/templates/auth/better-auth/convex/web/react/tanstack-start/src/routes/_auth/dashboard.tsx.hbs
  • packages/template-generator/templates/auth/better-auth/convex/web/react/tanstack-start/src/routes/_auth/route.tsx.hbs
  • packages/template-generator/templates/auth/better-auth/web/react/tanstack-router/src/routes/_auth/dashboard.tsx.hbs
  • packages/template-generator/templates/auth/better-auth/web/react/tanstack-router/src/routes/_auth/route.tsx.hbs
  • packages/template-generator/templates/auth/better-auth/web/react/tanstack-start/src/routes/_auth/dashboard.tsx.hbs
  • packages/template-generator/templates/auth/better-auth/web/react/tanstack-start/src/routes/_auth/route.tsx.hbs
  • packages/template-generator/templates/auth/clerk/convex/web/react/tanstack-router/src/routes/_auth/dashboard.tsx.hbs
  • packages/template-generator/templates/auth/clerk/convex/web/react/tanstack-router/src/routes/_auth/route.tsx.hbs
  • packages/template-generator/templates/auth/clerk/convex/web/react/tanstack-router/src/routes/dashboard.tsx.hbs
  • packages/template-generator/templates/auth/clerk/convex/web/react/tanstack-start/src/routes/_auth/dashboard.tsx.hbs
  • packages/template-generator/templates/auth/clerk/convex/web/react/tanstack-start/src/routes/_auth/route.tsx.hbs
  • packages/template-generator/templates/auth/clerk/convex/web/react/tanstack-start/src/routes/dashboard.tsx.hbs
  • packages/template-generator/templates/auth/clerk/web/react/tanstack-router/src/routes/_auth/dashboard.tsx.hbs
  • packages/template-generator/templates/auth/clerk/web/react/tanstack-router/src/routes/_auth/route.tsx.hbs
  • packages/template-generator/templates/auth/clerk/web/react/tanstack-start/src/routes/_auth/dashboard.tsx.hbs
  • packages/template-generator/templates/auth/clerk/web/react/tanstack-start/src/routes/_auth/route.tsx.hbs
  • packages/template-generator/templates/frontend/react/tanstack-start/src/router.tsx.hbs
  • packages/template-generator/templates/packages/infra/alchemy.run.ts.hbs
💤 Files with no reviewable changes (2)
  • packages/template-generator/templates/auth/clerk/convex/web/react/tanstack-router/src/routes/dashboard.tsx.hbs
  • packages/template-generator/templates/auth/clerk/convex/web/react/tanstack-start/src/routes/dashboard.tsx.hbs
✅ Files skipped from review due to trivial changes (4)
  • packages/template-generator/templates/auth/better-auth/convex/web/react/tanstack-start/src/routes/_auth/route.tsx.hbs
  • packages/template-generator/templates/auth/clerk/web/react/tanstack-start/src/routes/_auth/route.tsx.hbs
  • packages/template-generator/templates/auth/clerk/convex/web/react/tanstack-start/src/routes/_auth/dashboard.tsx.hbs
  • packages/template-generator/templates/api/orpc/web/react/base/src/utils/orpc.ts.hbs
🚧 Files skipped from review as they are similar to previous changes (4)
  • apps/cli/test/api.test.ts
  • apps/cli/test/deployment.test.ts
  • packages/template-generator/templates/packages/infra/alchemy.run.ts.hbs
  • packages/template-generator/templates/frontend/react/tanstack-start/src/router.tsx.hbs

@AmanVarshney01
AmanVarshney01 force-pushed the aman/fix-tanstack-start-auth-ssr branch from 258494c to 6c80944 Compare June 3, 2026 08:21
@AmanVarshney01
AmanVarshney01 merged commit 5ef5596 into main Jun 3, 2026
3 checks passed
@AmanVarshney01
AmanVarshney01 deleted the aman/fix-tanstack-start-auth-ssr branch June 3, 2026 16:11
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.

1 participant