Skip to content

refactor(tanstack-start): reuse TanStack request context - #2191

Open
bgub wants to merge 1 commit into
mainfrom
bg/simplify-tanstack-request-context
Open

refactor(tanstack-start): reuse TanStack request context#2191
bgub wants to merge 1 commit into
mainfrom
bg/simplify-tanstack-request-context

Conversation

@bgub

@bgub bgub commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

TL;DR

Reuse TanStack Start's existing request context instead of maintaining a second AsyncLocalStorage-backed condition store. Server translation helpers now resolve and memoize conditions directly from TanStack's current Request, so gtMiddleware is optional.

Code Changes

  • Remove AsyncLocalConditionStore and its singleton plumbing.
  • Resolve server conditions through TanStack's getRequest() and memoize them per request.
  • Share the small request cache/config record across package bundle instances.
  • Collapse the runtime helpers onto one isomorphic condition lookup.
  • Delegate client locale initialization to the existing shared browser condition store.
  • Reduce parseLocale() to a deprecated alias for getLocale().
  • Retain and deprecate gtMiddleware as a compatibility/eager-warming hook.
  • Add focused request isolation, memoization, bundle-sharing, and middleware coverage.

Notes / Flags

  • No public API is removed; existing gtMiddleware registrations continue to work.
  • Main entry gzip sizes improve from 3.35 kB to 2.94 kB on the client and 3.27 kB to 2.72 kB on the server. The deprecated /server compatibility entry grows from 1.66 kB to 2.47 kB because it now includes direct request resolution.
  • Verified with formatting, Oxlint, TypeScript, 29 package tests, and the production package build.

Greptile Summary

Summary

This change replaces the TanStack Start adapter’s async-local request state with conditions resolved from TanStack’s active request, memoized per request, and shared across package instances. It also makes gtMiddleware an optional compatibility hook and uses the shared React browser condition store during client initialization.

Focused execution disproved the relevant failure hypotheses: runtime helpers resolved the active request without middleware, separate requests retained separate locale and enablement values while repeated reads reused the same conditions, and a path locale did not override the cookie locale when locale routing was disabled. The related request-condition and runtime tests passed, and the package typecheck completed successfully.

No defects were found.

Confidence Score: 5/5

The changed TanStack Start request-condition behavior is safe to merge based on the exercised request-resolution, isolation, memoization, and locale-routing paths.

Focused executable coverage exercised the key behavior introduced by this change, while the adjacent changed-module tests and package typecheck also completed successfully. No independently actionable defects remain.

Files Needing Attention: No files need follow-up attention. The exercised implementation paths were packages/tanstack-start/src/functions/requestConditions.ts and packages/tanstack-start/src/functions/runtime.ts.

T-Rex T-Rex Logs

What T-Rex did

  • Ran a focused Vitest test against the changed request-condition and runtime modules.
  • Validated runtime helpers read locale and enablement from the mocked active getRequest() without middleware; distinct Request objects produced distinct conditions; repeated reads reused conditions; and locale routing disabled left the cookie locale in effect for /fr/about.
  • The focused test passed all three scenarios; the adjacent request-condition and runtime suites passed 10 tests; and the TanStack Start package typecheck completed successfully.
  • Validated locale-resolution and per-request condition behavior: runtime.ts resolved locale from the mock, requestConditions.ts produced separate conditions per Request and reused on repeats, and localeRouting=false prevented consulting /fr/about with the locale cookie es.
  • Artifacts documenting the validation sources and test results were prepared for review.

View all artifacts

T-Rex Ran code and verified through T-Rex

Reviews (1): Last reviewed commit: "refactor(tanstack-start): reuse TanStack..." | Re-trigger Greptile

@bgub
bgub requested a review from a team as a code owner August 26, 2026 01:26
@github-actions

Copy link
Copy Markdown
Contributor

size-limit report 📦

Path Size
generaltranslation 18.5 KB (0%)
generaltranslation/runtime 15.28 KB (0%)
generaltranslation/id 2.55 KB (0%)
generaltranslation/internal 7.34 KB (0%)
generaltranslation/types 115 B (0%)
generaltranslation/errors 81 B (0%)
@generaltranslation/format 9.92 KB (0%)
@generaltranslation/format/types 89 B (0%)
@generaltranslation/format/internal 880 B (0%)
gt-i18n 11.97 KB (0%)
gt-i18n/types 13 B (0%)
gt-i18n/internal 22.83 KB (0%)
gt-i18n/internal/cookies 228 B (0%)
gt-i18n/internal/string 2.9 KB (0%)
gt-i18n/internal/types 13 B (0%)
@generaltranslation/react-core/pure 26.46 KB (0%)
@generaltranslation/react-core/hooks 21.08 KB (0%)
@generaltranslation/react-core/components 23.52 KB (0%)
@generaltranslation/react-core/components-rsc 26.93 KB (0%)
gt-react (client) 32.86 KB (0%)
gt-react (rsc) 29.47 KB (0%)
gt-react (server) 32.7 KB (0%)
gt-react/macros 8.82 KB (0%)
gt-vue 9.7 KB (0%)
gt-vue (SPA) 19.25 KB (0%)
gt-next (client) 45.05 KB (0%)
gt-next (rsc) 49.32 KB (0%)
gt-next (server) 45.38 KB (0%)
gt-next/config 271.07 KB (0%)
gt-next/server 47.81 KB (0%)
gt-next/middleware 37.84 KB (0%)
gt-next/link 43.92 KB (0%)
gt-next/internal/_dictionary 144 B (0%)
gt-next/internal/_load-translations 144 B (0%)
gt-next/internal/_load-dictionary 144 B (0%)
gt-next/internal/_getLocale 125 B (0%)
gt-next/internal/_getRegion 122 B (0%)
gt-node 23.99 KB (0%)
gt-node/types 219 B (0%)
gt-node/internal 13.4 KB (0%)
gt-tanstack-start (client) 32.36 KB (-0.79% 🔽)
gt-tanstack-start (server) 32.75 KB (-1.05% 🔽)
gt-tanstack-start/server 20.67 KB (+101.65% 🔺)
gt-react-native 31.12 KB (0%)
gt-react-native/plugin 4.72 KB (0%)
gt-react-native/internal 746 B (0%)

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