@khanacademy/wonder-blocks-data 8.0.1 → 8.0.2

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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @khanacademy/wonder-blocks-data
2
2
 
3
+ ## 8.0.2
4
+
5
+ ### Patch Changes
6
+
7
+ - 580141ed: Always initialize SSR-only cache
8
+
3
9
  ## 8.0.1
4
10
 
5
11
  ### Patch Changes
package/dist/es/index.js CHANGED
@@ -172,11 +172,10 @@ class SsrCache {
172
172
  };
173
173
 
174
174
  this.getEntry = id => {
175
- var _this$_ssrOnlyCache$g, _this$_ssrOnlyCache;
175
+ const ssrEntry = Server.isServerSide() ? this._ssrOnlyCache.get(DefaultScope$2, id) : null;
176
+ const internalEntry = ssrEntry != null ? ssrEntry : this._hydrationCache.get(DefaultScope$2, id);
176
177
 
177
- const internalEntry = (_this$_ssrOnlyCache$g = (_this$_ssrOnlyCache = this._ssrOnlyCache) == null ? void 0 : _this$_ssrOnlyCache.get(DefaultScope$2, id)) != null ? _this$_ssrOnlyCache$g : this._hydrationCache.get(DefaultScope$2, id);
178
-
179
- if (this._ssrOnlyCache == null && internalEntry != null) {
178
+ if (!Server.isServerSide() && internalEntry != null) {
180
179
  this._hydrationCache.purge(DefaultScope$2, id);
181
180
  }
182
181
 
@@ -184,13 +183,11 @@ class SsrCache {
184
183
  };
185
184
 
186
185
  this.purgeData = predicate => {
187
- var _this$_ssrOnlyCache2;
188
-
189
186
  const realPredicate = predicate ? (_, key, cachedEntry) => predicate(key, cachedEntry) : undefined;
190
187
 
191
188
  this._hydrationCache.purgeAll(realPredicate);
192
189
 
193
- (_this$_ssrOnlyCache2 = this._ssrOnlyCache) == null ? void 0 : _this$_ssrOnlyCache2.purgeAll(realPredicate);
190
+ this._ssrOnlyCache.purgeAll(realPredicate);
194
191
  };
195
192
 
196
193
  this.cloneHydratableData = () => {
@@ -201,7 +198,7 @@ class SsrCache {
201
198
  return (_cache$DefaultScope = cache[DefaultScope$2]) != null ? _cache$DefaultScope : {};
202
199
  };
203
200
 
204
- this._ssrOnlyCache = process.env.NODE_ENV === "test" || Server.isServerSide() ? ssrOnlyCache || new SerializableInMemoryCache() : undefined;
201
+ this._ssrOnlyCache = ssrOnlyCache || new SerializableInMemoryCache();
205
202
  this._hydrationCache = hydrationCache || new SerializableInMemoryCache();
206
203
  }
207
204
 
@@ -212,9 +209,7 @@ class SsrCache {
212
209
  if (hydrate) {
213
210
  this._hydrationCache.set(DefaultScope$2, id, frozenEntry);
214
211
  } else {
215
- var _this$_ssrOnlyCache3;
216
-
217
- (_this$_ssrOnlyCache3 = this._ssrOnlyCache) == null ? void 0 : _this$_ssrOnlyCache3.set(DefaultScope$2, id, frozenEntry);
212
+ this._ssrOnlyCache.set(DefaultScope$2, id, frozenEntry);
218
213
  }
219
214
  }
220
215
 
package/dist/index.js CHANGED
@@ -165,6 +165,12 @@ module.exports = require("react");
165
165
 
166
166
  /***/ }),
167
167
  /* 2 */
