Skip to content

fix(client): remove useNavigate from AuthKitProvider to avoid SSR warning#59

Open
nicknisi wants to merge 1 commit intomainfrom
fix/issue-57
Open

fix(client): remove useNavigate from AuthKitProvider to avoid SSR warning#59
nicknisi wants to merge 1 commit intomainfrom
fix/issue-57

Conversation

@nicknisi
Copy link
Member

Summary

AuthKitProvider called useNavigate() unconditionally at the top of the component body. During SSR with TanStack Start, the Wrap component renders before RouterProvider context is established, causing repeated useRouter must be used inside a <RouterProvider> console warnings.

This PR removes useNavigate entirely from AuthKitProvider. The handleSignOut fallback navigation now uses window.location.href assignment instead of TanStack Router's navigate(), which is safe since sign-out is always a user-triggered client-side action.

Changes

  • src/client/AuthKitProvider.tsx: Removed useNavigate import and hook call. Replaced navigate({ to: returnTo }) with window.location.href = returnTo in the handleSignOut fallback path.
  • src/client/AuthKitProvider.spec.tsx: Removed global @tanstack/react-router mock. Added regression test that renders AuthKitProvider without RouterProvider context (would throw if useNavigate() were still called unconditionally). Updated sign-out tests to verify window.location.href assignment.

Closes #57

What was tested

Automated (176 tests pass):

  • All 16 AuthKitProvider.spec.tsx tests pass, including the new regression test "renders without router context (no useNavigate SSR warning)"
  • pnpm build succeeds with no type errors
  • No useNavigate references remain in source or built dist/client/ output
  • No @tanstack/react-router imports in dist/client/ output

Manual (Playwright E2E):

  • Started example app on localhost:3000
  • Navigated to home page and account page
  • Console output: 0 warnings (only a harmless favicon 404)
  • No useRouter must be used inside a <RouterProvider> warning observed

Manual reproduction steps

To reproduce the bug on main:

  1. Check out main and run pnpm install && pnpm build
  2. cd example && pnpm dev
  3. Open http://localhost:3000 in a browser with DevTools console open
  4. Observe: Repeated useRouter must be used inside a <RouterProvider> warnings in the console during SSR/hydration

To verify the fix on this branch:

  1. Check out fix/issue-57 and run pnpm install && pnpm build
  2. cd example && pnpm dev
  3. Open http://localhost:3000 in a browser with DevTools console open
  4. Observe: No useRouter warnings in the console

Verification

verified-no-warnings.png

Follow-ups

  • @tanstack/react-router remains in peerDependencies even though the client bundle no longer imports it. This is correct since server files (src/server/) still use redirect from that package. No action needed.

…ning

AuthKitProvider is rendered as a Wrap component before RouterProvider context exists. Calling useNavigate() unconditionally during render triggered "useRouter must be used inside a <RouterProvider>" warnings.

The navigate function was only used as a fallback in handleSignOut when no sign-out URL was returned. Replace it with window.location.href, which is appropriate for post-sign-out redirects (full page reload is desirable after session termination).

Fixes #57
Copy link
Member Author

@nicknisi nicknisi left a comment

Choose a reason for hiding this comment

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

Reviewer note (info): @tanstack/react-router remains in peerDependencies even though the client bundle no longer imports it. This is correct -- server files in src/server/ still use redirect from @tanstack/react-router. No action needed.

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Mar 15, 2026

Greptile Summary

Removes the unconditional useNavigate() hook call from AuthKitProvider to fix SSR warnings when the component renders before RouterProvider context is available (issue #57). The sign-out fallback now uses window.location.href assignment instead of TanStack Router's navigate(), which is appropriate since sign-out is always a user-triggered client-side action.

  • AuthKitProvider.tsx: Removed useNavigate import/hook call; replaced navigate({ to: returnTo }) with window.location.href = returnTo in the handleSignOut fallback when no logout URL is returned
  • AuthKitProvider.spec.tsx: Removed global @tanstack/react-router mock; added regression test verifying AuthKitProvider renders without router context; updated sign-out tests to assert window.location.href with proper window.location mock cleanup

Confidence Score: 5/5

  • This PR is safe to merge — it's a targeted fix that removes a problematic hook call and replaces it with a standard browser navigation API.
  • The change is small, focused, and well-tested with 176 passing tests including a new regression test. The behavioral difference (full page navigation vs SPA navigation on the sign-out fallback path) is negligible for a sign-out flow. No new dependencies, no security concerns, and no risk of breaking existing functionality.
  • No files require special attention.

Important Files Changed

Filename Overview
src/client/AuthKitProvider.tsx Removed useNavigate hook and replaced navigate({ to: returnTo }) with window.location.href = returnTo in the sign-out fallback path. Clean fix that eliminates SSR warning without changing core behavior.
src/client/AuthKitProvider.spec.tsx Removed global @tanstack/react-router mock, added regression test for rendering without router context, and updated sign-out tests to verify window.location.href assignment with proper cleanup via try/finally.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[User clicks Sign Out] --> B[handleSignOut called]
    B --> C[getSignOutUrl via server fn]
    C --> D{result.url returned?}
    D -->|Yes| E["window.location.href = result.url<br/>(redirect to WorkOS logout)"]
    D -->|No session| F["window.location.href = returnTo<br/>(was: navigate via TanStack Router)"]
    E --> G[Session terminated at WorkOS]
    F --> H[Full page navigation to returnTo]
Loading

Last reviewed commit: c3bf51c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant