Skip to content
6 changes: 3 additions & 3 deletions packages/start-client-core/src/client/hydrateStart.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { hydrate } from '@tanstack/router-core/ssr/client'

import { ServerFunctionSerializationAdapter } from './ServerFunctionSerializationAdapter'
import type { AnyRouter, AnySerializationAdapter } from '@tanstack/router-core'
import type { AnyStartInstanceOptions } from '../createStart'
import { startInstance } from '#tanstack-start-entry'
import {
hasPluginAdapters,
pluginSerializationAdapters,
} from '#tanstack-start-plugin-adapters'
import { getRouter } from '#tanstack-router-entry'
import { ServerFunctionSerializationAdapter } from './ServerFunctionSerializationAdapter'
import type { AnyRouter, AnySerializationAdapter } from '@tanstack/router-core'
import type { AnyStartInstanceOptions } from '../createStart'

export async function hydrateStart(): Promise<AnyRouter> {
const router = await getRouter()
Expand Down
37 changes: 25 additions & 12 deletions packages/start-client-core/src/createServerFn.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { mergeHeaders } from '@tanstack/router-core/ssr/client'

import { isRedirect, parseRedirect } from '@tanstack/router-core'
import { isNotFound, isRedirect, parseRedirect } from '@tanstack/router-core'
import { TSS_SERVER_FUNCTION_FACTORY } from './constants'
import { getStartOptions } from './getStartOptions'
import { getStartContextServerOnly } from './getStartContextServerOnly'
Expand All @@ -10,6 +10,14 @@ import type {
ServerFnMeta,
TSS_SERVER_FUNCTION,
} from './constants'
import type {
AnyFunctionMiddleware,
AnyRequestMiddleware,
AssignAllServerFnContext,
FunctionMiddlewareServerFnResult,
IntersectAllValidatorInputs,
IntersectAllValidatorOutputs,
} from './createMiddleware'
import type {
AnyValidator,
Constrain,
Expand All @@ -21,14 +29,6 @@ import type {
ValidateSerializableInput,
Validator,
} from '@tanstack/router-core'
import type {
AnyFunctionMiddleware,
AnyRequestMiddleware,
AssignAllServerFnContext,
FunctionMiddlewareServerFnResult,
IntersectAllValidatorInputs,
IntersectAllValidatorOutputs,
} from './createMiddleware'

type TODO = any

Expand Down Expand Up @@ -162,8 +162,15 @@ export const createServerFn: CreateServerFn<Register> = (options, __opts) => {
throw redirect
}

if (result.error) throw result.error
return result.result
if (
result.error instanceof Error ||
isRedirect(result.error) ||
isNotFound(result.error)
)
throw result.error
// Non-Error values in result.error are application-level error payloads;
// return them as the resolved value when no explicit result is present.
return result.result !== undefined ? result.result : result.error
},
{
// This copies over the URL, function ID
Expand Down Expand Up @@ -302,10 +309,16 @@ export async function executeMiddleware(

const result = await callNextMiddleware(nextCtx)

if (result.error) {
if (
result.error instanceof Error ||
isRedirect(result.error) ||
isNotFound(result.error)
) {
throw result.error
}

// Non-Error values in result.error are application-level error payloads;
// preserve them for downstream handlers.
return result
}

Expand Down
Loading