168
+ /***/ (function(module, exports) {
169
+
170
+ module.exports = require("@khanacademy/wonder-blocks-core");
171
+
172
+ /***/ }),
173
+ /* 3 */
168
174
  /***/ (function(module, __webpack_exports__, __webpack_require__) {
169
175
 
170
176
  "use strict";
@@ -187,12 +193,6 @@ const FetchPolicy = __webpack_require__(22).Mirrored(["CacheBeforeNetwork", "Cac
187
193
  * and functions are not allowed.
188
194
  */
189
195
 
190
- /***/ }),
191
- /* 3 */
192
- /***/ (function(module, exports) {
193
-
194
- module.exports = require("@khanacademy/wonder-blocks-core");
195
-
196
196
  /***/ }),
197
197
  /* 4 */
198
198
  /***/ (function(module, __webpack_exports__, __webpack_require__) {
@@ -556,7 +556,7 @@ class RequestTracker {
556
556
 
557
557
  "use strict";
558
558
  /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return SsrCache; });
559
- /* harmony import */ var _khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(3);
559
+ /* harmony import */ var _khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(2);
560
560
  /* harmony import */ var _khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_0__);
561
561
  /* harmony import */ var _serializable_in_memory_cache_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(13);
562
562
 
@@ -608,14 +608,14 @@ class SsrCache {
608
608
  };
609
609
 
610
610
  this.getEntry = id => {
611
- var _this$_ssrOnlyCache$g, _this$_ssrOnlyCache;
612
-
613
611
  // Get the cached entry for this value.
614
- // We first look in the ssr cache and then the hydration cache.
615
- const internalEntry = (_this$_ssrOnlyCache$g = (_this$_ssrOnlyCache = this._ssrOnlyCache) == null ? void 0 : _this$_ssrOnlyCache.get(DefaultScope, id)) != null ? _this$_ssrOnlyCache$g : this._hydrationCache.get(DefaultScope, id); // If we are not server-side and we hydrated something, let's clear
612
+ // We first look in the ssr cache, if we need to.
613
+ const ssrEntry = _khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_0__["Server"].isServerSide() ? this._ssrOnlyCache.get(DefaultScope, id) : null; // Now we defer to the SSR value, and fallback to the hydration cache.
614
+
615
+ const internalEntry = ssrEntry != null ? ssrEntry : this._hydrationCache.get(DefaultScope, id); // If we are not server-side and we hydrated something, let's clear
616
616
  // that from the hydration cache to save memory.
617
617
 
618
- if (this._ssrOnlyCache == null && internalEntry != null) {
618
+ if (!_khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_0__["Server"].isServerSide() && internalEntry != null) {
619
619
  // We now delete this from our hydration cache as we don't need it.
620
620
  // This does mean that if another handler of the same type but
621
621
  // without some sort of linked cache won't get the value, but
@@ -632,8 +632,6 @@ class SsrCache {
632
632
  };
633
633
 
634
634
  this.purgeData = predicate => {
635
- var _this$_ssrOnlyCache2;
636
-
637
635
  const realPredicate = predicate ? // We know what we're putting into the cache so let's assume it
638
636
  // conforms.
639
637
  // $FlowIgnore[incompatible-call]
@@ -641,7 +639,7 @@ class SsrCache {
641
639
 
642
640
  this._hydrationCache.purgeAll(realPredicate);
643
641
 
644
- (_this$_ssrOnlyCache2 = this._ssrOnlyCache) == null ? void 0 : _this$_ssrOnlyCache2.purgeAll(realPredicate);
642
+ this._ssrOnlyCache.purgeAll(realPredicate);
645
643
  };
646
644
 
647
645
  this.cloneHydratableData = () => {
@@ -658,13 +656,7 @@ class SsrCache {
658
656
  return (_cache$DefaultScope = cache[DefaultScope]) != null ? _cache$DefaultScope : {};
659
657
  };
660
658
 
661
- // The default instance gets made on first reference and if that happens
662
- // before server-side mode is turned on, the Default instance would
663
- // never have an SSR-only cache instance, which would then mean that if
664
- // server-side mode got turned on, it wouldn't work right.
665
- // This should only be an issue of surprise during testing, so, let's
666
- // always have an instance in that circumstance.
667
- this._ssrOnlyCache = false || _khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_0__["Server"].isServerSide() ? ssrOnlyCache || new _serializable_in_memory_cache_js__WEBPACK_IMPORTED_MODULE_1__[/* SerializableInMemoryCache */ "a"]() : undefined;
659
+ this._ssrOnlyCache = ssrOnlyCache || new _serializable_in_memory_cache_js__WEBPACK_IMPORTED_MODULE_1__[/* SerializableInMemoryCache */ "a"]();
668
660
  this._hydrationCache = hydrationCache || new _serializable_in_memory_cache_js__WEBPACK_IMPORTED_MODULE_1__[/* SerializableInMemoryCache */ "a"]();
669
661
  }
670
662
 
@@ -677,12 +669,10 @@ class SsrCache {
677
669
  if (hydrate) {
678
670
  this._hydrationCache.set(DefaultScope, id, frozenEntry);
679
671
  } else {
680
- var _this$_ssrOnlyCache3;
681
-
682
672
  // Usually, when server-side, this cache will always be present.
683
673
  // We do fake server-side in our doc example though, when it
684
674
  // won't be.
685
- (_this$_ssrOnlyCache3 = this._ssrOnlyCache) == null ? void 0 : _this$_ssrOnlyCache3.set(DefaultScope, id, frozenEntry);
675
+ this._ssrOnlyCache.set(DefaultScope, id, frozenEntry);
686
676
  }
687
677
  }
688
678
 
@@ -927,7 +917,7 @@ class ScopedInMemoryCache {
927
917
  /* harmony import */ var _use_server_effect_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(16);
928
918
  /* harmony import */ var _use_shared_cache_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(5);
929
919
  /* harmony import */ var _use_cached_effect_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(17);
930
- /* harmony import */ var _util_types_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(2);
920
+ /* harmony import */ var _util_types_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(3);
931
921
 
932
922
 
933
923
 
@@ -1129,7 +1119,7 @@ const purgeHydrationCache = predicate => _ssr_cache_js__WEBPACK_IMPORTED_MODULE_
1129
1119
 
1130
1120
  "use strict";
1131
1121
  /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return useServerEffect; });
1132
- /* harmony import */ var _khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(3);
1122
+ /* harmony import */ var _khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(2);
1133
1123
  /* harmony import */ var _khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_0__);
1134
1124
  /* harmony import */ var react__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(1);
1135
1125
  /* harmony import */ var react__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_1__);
@@ -1194,14 +1184,14 @@ const useServerEffect = (requestId, handler, options = {}) => {
1194
1184
  /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return useCachedEffect; });
1195
1185
  /* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(1);
1196
1186
  /* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_0__);
1197
- /* harmony import */ var _khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(3);
1187
+ /* harmony import */ var _khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(2);
1198
1188
  /* harmony import */ var _khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_1__);
1199
1189
  /* harmony import */ var _util_data_error_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(0);
1200
1190
  /* harmony import */ var _util_request_fulfillment_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(10);
1201
1191
  /* harmony import */ var _util_status_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(6);
1202
1192
  /* harmony import */ var _use_shared_cache_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(5);
1203
1193
  /* harmony import */ var _use_request_interception_js__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(19);
1204
- /* harmony import */ var _util_types_js__WEBPACK_IMPORTED_MODULE_7__ = __webpack_require__(2);
1194
+ /* harmony import */ var _util_types_js__WEBPACK_IMPORTED_MODULE_7__ = __webpack_require__(3);
1205
1195
 
1206
1196
 
1207
1197
 
@@ -1718,7 +1708,7 @@ module.exports = Object.freeze(Enum);
1718
1708
  /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "b", function() { return fetchTrackedRequests; });
