hydration.esm.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // TYPES
  2. // FUNCTIONS
  3. function dehydrateMutation(mutation) {
  4. return {
  5. mutationKey: mutation.options.mutationKey,
  6. state: mutation.state
  7. };
  8. } // Most config is not dehydrated but instead meant to configure again when
  9. // consuming the de/rehydrated data, typically with useQuery on the client.
  10. // Sometimes it might make sense to prefetch data on the server and include
  11. // in the html-payload, but not consume it on the initial render.
  12. function dehydrateQuery(query) {
  13. return {
  14. state: query.state,
  15. queryKey: query.queryKey,
  16. queryHash: query.queryHash
  17. };
  18. }
  19. function defaultShouldDehydrateMutation(mutation) {
  20. return mutation.state.isPaused;
  21. }
  22. function defaultShouldDehydrateQuery(query) {
  23. return query.state.status === 'success';
  24. }
  25. function dehydrate(client, options = {}) {
  26. const mutations = [];
  27. const queries = [];
  28. if (options.dehydrateMutations !== false) {
  29. const shouldDehydrateMutation = options.shouldDehydrateMutation || defaultShouldDehydrateMutation;
  30. client.getMutationCache().getAll().forEach(mutation => {
  31. if (shouldDehydrateMutation(mutation)) {
  32. mutations.push(dehydrateMutation(mutation));
  33. }
  34. });
  35. }
  36. if (options.dehydrateQueries !== false) {
  37. const shouldDehydrateQuery = options.shouldDehydrateQuery || defaultShouldDehydrateQuery;
  38. client.getQueryCache().getAll().forEach(query => {
  39. if (shouldDehydrateQuery(query)) {
  40. queries.push(dehydrateQuery(query));
  41. }
  42. });
  43. }
  44. return {
  45. mutations,
  46. queries
  47. };
  48. }
  49. function hydrate(client, dehydratedState, options) {
  50. if (typeof dehydratedState !== 'object' || dehydratedState === null) {
  51. return;
  52. }
  53. const mutationCache = client.getMutationCache();
  54. const queryCache = client.getQueryCache(); // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
  55. const mutations = dehydratedState.mutations || []; // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
  56. const queries = dehydratedState.queries || [];
  57. mutations.forEach(dehydratedMutation => {
  58. var _options$defaultOptio;
  59. mutationCache.build(client, { ...(options == null ? void 0 : (_options$defaultOptio = options.defaultOptions) == null ? void 0 : _options$defaultOptio.mutations),
  60. mutationKey: dehydratedMutation.mutationKey
  61. }, dehydratedMutation.state);
  62. });
  63. queries.forEach(({
  64. queryKey,
  65. state,
  66. queryHash
  67. }) => {
  68. var _options$defaultOptio2;
  69. const query = queryCache.get(queryHash); // Do not hydrate if an existing query exists with newer data
  70. if (query) {
  71. if (query.state.dataUpdatedAt < state.dataUpdatedAt) {
  72. // omit fetchStatus from dehydrated state
  73. // so that query stays in its current fetchStatus
  74. const {
  75. fetchStatus: _ignored,
  76. ...dehydratedQueryState
  77. } = state;
  78. query.setState(dehydratedQueryState);
  79. }
  80. return;
  81. } // Restore query
  82. queryCache.build(client, { ...(options == null ? void 0 : (_options$defaultOptio2 = options.defaultOptions) == null ? void 0 : _options$defaultOptio2.queries),
  83. queryKey,
  84. queryHash
  85. }, // Reset fetch status to idle to avoid
  86. // query being stuck in fetching state upon hydration
  87. { ...state,
  88. fetchStatus: 'idle'
  89. });
  90. });
  91. }
  92. export { defaultShouldDehydrateMutation, defaultShouldDehydrateQuery, dehydrate, hydrate };
  93. //# sourceMappingURL=hydration.esm.js.map