Skip to content

Commit

Permalink
fix(start): revert status codes for redirect/not found (#2555)
Browse files Browse the repository at this point in the history
  • Loading branch information
schiller-manuel authored Oct 16, 2024
1 parent 8536b73 commit c5da320
Showing 1 changed file with 7 additions and 27 deletions.
34 changes: 7 additions & 27 deletions packages/start/src/server-handler/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
serverFnPayloadTypeHeader,
serverFnReturnTypeHeader,
} from '../constants'
import type { AnyRedirect, NotFoundError } from '@tanstack/react-router'
import type { H3Event } from 'vinxi/server'

export default eventHandler(handleServerAction)
Expand Down Expand Up @@ -116,11 +115,8 @@ export async function handleServerRequest(request: Request, event?: H3Event) {
// return new Response(null, { status: 200 })
// }

if (isRedirect(result)) {
return handleRedirect(result)
}
if (isNotFound(result)) {
return handleNotFound(result)
if (isRedirect(result) || isNotFound(result)) {
return redirectOrNotFoundResponse(result)
}

return new Response(
Expand All @@ -143,11 +139,8 @@ export async function handleServerRequest(request: Request, event?: H3Event) {
// The client will check for __redirect and __notFound keys,
// and if they exist, it will handle them appropriately.

if (isRedirect(error)) {
return handleRedirect(error)
}
if (isNotFound(error)) {
return handleNotFound(error)
if (isRedirect(error) || isNotFound(error)) {
return redirectOrNotFoundResponse(error)
}

console.error('Server Fn Error!')
Expand Down Expand Up @@ -182,28 +175,15 @@ export async function handleServerRequest(request: Request, event?: H3Event) {
return response
}

function handleRedirect(redirect: AnyRedirect) {
const { headers, ...rest } = redirect

return new Response(JSON.stringify(rest), {
status: redirect.statusCode ?? 307,
headers: {
'Content-Type': 'application/json',
[serverFnReturnTypeHeader]: 'json',
...(headers || {}),
},
})
}

function handleNotFound(error: NotFoundError) {
function redirectOrNotFoundResponse(error: any) {
const { headers, ...rest } = error

return new Response(JSON.stringify(rest), {
status: 404,
status: 200,
headers: {
'Content-Type': 'application/json',
[serverFnReturnTypeHeader]: 'json',
...(headers || {}),
...(error.headers || {}),
},
})
}

0 comments on commit c5da320

Please sign in to comment.