1719
1709
  /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "c", function() { return hasTrackedRequestsToBeFetched; });
1720
1710
  /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return abortInflightRequests; });
1721
- /* harmony import */ var _khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(3);
1711
+ /* harmony import */ var _khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(2);
1722
1712
  /* harmony import */ var _khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_0__);
1723
1713
  /* harmony import */ var _request_tracking_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(8);
1724
1714
  /* harmony import */ var _request_fulfillment_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(10);
@@ -1818,7 +1808,7 @@ const purgeCaches = () => {
1818
1808
  /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return TrackData; });
1819
1809
  /* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(1);
1820
1810
  /* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_0__);
1821
- /* harmony import */ var _khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(3);
1811
+ /* harmony import */ var _khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(2);
1822
1812
  /* harmony import */ var _khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_1__);
1823
1813
  /* harmony import */ var _util_request_tracking_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(8);
1824
1814
 
@@ -2272,7 +2262,7 @@ const getGqlDataFromResponse = async response => {
2272
2262
 
2273
2263
  "use strict";
2274
2264
  __webpack_require__.r(__webpack_exports__);
2275
- /* harmony import */ var _util_types_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(2);
2265
+ /* harmony import */ var _util_types_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(3);
2276
2266
  /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "FetchPolicy", function() { return _util_types_js__WEBPACK_IMPORTED_MODULE_0__["a"]; });
