@tanstack/query-core 5.90.5 → 5.90.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/hydration.ts CHANGED
@@ -79,25 +79,29 @@ function dehydrateQuery(
79
79
  serializeData: TransformerFn,
80
80
  shouldRedactErrors: (error: unknown) => boolean,
81
81
  ): 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
- })
82
+ const dehydratePromise = () => {
83
+ const promise = query.promise?.then(serializeData).catch((error) => {
84
+ if (!shouldRedactErrors(error)) {
85
+ // Reject original error if it should not be redacted
86
+ return Promise.reject(error)
87
+ }
88
+ // If not in production, log original error before rejecting redacted error
89
+ if (process.env.NODE_ENV !== 'production') {
90
+ console.error(
91
+ `A query that was dehydrated as pending ended up rejecting. [${query.queryHash}]: ${error}; The error will be redacted in production builds`,
92
+ )
93
+ }
94
+ return Promise.reject(new Error('redacted'))
95
+ })
96
+
97
+ // Avoid unhandled promise rejections
98
+ // We need the promise we dehydrate to reject to get the correct result into
99
+ // the query cache, but we also want to avoid unhandled promise rejections
100
+ // in whatever environment the prefetches are happening in.
101
+ promise?.catch(noop)
95
102
 
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)
103
+ return promise
104
+ }
101
105
 
102
106
  return {
103
107
  dehydratedAt: Date.now(),
@@ -110,7 +114,7 @@ function dehydrateQuery(
110
114
  queryKey: query.queryKey,
111
115
  queryHash: query.queryHash,
112
116
  ...(query.state.status === 'pending' && {
113
- promise,
117
+ promise: dehydratePromise(),
114
118
  }),
115
119
  ...(query.meta && { meta: query.meta }),
116
120
  }
package/src/query.ts CHANGED
@@ -210,10 +210,9 @@ export class Query<
210
210
  if (this.state && this.state.data === undefined) {
211
211
  const defaultState = getDefaultState(this.options)
212
212
  if (defaultState.data !== undefined) {
213
- this.setData(defaultState.data, {
214
- updatedAt: defaultState.dataUpdatedAt,
215
- manual: true,
216
- })
213
+ this.setState(
214
+ successState(defaultState.data, defaultState.dataUpdatedAt),
215
+ )
217
216
  this.#initialState = defaultState
218
217
  }
219
218
  }
@@ -635,12 +634,8 @@ export class Query<
635
634
  case 'success':
636
635
  const newState = {
637
636
  ...state,
638
- data: action.data,
637
+ ...successState(action.data, action.dataUpdatedAt),
639
638
  dataUpdateCount: state.dataUpdateCount + 1,
640
- dataUpdatedAt: action.dataUpdatedAt ?? Date.now(),
641
- error: null,
642
- isInvalidated: false,
643
- status: 'success' as const,
644
639
  ...(!action.manual && {
645
640
  fetchStatus: 'idle' as const,
646
641
  fetchFailureCount: 0,
@@ -710,6 +705,16 @@ export function fetchState<
710
705
  } as const
711
706
  }
712
707
 
708
+ function successState<TData>(data: TData | undefined, dataUpdatedAt?: number) {
709
+ return {
710
+ data,
711
+ dataUpdatedAt: dataUpdatedAt ?? Date.now(),
712
+ error: null,
713
+ isInvalidated: false,
714
+ status: 'success' as const,
715
+ }
716
+ }
717
+
713
718
  function getDefaultState<
714
719
  TQueryFnData,
715
720
  TError,