-
Notifications
You must be signed in to change notification settings - Fork 5
Fix client bundling and upgrade authkit-session to 0.3.0 #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Greptile OverviewGreptile SummaryThis PR upgrades
Confidence Score: 5/5
Important Files ChangedFile Analysis
Sequence DiagramsequenceDiagram
participant Client as Client Bundle
participant ServerFn as Server Function
participant AuthHelpers as auth-helpers.ts
participant Loader as authkit-loader.ts
participant AuthKit as @workos/authkit-session
Note over Client,AuthKit: Before (Static Import - Caused Bundling Issue)
Client->>ServerFn: Import
ServerFn->>AuthKit: Static import (bundled into client!)
AuthKit--xClient: @workos-inc/node leaked to client
Note over Client,AuthKit: After (Dynamic Import Pattern)
Client->>ServerFn: Import (safe - no static server deps)
ServerFn->>AuthHelpers: Call getRawAuthFromContext()
AuthHelpers->>Loader: await getAuthkit()
Loader->>AuthKit: await import('@workos/authkit-session')
AuthKit-->>Loader: AuthService instance (cached)
Loader-->>AuthHelpers: Return cached instance
AuthHelpers-->>ServerFn: Return auth result
ServerFn-->>Client: Response (server deps never bundled)
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
18 files reviewed, no comments
|
🙏 |
- Create src/server/context.ts with AuthKitServerContext interface - Add getAuthKitContext() and getAuthKitContextOrNull() helpers - Remove `as any` casts from production code - Rename _setPendingHeader to __setPendingHeader (deeper internal signal)
Summary
This PR upgrades
@workos/authkit-sessionto 0.3.0 and fixes critical session persistence issues.Problem: Can't Use TanStack's Built-in setResponseHeaders
TanStack Start provides
setResponseHeaders()for setting cookies from server functions. However, importing it breaks builds:This happens because TanStack's barrel exports pull in
node:streamcode that Vite can't handle. This is a known issue (TanStack/router#4022).Impact: When
refreshSession()orswitchToOrganization()is called, the session cookie can't be persisted. Users switch orgs successfully, but on page refresh they revert to their previous org.Solution: Context-Based Session Persistence
Instead of importing from
@tanstack/react-start/server, we use middleware context to defer cookie persistence:This follows the standard middleware pattern used in Express/Koa/etc.
Additional Fixes
getAuthKitContextOrNull()now gracefully handles unavailable context (e.g., after middleware completes)reloadDocumentto avoid CORS issues with external redirectsChanges
storage.ts- No TanStack server imports; uses context for headersmiddleware.ts- Passes header setter through context, applies pending headers to responseauth-helpers.ts- Returns session data for middleware to persistcontext.ts- Try-catch for graceful context accessserver-functions.ts/actions.ts- Use context-based persistenceRelated