11import { tryResolveSync } from './thenable'
2+ import { noop } from './utils'
23import type {
34 DefaultError ,
45 MutationKey ,
@@ -78,6 +79,26 @@ function dehydrateQuery(
7879 serializeData : TransformerFn ,
7980 shouldRedactErrors : ( error : unknown ) => boolean ,
8081) : DehydratedQuery {
82+ const promise = query . promise ?. then ( serializeData ) . catch ( ( error ) => {
83+ if ( ! shouldRedactErrors ( error ) ) {
84+ // Reject original error if it should not be redacted
85+ return Promise . reject ( error )
86+ }
87+ // If not in production, log original error before rejecting redacted error
88+ if ( process . env . NODE_ENV !== 'production' ) {
89+ console . error (
90+ `A query that was dehydrated as pending ended up rejecting. [${ query . queryHash } ]: ${ error } ; The error will be redacted in production builds` ,
91+ )
92+ }
93+ return Promise . reject ( new Error ( 'redacted' ) )
94+ } )
95+
96+ // Avoid unhandled promise rejections
97+ // We need the promise we dehydrate to reject to get the correct result into
98+ // the query cache, but we also want to avoid unhandled promise rejections
99+ // in whatever environment the prefetches are happening in.
100+ promise ?. catch ( noop )
101+
81102 return {
82103 dehydratedAt : Date . now ( ) ,
83104 state : {
@@ -89,19 +110,7 @@ function dehydrateQuery(
89110 queryKey : query . queryKey ,
90111 queryHash : query . queryHash ,
91112 ...( query . state . status === 'pending' && {
92- promise : query . promise ?. then ( serializeData ) . catch ( ( error ) => {
93- if ( ! shouldRedactErrors ( error ) ) {
94- // Reject original error if it should not be redacted
95- return Promise . reject ( error )
96- }
97- // If not in production, log original error before rejecting redacted error
98- if ( process . env . NODE_ENV !== 'production' ) {
99- console . error (
100- `A query that was dehydrated as pending ended up rejecting. [${ query . queryHash } ]: ${ error } ; The error will be redacted in production builds` ,
101- )
102- }
103- return Promise . reject ( new Error ( 'redacted' ) )
104- } ) ,
113+ promise,
105114 } ) ,
106115 ...( query . meta && { meta : query . meta } ) ,
107116 }
@@ -259,10 +268,13 @@ export function hydrate(
259268 // which will re-use the passed `initialPromise`
260269 // Note that we need to call these even when data was synchronously
261270 // available, as we still need to set up the retryer
262- void query . fetch ( undefined , {
263- // RSC transformed promises are not thenable
264- initialPromise : Promise . resolve ( promise ) . then ( deserializeData ) ,
265- } )
271+ query
272+ . fetch ( undefined , {
273+ // RSC transformed promises are not thenable
274+ initialPromise : Promise . resolve ( promise ) . then ( deserializeData ) ,
275+ } )
276+ // Avoid unhandled promise rejections
277+ . catch ( noop )
266278 }
267279 } ,
268280 )
0 commit comments