Skip to content

Conversation

@canac
Copy link
Contributor

@canac canac commented Jan 4, 2026

useServerFn returns a promise that resolves to undefined when the server function redirects. This PR updates the return type to reflect that.

Summary by CodeRabbit

  • Improvements
    • Mutation hooks now allow empty/void results; success callbacks and state updates run only when data is returned.
    • Server-facing hooks now accept void-returning flows (e.g., redirects), so caller return types can be a value or void.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 4, 2026

📝 Walkthrough

Walkthrough

Three hooks updated: two useMutation implementations now allow mutation functions to resolve to void and only perform success handling when returned data is truthy; useServerFn's public callback type now may include Promise<void> and the prior as any cast was removed.

Changes

Cohort / File(s) Summary
Mutation Hook Updates
e2e/react-start/basic-auth/src/hooks/useMutation.ts, examples/react/start-supabase-basic/src/hooks/useMutation.ts
Updated fn type from (variables: TVariables) => Promise<TData> to (variables: TVariables) => Promise<TData | void>. Added if (data) { ... } guard so onSuccess, setStatus('success'), setError(undefined), setData(data), and returned value only occur when data is truthy.
Server Function Hook Return Type
packages/react-start/src/useServerFn.ts
Broadened returned callback type from (...args: Parameters<T>) => ReturnType<T> to (...args: Parameters<T>) => ReturnType<T> | Promise<void>. Removed the public as any cast; implementation and control flow unchanged.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐇 I nibble bytes and patch the nest,

mutations hush when data rests.
If nothing comes, I skip the cheer—
else I hop and set state clear.
A quiet thump, the changes nest.

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix(react-start): Update useServerFn return type to be undefined after redirect' accurately describes the main change—updating useServerFn's return type to reflect undefined resolution after redirects, matching the PR's primary objective.
✨ Finishing touches
  • 📝 Generate docstrings

📜 Recent review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ddd6a34 and 31cadc5.

📒 Files selected for processing (1)
  • packages/react-start/src/useServerFn.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/react-start/src/useServerFn.ts
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: Test
  • GitHub Check: Preview

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.

@nx-cloud
Copy link

nx-cloud bot commented Jan 4, 2026

View your CI Pipeline Execution ↗ for commit 31cadc5

Command Status Duration Result
nx affected --targets=test:eslint,test:unit,tes... ✅ Succeeded 9m 6s View ↗
nx run-many --target=build --exclude=examples/*... ✅ Succeeded 11s View ↗

☁️ Nx Cloud last updated this comment at 2026-01-04 18:43:53 UTC

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

Caution

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

⚠️ Outside diff range comments (2)
examples/react/start-supabase-basic/src/hooks/useMutation.ts (1)

35-35: Missing dependency in useCallback array.

The callback uses both opts.fn and opts.onSuccess (line 24), but the dependency array only includes opts.fn. This violates the exhaustive-deps rule and can cause stale closures where changes to opts.onSuccess are not reflected in the callback.

🔎 Recommended fix
-    [opts.fn],
+    [opts],

Or destructure and include both dependencies:

+  const { fn, onSuccess } = opts
   const mutate = React.useCallback(
     async (variables: TVariables): Promise<TData | undefined> => {
       setStatus('pending')
       setSubmittedAt(Date.now())
       setVariables(variables)
       //
       try {
-        const data = await opts.fn(variables)
+        const data = await fn(variables)
         if (data !== undefined) {
-          await opts.onSuccess?.({ data })
+          await onSuccess?.({ data })
           setStatus('success')
           setError(undefined)
           setData(data)
           return data
         }
       } catch (err) {
         setStatus('error')
         setError(err as TError)
       }
     },
-    [opts.fn],
+    [fn, onSuccess],
   )
e2e/react-start/basic-auth/src/hooks/useMutation.ts (1)

35-35: Missing dependency in useCallback array.

The callback uses both opts.fn and opts.onSuccess (line 24), but the dependency array only includes opts.fn. This violates the exhaustive-deps rule and can cause stale closures where changes to opts.onSuccess are not reflected in the callback.

🔎 Recommended fix
-    [opts.fn],
+    [opts],

Or destructure and include both dependencies:

+  const { fn, onSuccess } = opts
   const mutate = React.useCallback(
     async (variables: TVariables): Promise<TData | undefined> => {
       setStatus('pending')
       setSubmittedAt(Date.now())
       setVariables(variables)
       //
       try {
-        const data = await opts.fn(variables)
+        const data = await fn(variables)
         if (data !== undefined) {
-          await opts.onSuccess?.({ data })
+          await onSuccess?.({ data })
           setStatus('success')
           setError(undefined)
           setData(data)
           return data
         }
       } catch (err: any) {
         setStatus('error')
         setError(err)
       }
     },
-    [opts.fn],
+    [fn, onSuccess],
   )
🧹 Nitpick comments (1)
packages/react-start/src/useServerFn.ts (1)

29-29: Type assertion defeats the purpose of the type change.

The as any cast on line 29 bypasses TypeScript's type checking, undermining the type safety improvement made on line 6. While this may be necessary to satisfy TypeScript's callback typing constraints, it means callers won't benefit from the updated return type signature.

Consider whether the type assertion can be refined to preserve some type safety, for example:

) as (...args: Parameters<T>) => ReturnType<T> | Promise<void>
📜 Review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f19b51e and 0ce7167.

📒 Files selected for processing (3)
  • e2e/react-start/basic-auth/src/hooks/useMutation.ts
  • examples/react/start-supabase-basic/src/hooks/useMutation.ts
  • packages/react-start/src/useServerFn.ts
🧰 Additional context used
📓 Path-based instructions (2)
**/*.{ts,tsx}

