hydration.js 3.5 KB

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