2277
2267
 
2278
2268
  /* harmony import */ var _util_hydration_cache_api_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(15);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@khanacademy/wonder-blocks-data",
3
- "version": "8.0.1",
3
+ "version": "8.0.2",
4
4
  "design": "v1",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -17,61 +17,51 @@ describe("../ssr-cache.js", () => {
17
17
  });
18
18
 
19
19
  describe("#constructor", () => {
20
- const NODE_ENV = process.env.NODE_ENV;
21
-
22
- afterEach(() => {
23
- if (NODE_ENV == null) {
24
- delete process.env.NODE_ENV;
25
- } else {
26
- process.env.NODE_ENV = NODE_ENV;
27
- }
20
+ it("should default the ssr-only cache to a cache instance", () => {
21
+ // Arrange
22
+
23
+ // Act
24
+ const cache = new SsrCache();
25
+
26
+ // Assert
27
+ expect(cache._ssrOnlyCache).toBeInstanceOf(
28
+ SerializableInMemoryCache,
29
+ );
28
30
  });
29
31
 
30
- it.each(["development", "production"])(
31
- "should default the ssr-only cache to a undefined when client-side in %s",
32
- (nodeEnv) => {
33
- // Arrange
34
- jest.spyOn(Server, "isServerSide").mockReturnValue(false);
35
- process.env.NODE_ENV = nodeEnv;
32
+ it("should set the hydration cache to the passed instance if there is one", () => {
33
+ // Arrange
34
+ const passedInstance = new SerializableInMemoryCache();
36
35
 
37
- // Act
38
- const cache = new SsrCache();
36
+ // Act
37
+ const cache = new SsrCache(null, passedInstance);
39
38
 
40
- // Assert
41
- expect(cache._ssrOnlyCache).toBeUndefined();
42
- },
43
- );
39
+ // Assert
40
+ expect(cache._ssrOnlyCache).toBe(passedInstance);
41
+ });
44
42
 
45
- it("should default the ssr-only cache to a cache instance when client-side in test", () => {
43
+ it("should default the hydration cache to a cache instance", () => {
46
44
  // Arrange
47
- jest.spyOn(Server, "isServerSide").mockReturnValue(false);
48
- process.env.NODE_ENV = "test";
49
45
 
50
46
  // Act
51
47
  const cache = new SsrCache();
52
48
 
53
49
  // Assert
54
- expect(cache._ssrOnlyCache).toBeInstanceOf(
50
+ expect(cache._hydrationCache).toBeInstanceOf(
55
51
  SerializableInMemoryCache,
56
52
  );
57
53
  });
58
54
 
59
- it.each(["development", "production"])(
60
- "should default the ssr-only cache to a cache instance when server-side in %s",
61
- (nodeEnv) => {
62
- // Arrange
63
- jest.spyOn(Server, "isServerSide").mockReturnValue(true);
64
- process.env.NODE_ENV = nodeEnv;
55
+ it("should set the hydration cache to the passed instance if there is one", () => {
56
+ // Arrange
57
+ const passedInstance = new SerializableInMemoryCache();
65
58
 
66
- // Act
67
- const cache = new SsrCache();
59
+ // Act
60
+ const cache = new SsrCache(passedInstance);
68
61
 
69
- // Assert
70
- expect(cache._ssrOnlyCache).toBeInstanceOf(
71
- SerializableInMemoryCache,
72
- );
73
- },
74
- );
62
+ // Assert
63
+ expect(cache._hydrationCache).toBe(passedInstance);
64
+ });
75
65
  });
76
66
 
77
67
  describe("@Default", () => {
@@ -26,22 +26,13 @@ export class SsrCache {
26
26
  }
27
27
 
28
28
  _hydrationCache: SerializableInMemoryCache;
29
- _ssrOnlyCache: ?SerializableInMemoryCache;
29
+ _ssrOnlyCache: SerializableInMemoryCache;
30
30
 
31
31
  constructor(
32
32
  hydrationCache: ?SerializableInMemoryCache = null,
33
33
  ssrOnlyCache: ?SerializableInMemoryCache = null,
34
34
  ) {
35
- // The default instance gets made on first reference and if that happens
36
- // before server-side mode is turned on, the Default instance would
37
- // never have an SSR-only cache instance, which would then mean that if
38
- // server-side mode got turned on, it wouldn't work right.
39
- // This should only be an issue of surprise during testing, so, let's
40
- // always have an instance in that circumstance.
41
- this._ssrOnlyCache =
42
- process.env.NODE_ENV === "test" || Server.isServerSide()
43
- ? ssrOnlyCache || new SerializableInMemoryCache()
44
- : undefined;
35
+ this._ssrOnlyCache = ssrOnlyCache || new SerializableInMemoryCache();
45
36
  this._hydrationCache =
46
37
  hydrationCache || new SerializableInMemoryCache();
47
38
  }
@@ -61,7 +52,7 @@ export class SsrCache {
61
52
  // Usually, when server-side, this cache will always be present.
62
53
  // We do fake server-side in our doc example though, when it
63
54
  // won't be.
64
- this._ssrOnlyCache?.set(DefaultScope, id, frozenEntry);
55
+ this._ssrOnlyCache.set(DefaultScope, id, frozenEntry);
65
56
  }
66
57
  }
67
58
  return frozenEntry;
@@ -127,14 +118,18 @@ export class SsrCache {
127
118
  ): ?$ReadOnly<CachedResponse<TData>> => {
128
119
  // Get the cached entry for this value.
129
120
 
130
- // We first look in the ssr cache and then the hydration cache.
121
+ // We first look in the ssr cache, if we need to.
122
+ const ssrEntry = Server.isServerSide()
123
+ ? this._ssrOnlyCache.get(DefaultScope, id)
124
+ : null;
125
+
126
+ // Now we defer to the SSR value, and fallback to the hydration cache.
131
127
  const internalEntry =
132
- this._ssrOnlyCache?.get(DefaultScope, id) ??
133
- this._hydrationCache.get(DefaultScope, id);
128
+ ssrEntry ?? this._hydrationCache.get(DefaultScope, id);
134
129
 
135
130
  // If we are not server-side and we hydrated something, let's clear
136
131
  // that from the hydration cache to save memory.
137
- if (this._ssrOnlyCache == null && internalEntry != null) {
132
+ if (!Server.isServerSide() && internalEntry != null) {
138
133
  // We now delete this from our hydration cache as we don't need it.
139
134
  // This does mean that if another handler of the same type but
140
135
  // without some sort of linked cache won't get the value, but
@@ -172,7 +167,7 @@ export class SsrCache {
172
167
 
173
168
  // Apply the predicate to what we have in our caches.
174
169
  this._hydrationCache.purgeAll(realPredicate);
175
- this._ssrOnlyCache?.purgeAll(realPredicate);
170
+ this._ssrOnlyCache.purgeAll(realPredicate);
176
171
  };
177
172
 
178
173
  /**