'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); // TYPES // FUNCTIONS function dehydrateMutation(mutation) { return { mutationKey: mutation.options.mutationKey, state: mutation.state }; } // Most config is not dehydrated but instead meant to configure again when // consuming the de/rehydrated data, typically with useQuery on the client. // Sometimes it might make sense to prefetch data on the server and include // in the html-payload, but not consume it on the initial render. function dehydrateQuery(query) { return { state: query.state, queryKey: query.queryKey, queryHash: query.queryHash }; } function defaultShouldDehydrateMutation(mutation) { return mutation.state.isPaused; } function defaultShouldDehydrateQuery(query) { return query.state.status === 'success'; } function dehydrate(client, options = {}) { const mutations = []; const queries = []; if (options.dehydrateMutations !== false) { const shouldDehydrateMutation = options.shouldDehydrateMutation || defaultShouldDehydrateMutation; client.getMutationCache().getAll().forEach(mutation => { if (shouldDehydrateMutation(mutation)) { mutations.push(dehydrateMutation(mutation)); } }); } if (options.dehydrateQueries !== false) { const shouldDehydrateQuery = options.shouldDehydrateQuery || defaultShouldDehydrateQuery; client.getQueryCache().getAll().forEach(query => { if (shouldDehydrateQuery(query)) { queries.push(dehydrateQuery(query)); } }); } return { mutations, queries }; } function hydrate(client, dehydratedState, options) { if (typeof dehydratedState !== 'object' || dehydratedState === null) { return; } const mutationCache = client.getMutationCache(); const queryCache = client.getQueryCache(); // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition const mutations = dehydratedState.mutations || []; // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition const queries = dehydratedState.queries || []; mutations.forEach(dehydratedMutation => { var _options$defaultOptio; mutationCache.build(client, { ...(options == null ? void 0 : (_options$defaultOptio = options.defaultOptions) == null ? void 0 : _options$defaultOptio.mutations), mutationKey: dehydratedMutation.mutationKey }, dehydratedMutation.state); }); queries.forEach(({ queryKey, state, queryHash }) => { var _options$defaultOptio2; const query = queryCache.get(queryHash); // Do not hydrate if an existing query exists with newer data if (query) { if (query.state.dataUpdatedAt < state.dataUpdatedAt) { // omit fetchStatus from dehydrated state // so that query stays in its current fetchStatus const { fetchStatus: _ignored, ...dehydratedQueryState } = state; query.setState(dehydratedQueryState); } return; } // Restore query queryCache.build(client, { ...(options == null ? void 0 : (_options$defaultOptio2 = options.defaultOptions) == null ? void 0 : _options$defaultOptio2.queries), queryKey, queryHash }, // Reset fetch status to idle to avoid // query being stuck in fetching state upon hydration { ...state, fetchStatus: 'idle' }); }); } exports.defaultShouldDehydrateMutation = defaultShouldDehydrateMutation; exports.defaultShouldDehydrateQuery = defaultShouldDehydrateQuery; exports.dehydrate = dehydrate; exports.hydrate = hydrate; //# sourceMappingURL=hydration.js.map