Skip to content

fix: handle deferred loading promise rejections properly #4377

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/start-client-core/src/ssr-client.tsx
Original file line number Diff line number Diff line change
@@ -35,6 +35,15 @@ export interface StartSsrGlobal {
id: number
promiseState: DeferredPromiseState<any>
}) => void
rejectPromise: (opts: {
matchId: string
id: number
error: {
message: string
stack?: string
name: string
}
}) => void
injectChunk: (opts: { matchId: string; id: number; chunk: string }) => void
closeStream: (opts: { matchId: string; id: number }) => void
}
48 changes: 34 additions & 14 deletions packages/start-server-core/src/ssr-server.ts
Original file line number Diff line number Diff line change
@@ -216,20 +216,40 @@ export function onMatchSettled(opts: {

function injectPromise(entry: ServerExtractedPromise) {
router.serverSsr!.injectScript(async () => {
await entry.promise

return `__TSR_SSR__.resolvePromise(${jsesc(
{
matchId: match.id,
id: entry.id,
promiseState: entry.promise[TSR_DEFERRED_PROMISE],
} satisfies ResolvePromiseState,
{
isScriptContext: true,
wrap: true,
json: true,
},
)})`
try {
await entry.promise

return `__TSR_SSR__.resolvePromise(${jsesc(
{
matchId: match.id,
id: entry.id,
promiseState: entry.promise[TSR_DEFERRED_PROMISE],
} satisfies ResolvePromiseState,
{
isScriptContext: true,
wrap: true,
json: true,
},
)})`
} catch (error) {
// Serialize and pass errors to the client
return `__TSR_SSR__.rejectPromise(${jsesc(
{
matchId: match.id,
id: entry.id,
error: {
message: error instanceof Error ? error.message : String(error),
stack: error instanceof Error ? error.stack : undefined,
name: error instanceof Error ? error.name : 'Unknown Error',
},
},
{
isScriptContext: true,
wrap: true,
json: true,
},
)})`
}
})
}

17 changes: 17 additions & 0 deletions packages/start-server-core/src/tsrScript.ts
Original file line number Diff line number Diff line change
@@ -58,6 +58,23 @@ const __TSR_SSR__: StartSsrGlobal = {
}
return false
},
rejectPromise: ({ matchId, id, error }) => {
const match = __TSR_SSR__.matches.find((m) => m.id === matchId)
if (match) {
const ex = match.extracted?.[id]
if (ex && ex.type === 'promise' && ex.value) {
// Rebuild the error object and pass it to the client
const reconstructedError = new Error(error.message)
reconstructedError.name = error.name
if (error.stack) {
reconstructedError.stack = error.stack
}
ex.value.reject(reconstructedError)
return true
}
}
return false
},
injectChunk: ({ matchId, id, chunk }) => {
const match = __TSR_SSR__.matches.find((m) => m.id === matchId)