@tanstack/query-core 4.35.3 → 4.36.1
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/build/lib/hydration.d.ts.map +1 -1
- package/build/lib/hydration.esm.js +20 -11
- package/build/lib/hydration.esm.js.map +1 -1
- package/build/lib/hydration.js +20 -11
- package/build/lib/hydration.js.map +1 -1
- package/build/lib/hydration.mjs +20 -11
- package/build/lib/hydration.mjs.map +1 -1
- package/build/umd/index.development.js +20 -11
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +1 -1
- package/build/umd/index.production.js.map +1 -1
- package/package.json +1 -1
- package/src/hydration.ts +14 -13
- package/src/tests/hydration.test.tsx +33 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hydration.d.ts","sourceRoot":"","sources":["../../src/hydration.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;AAChD,OAAO,KAAK,EAAE,KAAK,EAAE,UAAU,EAAE,MAAM,SAAS,CAAA;AAChD,OAAO,KAAK,EACV,WAAW,EACX,eAAe,EACf,QAAQ,EACR,YAAY,EACb,MAAM,SAAS,CAAA;AAChB,OAAO,KAAK,EAAE,QAAQ,EAAE,aAAa,EAAE,MAAM,YAAY,CAAA;AAIzD,MAAM,WAAW,gBAAgB;IAC/B,kBAAkB,CAAC,EAAE,OAAO,CAAA;IAC5B,gBAAgB,CAAC,EAAE,OAAO,CAAA;IAC1B,uBAAuB,CAAC,EAAE,+BAA+B,CAAA;IACzD,oBAAoB,CAAC,EAAE,4BAA4B,CAAA;CACpD;AAED,MAAM,WAAW,cAAc;IAC7B,cAAc,CAAC,EAAE;QACf,OAAO,CAAC,EAAE,YAAY,CAAA;QACtB,SAAS,CAAC,EAAE,eAAe,CAAA;KAC5B,CAAA;CACF;AAED,UAAU,kBAAkB;IAC1B,WAAW,CAAC,EAAE,WAAW,CAAA;IACzB,KAAK,EAA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B,CAAA;CACpD;AAED,MAAM,WAAW,cAAc;IAC7B,cAAc,CAAC,EAAE;QACf,OAAO,CAAC,EAAE,YAAY,CAAA;QACtB,SAAS,CAAC,EAAE,eAAe,CAAA;KAC5B,CAAA;CACF;AAED,UAAU,kBAAkB;IAC1B,WAAW,CAAC,EAAE,WAAW,CAAA;IACzB,KAAK,EAAE,aAAa,CAAA;CACrB;AAED,UAAU,eAAe;IACvB,SAAS,EAAE,MAAM,CAAA;IACjB,QAAQ,EAAE,QAAQ,CAAA;IAClB,KAAK,EAAE,UAAU,CAAA;CAClB;AAED,MAAM,WAAW,eAAe;IAC9B,SAAS,EAAE,kBAAkB,EAAE,CAAA;IAC/B,OAAO,EAAE,eAAe,EAAE,CAAA;CAC3B;AAED,oBAAY,4BAA4B,GAAG,CAAC,KAAK,EAAE,KAAK,KAAK,OAAO,CAAA;AAEpE,oBAAY,+BAA+B,GAAG,CAAC,QAAQ,EAAE,QAAQ,KAAK,OAAO,CAAA;AAuB7E,wBAAgB,8BAA8B,CAAC,QAAQ,EAAE,QAAQ,WAEhE;AAED,wBAAgB,2BAA2B,CAAC,KAAK,EAAE,KAAK,WAEvD;AAED,wBAAgB,SAAS,CACvB,MAAM,EAAE,WAAW,EACnB,OAAO,GAAE,gBAAqB,GAC7B,eAAe,CAiCjB;AAED,wBAAgB,OAAO,CACrB,MAAM,EAAE,WAAW,EACnB,eAAe,EAAE,OAAO,EACxB,OAAO,CAAC,EAAE,cAAc,GACvB,IAAI,CAsDN"}
|
|
@@ -70,18 +70,23 @@ function hydrate(client, dehydratedState, options) {
|
|
|
70
70
|
mutationKey: dehydratedMutation.mutationKey
|
|
71
71
|
}, dehydratedMutation.state);
|
|
72
72
|
});
|
|
73
|
-
queries.forEach(
|
|
73
|
+
queries.forEach(({
|
|
74
|
+
queryKey,
|
|
75
|
+
state,
|
|
76
|
+
queryHash
|
|
77
|
+
}) => {
|
|
74
78
|
var _options$defaultOptio2;
|
|
75
79
|
|
|
76
|
-
const query = queryCache.get(
|
|
77
|
-
// query being stuck in fetching state upon hydration
|
|
78
|
-
|
|
79
|
-
const dehydratedQueryState = { ...dehydratedQuery.state,
|
|
80
|
-
fetchStatus: 'idle'
|
|
81
|
-
}; // Do not hydrate if an existing query exists with newer data
|
|
80
|
+
const query = queryCache.get(queryHash); // Do not hydrate if an existing query exists with newer data
|
|
82
81
|
|
|
83
82
|
if (query) {
|
|
84
|
-
if (query.state.dataUpdatedAt <
|
|
83
|
+
if (query.state.dataUpdatedAt < state.dataUpdatedAt) {
|
|
84
|
+
// omit fetchStatus from dehydrated state
|
|
85
|
+
// so that query stays in its current fetchStatus
|
|
86
|
+
const {
|
|
87
|
+
fetchStatus: _ignored,
|
|
88
|
+
...dehydratedQueryState
|
|
89
|
+
} = state;
|
|
85
90
|
query.setState(dehydratedQueryState);
|
|
86
91
|
}
|
|
87
92
|
|
|
@@ -90,9 +95,13 @@ function hydrate(client, dehydratedState, options) {
|
|
|
90
95
|
|
|
91
96
|
|
|
92
97
|
queryCache.build(client, { ...(options == null ? void 0 : (_options$defaultOptio2 = options.defaultOptions) == null ? void 0 : _options$defaultOptio2.queries),
|
|
93
|
-
queryKey
|
|
94
|
-
queryHash
|
|
95
|
-
},
|
|
98
|
+
queryKey,
|
|
99
|
+
queryHash
|
|
100
|
+
}, // Reset fetch status to idle to avoid
|
|
101
|
+
// query being stuck in fetching state upon hydration
|
|
102
|
+
{ ...state,
|
|
103
|
+
fetchStatus: 'idle'
|
|
104
|
+
});
|
|
96
105
|
});
|
|
97
106
|
}
|
|
98
107
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hydration.esm.js","sources":["../../src/hydration.ts"],"sourcesContent":["import type { QueryClient } from './queryClient'\nimport type { Query, QueryState } from './query'\nimport type {\n MutationKey,\n MutationOptions,\n QueryKey,\n QueryOptions,\n} from './types'\nimport type { Mutation, MutationState } from './mutation'\n\n// TYPES\n\nexport interface DehydrateOptions {\n dehydrateMutations?: boolean\n dehydrateQueries?: boolean\n shouldDehydrateMutation?: ShouldDehydrateMutationFunction\n shouldDehydrateQuery?: ShouldDehydrateQueryFunction\n}\n\nexport interface HydrateOptions {\n defaultOptions?: {\n queries?: QueryOptions\n mutations?: MutationOptions\n }\n}\n\ninterface DehydratedMutation {\n mutationKey?: MutationKey\n state: MutationState\n}\n\ninterface DehydratedQuery {\n queryHash: string\n queryKey: QueryKey\n state: QueryState\n}\n\nexport interface DehydratedState {\n mutations: DehydratedMutation[]\n queries: DehydratedQuery[]\n}\n\nexport type ShouldDehydrateQueryFunction = (query: Query) => boolean\n\nexport type ShouldDehydrateMutationFunction = (mutation: Mutation) => boolean\n\n// FUNCTIONS\n\nfunction dehydrateMutation(mutation: Mutation): DehydratedMutation {\n return {\n mutationKey: mutation.options.mutationKey,\n state: mutation.state,\n }\n}\n\n// Most config is not dehydrated but instead meant to configure again when\n// consuming the de/rehydrated data, typically with useQuery on the client.\n// Sometimes it might make sense to prefetch data on the server and include\n// in the html-payload, but not consume it on the initial render.\nfunction dehydrateQuery(query: Query): DehydratedQuery {\n return {\n state: query.state,\n queryKey: query.queryKey,\n queryHash: query.queryHash,\n }\n}\n\nexport function defaultShouldDehydrateMutation(mutation: Mutation) {\n return mutation.state.isPaused\n}\n\nexport function defaultShouldDehydrateQuery(query: Query) {\n return query.state.status === 'success'\n}\n\nexport function dehydrate(\n client: QueryClient,\n options: DehydrateOptions = {},\n): DehydratedState {\n const mutations: DehydratedMutation[] = []\n const queries: DehydratedQuery[] = []\n\n if (options.dehydrateMutations !== false) {\n const shouldDehydrateMutation =\n options.shouldDehydrateMutation || defaultShouldDehydrateMutation\n\n client\n .getMutationCache()\n .getAll()\n .forEach((mutation) => {\n if (shouldDehydrateMutation(mutation)) {\n mutations.push(dehydrateMutation(mutation))\n }\n })\n }\n\n if (options.dehydrateQueries !== false) {\n const shouldDehydrateQuery =\n options.shouldDehydrateQuery || defaultShouldDehydrateQuery\n\n client\n .getQueryCache()\n .getAll()\n .forEach((query) => {\n if (shouldDehydrateQuery(query)) {\n queries.push(dehydrateQuery(query))\n }\n })\n }\n\n return { mutations, queries }\n}\n\nexport function hydrate(\n client: QueryClient,\n dehydratedState: unknown,\n options?: HydrateOptions,\n): void {\n if (typeof dehydratedState !== 'object' || dehydratedState === null) {\n return\n }\n\n const mutationCache = client.getMutationCache()\n const queryCache = client.getQueryCache()\n\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n const mutations = (dehydratedState as DehydratedState).mutations || []\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n const queries = (dehydratedState as DehydratedState).queries || []\n\n mutations.forEach((dehydratedMutation) => {\n mutationCache.build(\n client,\n {\n ...options?.defaultOptions?.mutations,\n mutationKey: dehydratedMutation.mutationKey,\n },\n dehydratedMutation.state,\n )\n })\n\n queries.forEach((
|
|
1
|
+
{"version":3,"file":"hydration.esm.js","sources":["../../src/hydration.ts"],"sourcesContent":["import type { QueryClient } from './queryClient'\nimport type { Query, QueryState } from './query'\nimport type {\n MutationKey,\n MutationOptions,\n QueryKey,\n QueryOptions,\n} from './types'\nimport type { Mutation, MutationState } from './mutation'\n\n// TYPES\n\nexport interface DehydrateOptions {\n dehydrateMutations?: boolean\n dehydrateQueries?: boolean\n shouldDehydrateMutation?: ShouldDehydrateMutationFunction\n shouldDehydrateQuery?: ShouldDehydrateQueryFunction\n}\n\nexport interface HydrateOptions {\n defaultOptions?: {\n queries?: QueryOptions\n mutations?: MutationOptions\n }\n}\n\ninterface DehydratedMutation {\n mutationKey?: MutationKey\n state: MutationState\n}\n\ninterface DehydratedQuery {\n queryHash: string\n queryKey: QueryKey\n state: QueryState\n}\n\nexport interface DehydratedState {\n mutations: DehydratedMutation[]\n queries: DehydratedQuery[]\n}\n\nexport type ShouldDehydrateQueryFunction = (query: Query) => boolean\n\nexport type ShouldDehydrateMutationFunction = (mutation: Mutation) => boolean\n\n// FUNCTIONS\n\nfunction dehydrateMutation(mutation: Mutation): DehydratedMutation {\n return {\n mutationKey: mutation.options.mutationKey,\n state: mutation.state,\n }\n}\n\n// Most config is not dehydrated but instead meant to configure again when\n// consuming the de/rehydrated data, typically with useQuery on the client.\n// Sometimes it might make sense to prefetch data on the server and include\n// in the html-payload, but not consume it on the initial render.\nfunction dehydrateQuery(query: Query): DehydratedQuery {\n return {\n state: query.state,\n queryKey: query.queryKey,\n queryHash: query.queryHash,\n }\n}\n\nexport function defaultShouldDehydrateMutation(mutation: Mutation) {\n return mutation.state.isPaused\n}\n\nexport function defaultShouldDehydrateQuery(query: Query) {\n return query.state.status === 'success'\n}\n\nexport function dehydrate(\n client: QueryClient,\n options: DehydrateOptions = {},\n): DehydratedState {\n const mutations: DehydratedMutation[] = []\n const queries: DehydratedQuery[] = []\n\n if (options.dehydrateMutations !== false) {\n const shouldDehydrateMutation =\n options.shouldDehydrateMutation || defaultShouldDehydrateMutation\n\n client\n .getMutationCache()\n .getAll()\n .forEach((mutation) => {\n if (shouldDehydrateMutation(mutation)) {\n mutations.push(dehydrateMutation(mutation))\n }\n })\n }\n\n if (options.dehydrateQueries !== false) {\n const shouldDehydrateQuery =\n options.shouldDehydrateQuery || defaultShouldDehydrateQuery\n\n client\n .getQueryCache()\n .getAll()\n .forEach((query) => {\n if (shouldDehydrateQuery(query)) {\n queries.push(dehydrateQuery(query))\n }\n })\n }\n\n return { mutations, queries }\n}\n\nexport function hydrate(\n client: QueryClient,\n dehydratedState: unknown,\n options?: HydrateOptions,\n): void {\n if (typeof dehydratedState !== 'object' || dehydratedState === null) {\n return\n }\n\n const mutationCache = client.getMutationCache()\n const queryCache = client.getQueryCache()\n\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n const mutations = (dehydratedState as DehydratedState).mutations || []\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n const queries = (dehydratedState as DehydratedState).queries || []\n\n mutations.forEach((dehydratedMutation) => {\n mutationCache.build(\n client,\n {\n ...options?.defaultOptions?.mutations,\n mutationKey: dehydratedMutation.mutationKey,\n },\n dehydratedMutation.state,\n )\n })\n\n queries.forEach(({ queryKey, state, queryHash }) => {\n const query = queryCache.get(queryHash)\n\n // Do not hydrate if an existing query exists with newer data\n if (query) {\n if (query.state.dataUpdatedAt < state.dataUpdatedAt) {\n // omit fetchStatus from dehydrated state\n // so that query stays in its current fetchStatus\n const { fetchStatus: _ignored, ...dehydratedQueryState } = state\n query.setState(dehydratedQueryState)\n }\n return\n }\n\n // Restore query\n queryCache.build(\n client,\n {\n ...options?.defaultOptions?.queries,\n queryKey,\n queryHash,\n },\n // Reset fetch status to idle to avoid\n // query being stuck in fetching state upon hydration\n {\n ...state,\n fetchStatus: 'idle',\n },\n )\n })\n}\n"],"names":["dehydrateMutation","mutation","mutationKey","options","state","dehydrateQuery","query","queryKey","queryHash","defaultShouldDehydrateMutation","isPaused","defaultShouldDehydrateQuery","status","dehydrate","client","mutations","queries","dehydrateMutations","shouldDehydrateMutation","getMutationCache","getAll","forEach","push","dehydrateQueries","shouldDehydrateQuery","getQueryCache","hydrate","dehydratedState","mutationCache","queryCache","dehydratedMutation","build","defaultOptions","get","dataUpdatedAt","fetchStatus","_ignored","dehydratedQueryState","setState"],"mappings":"AAUA;AAoCA;AAEA,SAASA,iBAAT,CAA2BC,QAA3B,EAAmE;EACjE,OAAO;AACLC,IAAAA,WAAW,EAAED,QAAQ,CAACE,OAAT,CAAiBD,WADzB;IAELE,KAAK,EAAEH,QAAQ,CAACG,KAAAA;GAFlB,CAAA;AAID;AAGD;AACA;AACA;;;AACA,SAASC,cAAT,CAAwBC,KAAxB,EAAuD;EACrD,OAAO;IACLF,KAAK,EAAEE,KAAK,CAACF,KADR;IAELG,QAAQ,EAAED,KAAK,CAACC,QAFX;IAGLC,SAAS,EAAEF,KAAK,CAACE,SAAAA;GAHnB,CAAA;AAKD,CAAA;;AAEM,SAASC,8BAAT,CAAwCR,QAAxC,EAA4D;AACjE,EAAA,OAAOA,QAAQ,CAACG,KAAT,CAAeM,QAAtB,CAAA;AACD,CAAA;AAEM,SAASC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ACD,MAAA,OAAA;AACD,KAZiD;;;AAelDR,IAAAA,UAAU,CAACE,KAAX,CACEjB,MADF,EAEE,EACE,IAAGX,OAAH,IAAA,IAAA,GAAA,KAAA,CAAA,GAAA,CAAA,sBAAA,GAAGA,OAAO,CAAE6B,cAAZ,KAAG,IAAA,GAAA,KAAA,CAAA,GAAA,sBAAA,CAAyBhB,OAA5B,CADF;MAEET,QAFF;AAGEC,MAAAA,SAAAA;AAHF,KAFF;AAQE;AACA,IAAA,EACE,GAAGJ,KADL;AAEE+B,MAAAA,WAAW,EAAE,MAAA;KAXjB,CAAA,CAAA;GAfF,CAAA,CAAA;AA8BD;;;;"}
|
package/build/lib/hydration.js
CHANGED
|
@@ -74,18 +74,23 @@ function hydrate(client, dehydratedState, options) {
|
|
|
74
74
|
mutationKey: dehydratedMutation.mutationKey
|
|
75
75
|
}, dehydratedMutation.state);
|
|
76
76
|
});
|
|
77
|
-
queries.forEach(
|
|
77
|
+
queries.forEach(({
|
|
78
|
+
queryKey,
|
|
79
|
+
state,
|
|
80
|
+
queryHash
|
|
81
|
+
}) => {
|
|
78
82
|
var _options$defaultOptio2;
|
|
79
83
|
|
|
80
|
-
const query = queryCache.get(
|
|
81
|
-
// query being stuck in fetching state upon hydration
|
|
82
|
-
|
|
83
|
-
const dehydratedQueryState = { ...dehydratedQuery.state,
|
|
84
|
-
fetchStatus: 'idle'
|
|
85
|
-
}; // Do not hydrate if an existing query exists with newer data
|
|
84
|
+
const query = queryCache.get(queryHash); // Do not hydrate if an existing query exists with newer data
|
|
86
85
|
|
|
87
86
|
if (query) {
|
|
88
|
-
if (query.state.dataUpdatedAt <
|
|
87
|
+
if (query.state.dataUpdatedAt < state.dataUpdatedAt) {
|
|
88
|
+
// omit fetchStatus from dehydrated state
|
|
89
|
+
// so that query stays in its current fetchStatus
|
|
90
|
+
const {
|
|
91
|
+
fetchStatus: _ignored,
|
|
92
|
+
...dehydratedQueryState
|
|
93
|
+
} = state;
|
|
89
94
|
query.setState(dehydratedQueryState);
|
|
90
95
|
}
|
|
91
96
|
|
|
@@ -94,9 +99,13 @@ function hydrate(client, dehydratedState, options) {
|
|
|
94
99
|
|
|
95
100
|
|
|
96
101
|
queryCache.build(client, { ...(options == null ? void 0 : (_options$defaultOptio2 = options.defaultOptions) == null ? void 0 : _options$defaultOptio2.queries),
|
|
97
|
-
queryKey
|
|
98
|
-
queryHash
|
|
99
|
-
},
|
|
102
|
+
queryKey,
|
|
103
|
+
queryHash
|
|
104
|
+
}, // Reset fetch status to idle to avoid
|
|
105
|
+
// query being stuck in fetching state upon hydration
|
|
106
|
+
{ ...state,
|
|
107
|
+
fetchStatus: 'idle'
|
|
108
|
+
});
|
|
100
109
|
});
|
|
101
110
|
}
|
|
102
111
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hydration.js","sources":["../../src/hydration.ts"],"sourcesContent":["import type { QueryClient } from './queryClient'\nimport type { Query, QueryState } from './query'\nimport type {\n MutationKey,\n MutationOptions,\n QueryKey,\n QueryOptions,\n} from './types'\nimport type { Mutation, MutationState } from './mutation'\n\n// TYPES\n\nexport interface DehydrateOptions {\n dehydrateMutations?: boolean\n dehydrateQueries?: boolean\n shouldDehydrateMutation?: ShouldDehydrateMutationFunction\n shouldDehydrateQuery?: ShouldDehydrateQueryFunction\n}\n\nexport interface HydrateOptions {\n defaultOptions?: {\n queries?: QueryOptions\n mutations?: MutationOptions\n }\n}\n\ninterface DehydratedMutation {\n mutationKey?: MutationKey\n state: MutationState\n}\n\ninterface DehydratedQuery {\n queryHash: string\n queryKey: QueryKey\n state: QueryState\n}\n\nexport interface DehydratedState {\n mutations: DehydratedMutation[]\n queries: DehydratedQuery[]\n}\n\nexport type ShouldDehydrateQueryFunction = (query: Query) => boolean\n\nexport type ShouldDehydrateMutationFunction = (mutation: Mutation) => boolean\n\n// FUNCTIONS\n\nfunction dehydrateMutation(mutation: Mutation): DehydratedMutation {\n return {\n mutationKey: mutation.options.mutationKey,\n state: mutation.state,\n }\n}\n\n// Most config is not dehydrated but instead meant to configure again when\n// consuming the de/rehydrated data, typically with useQuery on the client.\n// Sometimes it might make sense to prefetch data on the server and include\n// in the html-payload, but not consume it on the initial render.\nfunction dehydrateQuery(query: Query): DehydratedQuery {\n return {\n state: query.state,\n queryKey: query.queryKey,\n queryHash: query.queryHash,\n }\n}\n\nexport function defaultShouldDehydrateMutation(mutation: Mutation) {\n return mutation.state.isPaused\n}\n\nexport function defaultShouldDehydrateQuery(query: Query) {\n return query.state.status === 'success'\n}\n\nexport function dehydrate(\n client: QueryClient,\n options: DehydrateOptions = {},\n): DehydratedState {\n const mutations: DehydratedMutation[] = []\n const queries: DehydratedQuery[] = []\n\n if (options.dehydrateMutations !== false) {\n const shouldDehydrateMutation =\n options.shouldDehydrateMutation || defaultShouldDehydrateMutation\n\n client\n .getMutationCache()\n .getAll()\n .forEach((mutation) => {\n if (shouldDehydrateMutation(mutation)) {\n mutations.push(dehydrateMutation(mutation))\n }\n })\n }\n\n if (options.dehydrateQueries !== false) {\n const shouldDehydrateQuery =\n options.shouldDehydrateQuery || defaultShouldDehydrateQuery\n\n client\n .getQueryCache()\n .getAll()\n .forEach((query) => {\n if (shouldDehydrateQuery(query)) {\n queries.push(dehydrateQuery(query))\n }\n })\n }\n\n return { mutations, queries }\n}\n\nexport function hydrate(\n client: QueryClient,\n dehydratedState: unknown,\n options?: HydrateOptions,\n): void {\n if (typeof dehydratedState !== 'object' || dehydratedState === null) {\n return\n }\n\n const mutationCache = client.getMutationCache()\n const queryCache = client.getQueryCache()\n\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n const mutations = (dehydratedState as DehydratedState).mutations || []\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n const queries = (dehydratedState as DehydratedState).queries || []\n\n mutations.forEach((dehydratedMutation) => {\n mutationCache.build(\n client,\n {\n ...options?.defaultOptions?.mutations,\n mutationKey: dehydratedMutation.mutationKey,\n },\n dehydratedMutation.state,\n )\n })\n\n queries.forEach((
|
|
1
|
+
{"version":3,"file":"hydration.js","sources":["../../src/hydration.ts"],"sourcesContent":["import type { QueryClient } from './queryClient'\nimport type { Query, QueryState } from './query'\nimport type {\n MutationKey,\n MutationOptions,\n QueryKey,\n QueryOptions,\n} from './types'\nimport type { Mutation, MutationState } from './mutation'\n\n// TYPES\n\nexport interface DehydrateOptions {\n dehydrateMutations?: boolean\n dehydrateQueries?: boolean\n shouldDehydrateMutation?: ShouldDehydrateMutationFunction\n shouldDehydrateQuery?: ShouldDehydrateQueryFunction\n}\n\nexport interface HydrateOptions {\n defaultOptions?: {\n queries?: QueryOptions\n mutations?: MutationOptions\n }\n}\n\ninterface DehydratedMutation {\n mutationKey?: MutationKey\n state: MutationState\n}\n\ninterface DehydratedQuery {\n queryHash: string\n queryKey: QueryKey\n state: QueryState\n}\n\nexport interface DehydratedState {\n mutations: DehydratedMutation[]\n queries: DehydratedQuery[]\n}\n\nexport type ShouldDehydrateQueryFunction = (query: Query) => boolean\n\nexport type ShouldDehydrateMutationFunction = (mutation: Mutation) => boolean\n\n// FUNCTIONS\n\nfunction dehydrateMutation(mutation: Mutation): DehydratedMutation {\n return {\n mutationKey: mutation.options.mutationKey,\n state: mutation.state,\n }\n}\n\n// Most config is not dehydrated but instead meant to configure again when\n// consuming the de/rehydrated data, typically with useQuery on the client.\n// Sometimes it might make sense to prefetch data on the server and include\n// in the html-payload, but not consume it on the initial render.\nfunction dehydrateQuery(query: Query): DehydratedQuery {\n return {\n state: query.state,\n queryKey: query.queryKey,\n queryHash: query.queryHash,\n }\n}\n\nexport function defaultShouldDehydrateMutation(mutation: Mutation) {\n return mutation.state.isPaused\n}\n\nexport function defaultShouldDehydrateQuery(query: Query) {\n return query.state.status === 'success'\n}\n\nexport function dehydrate(\n client: QueryClient,\n options: DehydrateOptions = {},\n): DehydratedState {\n const mutations: DehydratedMutation[] = []\n const queries: DehydratedQuery[] = []\n\n if (options.dehydrateMutations !== false) {\n const shouldDehydrateMutation =\n options.shouldDehydrateMutation || defaultShouldDehydrateMutation\n\n client\n .getMutationCache()\n .getAll()\n .forEach((mutation) => {\n if (shouldDehydrateMutation(mutation)) {\n mutations.push(dehydrateMutation(mutation))\n }\n })\n }\n\n if (options.dehydrateQueries !== false) {\n const shouldDehydrateQuery =\n options.shouldDehydrateQuery || defaultShouldDehydrateQuery\n\n client\n .getQueryCache()\n .getAll()\n .forEach((query) => {\n if (shouldDehydrateQuery(query)) {\n queries.push(dehydrateQuery(query))\n }\n })\n }\n\n return { mutations, queries }\n}\n\nexport function hydrate(\n client: QueryClient,\n dehydratedState: unknown,\n options?: HydrateOptions,\n): void {\n if (typeof dehydratedState !== 'object' || dehydratedState === null) {\n return\n }\n\n const mutationCache = client.getMutationCache()\n const queryCache = client.getQueryCache()\n\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n const mutations = (dehydratedState as DehydratedState).mutations || []\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n const queries = (dehydratedState as DehydratedState).queries || []\n\n mutations.forEach((dehydratedMutation) => {\n mutationCache.build(\n client,\n {\n ...options?.defaultOptions?.mutations,\n mutationKey: dehydratedMutation.mutationKey,\n },\n dehydratedMutation.state,\n )\n })\n\n queries.forEach(({ queryKey, state, queryHash }) => {\n const query = queryCache.get(queryHash)\n\n // Do not hydrate if an existing query exists with newer data\n if (query) {\n if (query.state.dataUpdatedAt < state.dataUpdatedAt) {\n // omit fetchStatus from dehydrated state\n // so that query stays in its current fetchStatus\n const { fetchStatus: _ignored, ...dehydratedQueryState } = state\n query.setState(dehydratedQueryState)\n }\n return\n }\n\n // Restore query\n queryCache.build(\n client,\n {\n ...options?.defaultOptions?.queries,\n queryKey,\n queryHash,\n },\n // Reset fetch status to idle to avoid\n // query being stuck in fetching state upon hydration\n {\n ...state,\n fetchStatus: 'idle',\n },\n )\n })\n}\n"],"names":["dehydrateMutation","mutation","mutationKey","options","state","dehydrateQuery","query","queryKey","queryHash","defaultShouldDehydrateMutation","isPaused","defaultShouldDehydrateQuery","status","dehydrate","client","mutations","queries","dehydrateMutations","shouldDehydrateMutation","getMutationCache","getAll","forEach","push","dehydrateQueries","shouldDehydrateQuery","getQueryCache","hydrate","dehydratedState","mutationCache","queryCache","dehydratedMutation","build","defaultOptions","get","dataUpdatedAt","fetchStatus","_ignored","dehydratedQueryState","setState"],"mappings":";;;;AAUA;AAoCA;AAEA,SAASA,iBAAT,CAA2BC,QAA3B,EAAmE;EACjE,OAAO;AACLC,IAAAA,WAAW,EAAED,QAAQ,CAACE,OAAT,CAAiBD,WADzB;IAELE,KAAK,EAAEH,QAAQ,CAACG,KAAAA;GAFlB,CAAA;AAID;AAGD;AACA;AACA;;;AACA,SAASC,cAAT,CAAwBC,KAAxB,EAAuD;EACrD,OAAO;IACLF,KAAK,EAAEE,KAAK,CAACF,KADR;IAELG,QAAQ,EAAED,KAAK,CAACC,QAFX;IAGLC,SAAS,EAAEF,KAAK,CAACE,SAAAA;GAHnB,CAAA;AAKD,CAAA;;AAEM,SAASC,8BAAT,CAAwCR,QAAxC,EAA4D;AACjE,EAAA,OAAOA,QAAQ,CAACG,KAAT,CAAeM,QAAtB,CAAA;AACD,CAAA;AAEM,SAASC,2BAAT,CAAqCL,KAArC,EAAmD;AACxD,EAAA,OAAOA,KAAK,CAACF,KAAN,CAAYQ,MAAZ,KAAuB,SAA9B,CAAA;AACD,CAAA;AAEM,SAASC,SAAT,CACLC,MADK,EAELX,OAAyB,GAAG,EAFvB,EAGY;EACjB,MAAMY,SAA+B,GAAG,EAAxC,CAAA;EACA,MAAMC,OAA0B,GAAG,EAAnC,CAAA;;AAEA,EAAA,IAAIb,OAAO,CAACc,kBAAR,KAA+B,KAAnC,EAA0C;AACxC,IAAA,MAAMC,uBAAuB,GAC3Bf,OAAO,CAACe,uBAAR,IAAmCT,8BADrC,CAAA;IAGAK,MAAM,CACHK,gBADH,EAEGC,CAAAA,MAFH,GAGGC,OAHH,CAGYpB,QAAD,IAAc;AACrB,MAAA,IAAIiB,uBAAuB,CA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: dehydratedMutation.mutationKey
|
|
71
71
|
}, dehydratedMutation.state);
|
|
72
72
|
});
|
|
73
|
-
queries.forEach(
|
|
73
|
+
queries.forEach(({
|
|
74
|
+
queryKey,
|
|
75
|
+
state,
|
|
76
|
+
queryHash
|
|
77
|
+
}) => {
|
|
74
78
|
var _options$defaultOptio2;
|
|
75
79
|
|
|
76
|
-
const query = queryCache.get(
|
|
77
|
-
// query being stuck in fetching state upon hydration
|
|
78
|
-
|
|
79
|
-
const dehydratedQueryState = { ...dehydratedQuery.state,
|
|
80
|
-
fetchStatus: 'idle'
|
|
81
|
-
}; // Do not hydrate if an existing query exists with newer data
|
|
80
|
+
const query = queryCache.get(queryHash); // Do not hydrate if an existing query exists with newer data
|
|
82
81
|
|
|
83
82
|
if (query) {
|
|
84
|
-
if (query.state.dataUpdatedAt <
|
|
83
|
+
if (query.state.dataUpdatedAt < state.dataUpdatedAt) {
|
|
84
|
+
// omit fetchStatus from dehydrated state
|
|
85
|
+
// so that query stays in its current fetchStatus
|
|
86
|
+
const {
|
|
87
|
+
fetchStatus: _ignored,
|
|
88
|
+
...dehydratedQueryState
|
|
89
|
+
} = state;
|
|
85
90
|
query.setState(dehydratedQueryState);
|
|
86
91
|
}
|
|
87
92
|
|
|
@@ -90,9 +95,13 @@ function hydrate(client, dehydratedState, options) {
|
|
|
90
95
|
|
|
91
96
|
|
|
92
97
|
queryCache.build(client, { ...(options == null ? void 0 : (_options$defaultOptio2 = options.defaultOptions) == null ? void 0 : _options$defaultOptio2.queries),
|
|
93
|
-
queryKey
|
|
94
|
-
queryHash
|
|
95
|
-
},
|
|
98
|
+
queryKey,
|
|
99
|
+
queryHash
|
|
100
|
+
}, // Reset fetch status to idle to avoid
|
|
101
|
+
// query being stuck in fetching state upon hydration
|
|
102
|
+
{ ...state,
|
|
103
|
+
fetchStatus: 'idle'
|
|
104
|
+
});
|
|
96
105
|
});
|
|
97
106
|
}
|
|
98
107
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hydration.mjs","sources":["../../src/hydration.ts"],"sourcesContent":["import type { QueryClient } from './queryClient'\nimport type { Query, QueryState } from './query'\nimport type {\n MutationKey,\n MutationOptions,\n QueryKey,\n QueryOptions,\n} from './types'\nimport type { Mutation, MutationState } from './mutation'\n\n// TYPES\n\nexport interface DehydrateOptions {\n dehydrateMutations?: boolean\n dehydrateQueries?: boolean\n shouldDehydrateMutation?: ShouldDehydrateMutationFunction\n shouldDehydrateQuery?: ShouldDehydrateQueryFunction\n}\n\nexport interface HydrateOptions {\n defaultOptions?: {\n queries?: QueryOptions\n mutations?: MutationOptions\n }\n}\n\ninterface DehydratedMutation {\n mutationKey?: MutationKey\n state: MutationState\n}\n\ninterface DehydratedQuery {\n queryHash: string\n queryKey: QueryKey\n state: QueryState\n}\n\nexport interface DehydratedState {\n mutations: DehydratedMutation[]\n queries: DehydratedQuery[]\n}\n\nexport type ShouldDehydrateQueryFunction = (query: Query) => boolean\n\nexport type ShouldDehydrateMutationFunction = (mutation: Mutation) => boolean\n\n// FUNCTIONS\n\nfunction dehydrateMutation(mutation: Mutation): DehydratedMutation {\n return {\n mutationKey: mutation.options.mutationKey,\n state: mutation.state,\n }\n}\n\n// Most config is not dehydrated but instead meant to configure again when\n// consuming the de/rehydrated data, typically with useQuery on the client.\n// Sometimes it might make sense to prefetch data on the server and include\n// in the html-payload, but not consume it on the initial render.\nfunction dehydrateQuery(query: Query): DehydratedQuery {\n return {\n state: query.state,\n queryKey: query.queryKey,\n queryHash: query.queryHash,\n }\n}\n\nexport function defaultShouldDehydrateMutation(mutation: Mutation) {\n return mutation.state.isPaused\n}\n\nexport function defaultShouldDehydrateQuery(query: Query) {\n return query.state.status === 'success'\n}\n\nexport function dehydrate(\n client: QueryClient,\n options: DehydrateOptions = {},\n): DehydratedState {\n const mutations: DehydratedMutation[] = []\n const queries: DehydratedQuery[] = []\n\n if (options.dehydrateMutations !== false) {\n const shouldDehydrateMutation =\n options.shouldDehydrateMutation || defaultShouldDehydrateMutation\n\n client\n .getMutationCache()\n .getAll()\n .forEach((mutation) => {\n if (shouldDehydrateMutation(mutation)) {\n mutations.push(dehydrateMutation(mutation))\n }\n })\n }\n\n if (options.dehydrateQueries !== false) {\n const shouldDehydrateQuery =\n options.shouldDehydrateQuery || defaultShouldDehydrateQuery\n\n client\n .getQueryCache()\n .getAll()\n .forEach((query) => {\n if (shouldDehydrateQuery(query)) {\n queries.push(dehydrateQuery(query))\n }\n })\n }\n\n return { mutations, queries }\n}\n\nexport function hydrate(\n client: QueryClient,\n dehydratedState: unknown,\n options?: HydrateOptions,\n): void {\n if (typeof dehydratedState !== 'object' || dehydratedState === null) {\n return\n }\n\n const mutationCache = client.getMutationCache()\n const queryCache = client.getQueryCache()\n\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n const mutations = (dehydratedState as DehydratedState).mutations || []\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n const queries = (dehydratedState as DehydratedState).queries || []\n\n mutations.forEach((dehydratedMutation) => {\n mutationCache.build(\n client,\n {\n ...options?.defaultOptions?.mutations,\n mutationKey: dehydratedMutation.mutationKey,\n },\n dehydratedMutation.state,\n )\n })\n\n queries.forEach((
|
|
1
|
+
{"version":3,"file":"hydration.mjs","sources":["../../src/hydration.ts"],"sourcesContent":["import type { QueryClient } from './queryClient'\nimport type { Query, QueryState } from './query'\nimport type {\n MutationKey,\n MutationOptions,\n QueryKey,\n QueryOptions,\n} from './types'\nimport type { Mutation, MutationState } from './mutation'\n\n// TYPES\n\nexport interface DehydrateOptions {\n dehydrateMutations?: boolean\n dehydrateQueries?: boolean\n shouldDehydrateMutation?: ShouldDehydrateMutationFunction\n shouldDehydrateQuery?: ShouldDehydrateQueryFunction\n}\n\nexport interface HydrateOptions {\n defaultOptions?: {\n queries?: QueryOptions\n mutations?: MutationOptions\n }\n}\n\ninterface DehydratedMutation {\n mutationKey?: MutationKey\n state: MutationState\n}\n\ninterface DehydratedQuery {\n queryHash: string\n queryKey: QueryKey\n state: QueryState\n}\n\nexport interface DehydratedState {\n mutations: DehydratedMutation[]\n queries: DehydratedQuery[]\n}\n\nexport type ShouldDehydrateQueryFunction = (query: Query) => boolean\n\nexport type ShouldDehydrateMutationFunction = (mutation: Mutation) => boolean\n\n// FUNCTIONS\n\nfunction dehydrateMutation(mutation: Mutation): DehydratedMutation {\n return {\n mutationKey: mutation.options.mutationKey,\n state: mutation.state,\n }\n}\n\n// Most config is not dehydrated but instead meant to configure again when\n// consuming the de/rehydrated data, typically with useQuery on the client.\n// Sometimes it might make sense to prefetch data on the server and include\n// in the html-payload, but not consume it on the initial render.\nfunction dehydrateQuery(query: Query): DehydratedQuery {\n return {\n state: query.state,\n queryKey: query.queryKey,\n queryHash: query.queryHash,\n }\n}\n\nexport function defaultShouldDehydrateMutation(mutation: Mutation) {\n return mutation.state.isPaused\n}\n\nexport function defaultShouldDehydrateQuery(query: Query) {\n return query.state.status === 'success'\n}\n\nexport function dehydrate(\n client: QueryClient,\n options: DehydrateOptions = {},\n): DehydratedState {\n const mutations: DehydratedMutation[] = []\n const queries: DehydratedQuery[] = []\n\n if (options.dehydrateMutations !== false) {\n const shouldDehydrateMutation =\n options.shouldDehydrateMutation || defaultShouldDehydrateMutation\n\n client\n .getMutationCache()\n .getAll()\n .forEach((mutation) => {\n if (shouldDehydrateMutation(mutation)) {\n mutations.push(dehydrateMutation(mutation))\n }\n })\n }\n\n if (options.dehydrateQueries !== false) {\n const shouldDehydrateQuery =\n options.shouldDehydrateQuery || defaultShouldDehydrateQuery\n\n client\n .getQueryCache()\n .getAll()\n .forEach((query) => {\n if (shouldDehydrateQuery(query)) {\n queries.push(dehydrateQuery(query))\n }\n })\n }\n\n return { mutations, queries }\n}\n\nexport function hydrate(\n client: QueryClient,\n dehydratedState: unknown,\n options?: HydrateOptions,\n): void {\n if (typeof dehydratedState !== 'object' || dehydratedState === null) {\n return\n }\n\n const mutationCache = client.getMutationCache()\n const queryCache = client.getQueryCache()\n\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n const mutations = (dehydratedState as DehydratedState).mutations || []\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n const queries = (dehydratedState as DehydratedState).queries || []\n\n mutations.forEach((dehydratedMutation) => {\n mutationCache.build(\n client,\n {\n ...options?.defaultOptions?.mutations,\n mutationKey: dehydratedMutation.mutationKey,\n },\n dehydratedMutation.state,\n )\n })\n\n queries.forEach(({ queryKey, state, queryHash }) => {\n const query = queryCache.get(queryHash)\n\n // Do not hydrate if an existing query exists with newer data\n if (query) {\n if (query.state.dataUpdatedAt < state.dataUpdatedAt) {\n // omit fetchStatus from dehydrated state\n // so that query stays in its current fetchStatus\n const { fetchStatus: _ignored, ...dehydratedQueryState } = state\n query.setState(dehydratedQueryState)\n }\n return\n }\n\n // Restore query\n queryCache.build(\n client,\n {\n ...options?.defaultOptions?.queries,\n queryKey,\n queryHash,\n },\n // Reset fetch status to idle to avoid\n // query being stuck in fetching state upon hydration\n {\n ...state,\n fetchStatus: 'idle',\n },\n )\n })\n}\n"],"names":["dehydrateMutation","mutation","mutationKey","options","state","dehydrateQuery","query","queryKey","queryHash","defaultShouldDehydrateMutation","isPaused","defaultShouldDehydrateQuery","status","dehydrate","client","mutations","queries","dehydrateMutations","shouldDehydrateMutation","getMutationCache","getAll","forEach","push","dehydrateQueries","shouldDehydrateQuery","getQueryCache","hydrate","dehydratedState","mutationCache","queryCache","dehydratedMutation","build","defaultOptions","get","dataUpdatedAt","fetchStatus","_ignored","dehydratedQueryState","setState"],"mappings":"AAUA;AAoCA;AAEA,SAASA,iBAAT,CAA2BC,QAA3B,EAAmE;EACjE,OAAO;AACLC,IAAAA,WAAW,EAAED,QAAQ,CAACE,OAAT,CAAiBD,WADzB;IAELE,KAAK,EAAEH,QAAQ,CAACG,KAAAA;GAFlB,CAAA;AAID;AAGD;AACA;AACA;;;AACA,SAASC,cAAT,CAAwBC,KAAxB,EAAuD;EACrD,OAAO;IACLF,KAAK,EAAEE,KAAK,CAACF,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AY8B,aAAZ,GAA4B9B,KAAK,CAAC8B,aAAtC,EAAqD;AACnD;AACA;QACA,MAAM;AAAEC,UAAAA,WAAW,EAAEC,QAAf;UAAyB,GAAGC,oBAAAA;AAA5B,SAAA,GAAqDjC,KAA3D,CAAA;QACAE,KAAK,CAACgC,QAAN,CAAeD,oBAAf,CAAA,CAAA;AACD,OAAA;;AACD,MAAA,OAAA;AACD,KAZiD;;;AAelDR,IAAAA,UAAU,CAACE,KAAX,CACEjB,MADF,EAEE,EACE,IAAGX,OAAH,IAAA,IAAA,GAAA,KAAA,CAAA,GAAA,CAAA,sBAAA,GAAGA,OAAO,CAAE6B,cAAZ,KAAG,IAAA,GAAA,KAAA,CAAA,GAAA,sBAAA,CAAyBhB,OAA5B,CADF;MAEET,QAFF;AAGEC,MAAAA,SAAAA;AAHF,KAFF;AAQE;AACA,IAAA,EACE,GAAGJ,KADL;AAEE+B,MAAAA,WAAW,EAAE,MAAA;KAXjB,CAAA,CAAA;GAfF,CAAA,CAAA;AA8BD;;;;"}
|
|
@@ -3239,18 +3239,23 @@
|
|
|
3239
3239
|
mutationKey: dehydratedMutation.mutationKey
|
|
3240
3240
|
}, dehydratedMutation.state);
|
|
3241
3241
|
});
|
|
3242
|
-
queries.forEach(
|
|
3242
|
+
queries.forEach(({
|
|
3243
|
+
queryKey,
|
|
3244
|
+
state,
|
|
3245
|
+
queryHash
|
|
3246
|
+
}) => {
|
|
3243
3247
|
var _options$defaultOptio2;
|
|
3244
3248
|
|
|
3245
|
-
const query = queryCache.get(
|
|
3246
|
-
// query being stuck in fetching state upon hydration
|
|
3247
|
-
|
|
3248
|
-
const dehydratedQueryState = { ...dehydratedQuery.state,
|
|
3249
|
-
fetchStatus: 'idle'
|
|
3250
|
-
}; // Do not hydrate if an existing query exists with newer data
|
|
3249
|
+
const query = queryCache.get(queryHash); // Do not hydrate if an existing query exists with newer data
|
|
3251
3250
|
|
|
3252
3251
|
if (query) {
|
|
3253
|
-
if (query.state.dataUpdatedAt <
|
|
3252
|
+
if (query.state.dataUpdatedAt < state.dataUpdatedAt) {
|
|
3253
|
+
// omit fetchStatus from dehydrated state
|
|
3254
|
+
// so that query stays in its current fetchStatus
|
|
3255
|
+
const {
|
|
3256
|
+
fetchStatus: _ignored,
|
|
3257
|
+
...dehydratedQueryState
|
|
3258
|
+
} = state;
|
|
3254
3259
|
query.setState(dehydratedQueryState);
|
|
3255
3260
|
}
|
|
3256
3261
|
|
|
@@ -3259,9 +3264,13 @@
|
|
|
3259
3264
|
|
|
3260
3265
|
|
|
3261
3266
|
queryCache.build(client, { ...(options == null ? void 0 : (_options$defaultOptio2 = options.defaultOptions) == null ? void 0 : _options$defaultOptio2.queries),
|
|
3262
|
-
queryKey
|
|
3263
|
-
queryHash
|
|
3264
|
-
},
|
|
3267
|
+
queryKey,
|
|
3268
|
+
queryHash
|
|
3269
|
+
}, // Reset fetch status to idle to avoid
|
|
3270
|
+
// query being stuck in fetching state upon hydration
|
|
3271
|
+
{ ...state,
|
|
3272
|
+
fetchStatus: 'idle'
|
|
3273
|
+
});
|
|
3265
3274
|
});
|
|
3266
3275
|
}
|
|
3267
3276
|
|