📄 CodeRabbit inference engine (AGENTS.md)

Use TypeScript strict mode with extensive type safety for all code

Files:

  • packages/react-start/src/useServerFn.ts
  • e2e/react-start/basic-auth/src/hooks/useMutation.ts
  • examples/react/start-supabase-basic/src/hooks/useMutation.ts
**/*.{js,ts,tsx}

📄 CodeRabbit inference engine (AGENTS.md)

Implement ESLint rules for router best practices using the ESLint plugin router

Files:

  • packages/react-start/src/useServerFn.ts
  • e2e/react-start/basic-auth/src/hooks/useMutation.ts
  • examples/react/start-supabase-basic/src/hooks/useMutation.ts
🧬 Code graph analysis (1)
examples/react/start-supabase-basic/src/hooks/useMutation.ts (1)
examples/solid/kitchen-sink/src/useMutation.tsx (2)
  • variables (37-39)
  • data (47-49)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Test
🔇 Additional comments (3)
packages/react-start/src/useServerFn.ts (1)

6-6: Type broadening correctly reflects redirect behavior.

The addition of Promise<void> to the return type accurately captures the scenario where router.navigate is called during a redirect (line 22), which returns Promise<void>.

examples/react/start-supabase-basic/src/hooks/useMutation.ts (1)

4-4: Signature change correctly accommodates redirect scenarios.

The updated signature Promise<TData | void> properly reflects that the mutation function may return void when a redirect occurs, aligning with the useServerFn type changes.

e2e/react-start/basic-auth/src/hooks/useMutation.ts (1)

4-4: Signature change correctly accommodates redirect scenarios.

The updated signature Promise<TData | void> properly reflects that the mutation function may return void when a redirect occurs, aligning with the useServerFn type changes.

Comment on lines +23 to +29
if (data) {
await opts.onSuccess?.({ data })
setStatus('success')
setError(undefined)
setData(data)
return data
}
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

Truthiness check will fail for valid falsy return values.

The if (data) check on line 23 uses JavaScript truthiness, which evaluates to false for valid data values like 0, false, "", or NaN. This will incorrectly skip success handling when TData is a falsy type.

🔎 Proposed fix using explicit undefined check
-        if (data) {
+        if (data !== undefined) {
           await opts.onSuccess?.({ data })
           setStatus('success')
           setError(undefined)
           setData(data)
           return data
         }

Additional concern: When data is undefined (redirect scenario), the status remains 'pending' indefinitely. Consider whether the status should transition to a different state (e.g., 'idle' or a new 'redirected' state) to accurately reflect the mutation's completion.

Note: This file is nearly identical to examples/react/start-supabase-basic/src/hooks/useMutation.ts. Consider consolidating these duplicate implementations into a shared utility.

🤖 Prompt for AI Agents
In e2e/react-start/basic-auth/src/hooks/useMutation.ts around lines 23 to 29,
the code uses a truthiness check (`if (data)`) which incorrectly treats valid
falsy values (0, false, "", NaN) as no-data and also leaves status as 'pending'
when data is undefined (redirect scenario); change the check to an explicit
undefined test (e.g., `if (data !== undefined)`) so valid falsy responses
trigger the onSuccess flow, set status to 'success' and setData for that case,
and add an explicit branch for `data === undefined` to transition status to a
suitable state (e.g., 'idle' or a new 'redirected' state) to mark the mutation
complete; also consider consolidating this file with the nearly identical
example at examples/react/start-supabase-basic/src/hooks/useMutation.ts into a
shared utility.

Comment on lines +23 to +29
if (data) {
await opts.onSuccess?.({ data })
setStatus('success')
setError(undefined)
setData(data)
return data
}
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

Truthiness check will fail for valid falsy return values.

The if (data) check on line 23 uses JavaScript truthiness, which evaluates to false for valid data values like 0, false, "", or NaN. This will incorrectly skip success handling when TData is a falsy type.

🔎 Proposed fix using explicit undefined check
-        if (data) {
+        if (data !== undefined) {
           await opts.onSuccess?.({ data })
           setStatus('success')
           setError(undefined)
           setData(data)
           return data
         }

Additional concern: When data is undefined (redirect scenario), the status remains 'pending' indefinitely. Consider whether the status should transition to a different state (e.g., 'idle' or a new 'redirected' state) to accurately reflect the mutation's completion.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (data) {
await opts.onSuccess?.({ data })
setStatus('success')
setError(undefined)
setData(data)
return data
}
if (data !== undefined) {
await opts.onSuccess?.({ data })
setStatus('success')
setError(undefined)
setData(data)
return data
}
🤖 Prompt for AI Agents
In examples/react/start-supabase-basic/src/hooks/useMutation.ts around lines 23
to 29, the current truthiness check "if (data)" will skip valid falsy results
(0, false, "", NaN); change the branch to explicitly check for undefined (e.g.,
"if (data !== undefined)") so success handling always runs for valid falsy
TData, call opts.onSuccess with the data, set error undefined and setData(data)
as before; additionally handle the case where data === undefined (redirect
scenario) by transitioning status out of 'pending' to an appropriate state (for
example setStatus('idle') or add/assign a 'redirected' status) so the mutation
does not remain stuck in 'pending'.

@pkg-pr-new
Copy link

pkg-pr-new bot commented Jan 4, 2026

More templates

@tanstack/arktype-adapter

npm i https://pkg.pr.new/TanStack/router/@tanstack/arktype-adapter@6295

@tanstack/eslint-plugin-router

npm i https://pkg.pr.new/TanStack/router/@tanstack/eslint-plugin-router@6295

@tanstack/history

npm i https://pkg.pr.new/TanStack/router/@tanstack/history@6295

@tanstack/nitro-v2-vite-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/nitro-v2-vite-plugin@6295

@tanstack/react-router

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router@6295

@tanstack/react-router-devtools

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router-devtools@6295

@tanstack/react-router-ssr-query

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-router-ssr-query@6295

@tanstack/react-start

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start@6295

@tanstack/react-start-client

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-client@6295

@tanstack/react-start-server

npm i https://pkg.pr.new/TanStack/router/@tanstack/react-start-server@6295

@tanstack/router-cli

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-cli@6295

@tanstack/router-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-core@6295

@tanstack/router-devtools

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-devtools@6295

@tanstack/router-devtools-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-devtools-core@6295

@tanstack/router-generator

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-generator@6295

@tanstack/router-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-plugin@6295

@tanstack/router-ssr-query-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-ssr-query-core@6295

@tanstack/router-utils

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-utils@6295

@tanstack/router-vite-plugin

npm i https://pkg.pr.new/TanStack/router/@tanstack/router-vite-plugin@6295

@tanstack/solid-router

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-router@6295

@tanstack/solid-router-devtools

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-router-devtools@6295

@tanstack/solid-router-ssr-query

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-router-ssr-query@6295

@tanstack/solid-start

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start@6295

@tanstack/solid-start-client

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-client@6295

@tanstack/solid-start-server

npm i https://pkg.pr.new/TanStack/router/@tanstack/solid-start-server@6295

@tanstack/start-client-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-client-core@6295

@tanstack/start-fn-stubs

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-fn-stubs@6295

@tanstack/start-plugin-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-plugin-core@6295

@tanstack/start-server-core

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-server-core@6295

@tanstack/start-static-server-functions

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-static-server-functions@6295

@tanstack/start-storage-context

npm i https://pkg.pr.new/TanStack/router/@tanstack/start-storage-context@6295

@tanstack/valibot-adapter

npm i https://pkg.pr.new/TanStack/router/@tanstack/valibot-adapter@6295

@tanstack/virtual-file-routes

npm i https://pkg.pr.new/TanStack/router/@tanstack/virtual-file-routes@6295

@tanstack/vue-router

npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-router@6295

@tanstack/vue-router-devtools

npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-router-devtools@6295

@tanstack/vue-router-ssr-query

npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-router-ssr-query@6295

@tanstack/vue-start

npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-start@6295

@tanstack/vue-start-client

npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-start-client@6295

@tanstack/vue-start-server

npm i https://pkg.pr.new/TanStack/router/@tanstack/vue-start-server@6295

@tanstack/zod-adapter

npm i https://pkg.pr.new/TanStack/router/@tanstack/zod-adapter@6295

commit: 31cadc5

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant