@khanacademy/wonder-blocks-data 8.0.1 → 8.0.4
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 +22 -0
- package/dist/es/index.js +8 -15
- package/dist/index.js +28 -167
- package/package.json +4 -3
- package/src/hooks/use-cached-effect.js +3 -14
- package/src/hooks/use-hydratable-effect.js +0 -4
- package/src/util/__tests__/ssr-cache.test.js +28 -38
- package/src/util/ssr-cache.js +12 -17
- package/src/util/types.js +0 -4
- package/src/__tests__/__snapshots__/generated-snapshot.test.js.snap +0 -337
- package/src/__tests__/generated-snapshot.test.js +0 -350
package/src/util/ssr-cache.js
CHANGED
|
@@ -26,22 +26,13 @@ export class SsrCache {
|
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
_hydrationCache: SerializableInMemoryCache;
|
|
29
|
-
_ssrOnlyCache:
|
|
29
|
+
_ssrOnlyCache: SerializableInMemoryCache;
|
|
30
30
|
|
|
31
31
|
constructor(
|
|
32
32
|
hydrationCache: ?SerializableInMemoryCache = null,
|
|
33
33
|
ssrOnlyCache: ?SerializableInMemoryCache = null,
|
|
34
34
|
) {
|
|
35
|
-
|
|
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
|
|
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
|
|
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.
|
|
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 (
|
|
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
|
|
170
|
+
this._ssrOnlyCache.purgeAll(realPredicate);
|
|
176
171
|
};
|
|
177
172
|
|
|
178
173
|
/**
|
package/src/util/types.js
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
// @flow
|
|
2
2
|
import type {Metadata} from "@khanacademy/wonder-stuff-core";
|
|
3
3
|
|
|
4
|
-
// TODO(somewhatabstract, FEI-4172): Update eslint-plugin-flowtype when
|
|
5
|
-
// they've fixed https://github.com/gajus/eslint-plugin-flowtype/issues/502
|
|
6
|
-
/* eslint-disable no-undef */
|
|
7
4
|
/**
|
|
8
5
|
* Defines the various fetch policies that can be applied to requests.
|
|
9
6
|
*/
|
|
@@ -30,7 +27,6 @@ export enum FetchPolicy {
|
|
|
30
27
|
*/
|
|
31
28
|
NetworkOnly,
|
|
32
29
|
}
|
|
33
|
-
/* eslint-enable no-undef */
|
|
34
30
|
|
|
35
31
|
/**
|
|
36
32
|
* Define what can be cached.
|
|
@@ -1,337 +0,0 @@
|
|
|
1
|
-
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
|
2
|
-
|
|
3
|
-
exports[`wonder-blocks-data example 1 1`] = `
|
|
4
|
-
<div
|
|
5
|
-
className=""
|
|
6
|
-
style={
|
|
7
|
-
Object {
|
|
8
|
-
"alignItems": "stretch",
|
|
9
|
-
"borderStyle": "solid",
|
|
10
|
-
"borderWidth": 0,
|
|
11
|
-
"boxSizing": "border-box",
|
|
12
|
-
"display": "flex",
|
|
13
|
-
"flexDirection": "column",
|
|
14
|
-
"margin": 0,
|
|
15
|
-
"minHeight": 0,
|
|
16
|
-
"minWidth": 0,
|
|
17
|
-
"padding": 0,
|
|
18
|
-
"position": "relative",
|
|
19
|
-
"zIndex": 0,
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
>
|
|
23
|
-
<div
|
|
24
|
-
className=""
|
|
25
|
-
style={
|
|
26
|
-
Object {
|
|
27
|
-
"alignItems": "stretch",
|
|
28
|
-
"borderStyle": "solid",
|
|
29
|
-
"borderWidth": 0,
|
|
30
|
-
"boxSizing": "border-box",
|
|
31
|
-
"display": "flex",
|
|
32
|
-
"flexDirection": "column",
|
|
33
|
-
"margin": 0,
|
|
34
|
-
"minHeight": 0,
|
|
35
|
-
"minWidth": 0,
|
|
36
|
-
"padding": 0,
|
|
37
|
-
"position": "relative",
|
|
38
|
-
"zIndex": 0,
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
>
|
|
42
|
-
<span
|
|
43
|
-
className=""
|
|
44
|
-
style={
|
|
45
|
-
Object {
|
|
46
|
-
"MozOsxFontSmoothing": "grayscale",
|
|
47
|
-
"WebkitFontSmoothing": "antialiased",
|
|
48
|
-
"display": "block",
|
|
49
|
-
"fontFamily": "Lato, \\"Noto Sans\\", sans-serif",
|
|
50
|
-
"fontSize": 16,
|
|
51
|
-
"fontWeight": 400,
|
|
52
|
-
"lineHeight": "22px",
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
>
|
|
56
|
-
This request will succeed and give us data!
|
|
57
|
-
</span>
|
|
58
|
-
Loading...
|
|
59
|
-
</div>
|
|
60
|
-
<div
|
|
61
|
-
aria-hidden="true"
|
|
62
|
-
className=""
|
|
63
|
-
style={
|
|
64
|
-
Object {
|
|
65
|
-
"MsFlexBasis": 12,
|
|
66
|
-
"MsFlexPreferredSize": 12,
|
|
67
|
-
"WebkitFlexBasis": 12,
|
|
68
|
-
"alignItems": "stretch",
|
|
69
|
-
"borderStyle": "solid",
|
|
70
|
-
"borderWidth": 0,
|
|
71
|
-
"boxSizing": "border-box",
|
|
72
|
-
"display": "flex",
|
|
73
|
-
"flexBasis": 12,
|
|
74
|
-
"flexDirection": "column",
|
|
75
|
-
"flexShrink": 0,
|
|
76
|
-
"margin": 0,
|
|
77
|
-
"minHeight": 0,
|
|
78
|
-
"minWidth": 0,
|
|
79
|
-
"padding": 0,
|
|
80
|
-
"position": "relative",
|
|
81
|
-
"width": 12,
|
|
82
|
-
"zIndex": 0,
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
/>
|
|
86
|
-
<div
|
|
87
|
-
className=""
|
|
88
|
-
style={
|
|
89
|
-
Object {
|
|
90
|
-
"alignItems": "stretch",
|
|
91
|
-
"borderStyle": "solid",
|
|
92
|
-
"borderWidth": 0,
|
|
93
|
-
"boxSizing": "border-box",
|
|
94
|
-
"display": "flex",
|
|
95
|
-
"flexDirection": "column",
|
|
96
|
-
"margin": 0,
|
|
97
|
-
"minHeight": 0,
|
|
98
|
-
"minWidth": 0,
|
|
99
|
-
"padding": 0,
|
|
100
|
-
"position": "relative",
|
|
101
|
-
"zIndex": 0,
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
>
|
|
105
|
-
<span
|
|
106
|
-
className=""
|
|
107
|
-
style={
|
|
108
|
-
Object {
|
|
109
|
-
"MozOsxFontSmoothing": "grayscale",
|
|
110
|
-
"WebkitFontSmoothing": "antialiased",
|
|
111
|
-
"display": "block",
|
|
112
|
-
"fontFamily": "Lato, \\"Noto Sans\\", sans-serif",
|
|
113
|
-
"fontSize": 16,
|
|
114
|
-
"fontWeight": 400,
|
|
115
|
-
"lineHeight": "22px",
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
>
|
|
119
|
-
This request will go boom and give us an error!
|
|
120
|
-
</span>
|
|
121
|
-
Loading...
|
|
122
|
-
</div>
|
|
123
|
-
</div>
|
|
124
|
-
`;
|
|
125
|
-
|
|
126
|
-
exports[`wonder-blocks-data example 2 1`] = `
|
|
127
|
-
<div
|
|
128
|
-
className=""
|
|
129
|
-
style={
|
|
130
|
-
Object {
|
|
131
|
-
"alignItems": "stretch",
|
|
132
|
-
"borderStyle": "solid",
|
|
133
|
-
"borderWidth": 0,
|
|
134
|
-
"boxSizing": "border-box",
|
|
135
|
-
"display": "flex",
|
|
136
|
-
"flexDirection": "column",
|
|
137
|
-
"margin": 0,
|
|
138
|
-
"minHeight": 0,
|
|
139
|
-
"minWidth": 0,
|
|
140
|
-
"padding": 0,
|
|
141
|
-
"position": "relative",
|
|
142
|
-
"zIndex": 0,
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
>
|
|
146
|
-
<div
|
|
147
|
-
className=""
|
|
148
|
-
style={
|
|
149
|
-
Object {
|
|
150
|
-
"alignItems": "stretch",
|
|
151
|
-
"borderStyle": "solid",
|
|
152
|
-
"borderWidth": 0,
|
|
153
|
-
"boxSizing": "border-box",
|
|
154
|
-
"display": "flex",
|
|
155
|
-
"flexDirection": "column",
|
|
156
|
-
"margin": 0,
|
|
157
|
-
"minHeight": 0,
|
|
158
|
-
"minWidth": 0,
|
|
159
|
-
"padding": 0,
|
|
160
|
-
"position": "relative",
|
|
161
|
-
"zIndex": 0,
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
>
|
|
165
|
-
<span
|
|
166
|
-
className=""
|
|
167
|
-
style={
|
|
168
|
-
Object {
|
|
169
|
-
"MozOsxFontSmoothing": "grayscale",
|
|
170
|
-
"WebkitFontSmoothing": "antialiased",
|
|
171
|
-
"display": "block",
|
|
172
|
-
"fontFamily": "Lato, \\"Noto Sans\\", sans-serif",
|
|
173
|
-
"fontSize": 16,
|
|
174
|
-
"fontWeight": 400,
|
|
175
|
-
"lineHeight": "22px",
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
>
|
|
179
|
-
This cache has data!
|
|
180
|
-
</span>
|
|
181
|
-
<span
|
|
182
|
-
className=""
|
|
183
|
-
style={
|
|
184
|
-
Object {
|
|
185
|
-
"MozOsxFontSmoothing": "grayscale",
|
|
186
|
-
"WebkitFontSmoothing": "antialiased",
|
|
187
|
-
"display": "block",
|
|
188
|
-
"fontFamily": "Inconsolata, monospace",
|
|
189
|
-
"fontSize": 17,
|
|
190
|
-
"fontWeight": 400,
|
|
191
|
-
"lineHeight": "22px",
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
>
|
|
195
|
-
I'm DATA from the hydration cache
|
|
196
|
-
</span>
|
|
197
|
-
</div>
|
|
198
|
-
</div>
|
|
199
|
-
`;
|
|
200
|
-
|
|
201
|
-
exports[`wonder-blocks-data example 3 1`] = `
|
|
202
|
-
<div
|
|
203
|
-
className=""
|
|
204
|
-
style={
|
|
205
|
-
Object {
|
|
206
|
-
"alignItems": "stretch",
|
|
207
|
-
"borderStyle": "solid",
|
|
208
|
-
"borderWidth": 0,
|
|
209
|
-
"boxSizing": "border-box",
|
|
210
|
-
"display": "flex",
|
|
211
|
-
"flexDirection": "column",
|
|
212
|
-
"margin": 0,
|
|
213
|
-
"minHeight": 0,
|
|
214
|
-
"minWidth": 0,
|
|
215
|
-
"padding": 0,
|
|
216
|
-
"position": "relative",
|
|
217
|
-
"zIndex": 0,
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
>
|
|
221
|
-
<span
|
|
222
|
-
className=""
|
|
223
|
-
style={
|
|
224
|
-
Object {
|
|
225
|
-
"MozOsxFontSmoothing": "grayscale",
|
|
226
|
-
"WebkitFontSmoothing": "antialiased",
|
|
227
|
-
"display": "block",
|
|
228
|
-
"fontFamily": "Lato, \\"Noto Sans\\", sans-serif",
|
|
229
|
-
"fontSize": 16,
|
|
230
|
-
"fontWeight": 400,
|
|
231
|
-
"lineHeight": "22px",
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
>
|
|
235
|
-
This received intercepted data!
|
|
236
|
-
</span>
|
|
237
|
-
If you see this, the example is broken!
|
|
238
|
-
</div>
|
|
239
|
-
`;
|
|
240
|
-
|
|
241
|
-
exports[`wonder-blocks-data example 4 1`] = `"Sorry, no snapshot for you"`;
|
|
242
|
-
|
|
243
|
-
exports[`wonder-blocks-data example 5 1`] = `
|
|
244
|
-
<div
|
|
245
|
-
className=""
|
|
246
|
-
style={
|
|
247
|
-
Object {
|
|
248
|
-
"alignItems": "stretch",
|
|
249
|
-
"borderStyle": "solid",
|
|
250
|
-
"borderWidth": 0,
|
|
251
|
-
"boxSizing": "border-box",
|
|
252
|
-
"display": "flex",
|
|
253
|
-
"flexDirection": "column",
|
|
254
|
-
"margin": 0,
|
|
255
|
-
"minHeight": 0,
|
|
256
|
-
"minWidth": 0,
|
|
257
|
-
"padding": 0,
|
|
258
|
-
"position": "relative",
|
|
259
|
-
"zIndex": 0,
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
>
|
|
263
|
-
<button
|
|
264
|
-
aria-disabled={false}
|
|
265
|
-
className=""
|
|
266
|
-
onBlur={[Function]}
|
|
267
|
-
onClick={[Function]}
|
|
268
|
-
onDragStart={[Function]}
|
|
269
|
-
onFocus={[Function]}
|
|
270
|
-
onKeyDown={[Function]}
|
|
271
|
-
onKeyUp={[Function]}
|
|
272
|
-
onMouseDown={[Function]}
|
|
273
|
-
onMouseEnter={[Function]}
|
|
274
|
-
onMouseLeave={[Function]}
|
|
275
|
-
onMouseUp={[Function]}
|
|
276
|
-
onTouchCancel={[Function]}
|
|
277
|
-
onTouchEnd={[Function]}
|
|
278
|
-
onTouchStart={[Function]}
|
|
279
|
-
role="button"
|
|
280
|
-
style={
|
|
281
|
-
Object {
|
|
282
|
-
"::MozFocusInner": Object {
|
|
283
|
-
"border": 0,
|
|
284
|
-
},
|
|
285
|
-
":focus": Object {
|
|
286
|
-
"WebkitTapHighlightColor": "rgba(0,0,0,0)",
|
|
287
|
-
},
|
|
288
|
-
"alignItems": "center",
|
|
289
|
-
"background": "#1865f2",
|
|
290
|
-
"border": "none",
|
|
291
|
-
"borderRadius": 4,
|
|
292
|
-
"boxSizing": "border-box",
|
|
293
|
-
"color": "#ffffff",
|
|
294
|
-
"cursor": "pointer",
|
|
295
|
-
"display": "inline-flex",
|
|
296
|
-
"height": 40,
|
|
297
|
-
"justifyContent": "center",
|
|
298
|
-
"margin": 0,
|
|
299
|
-
"outline": "none",
|
|
300
|
-
"paddingBottom": 0,
|
|
301
|
-
"paddingLeft": 16,
|
|
302
|
-
"paddingRight": 16,
|
|
303
|
-
"paddingTop": 0,
|
|
304
|
-
"position": "relative",
|
|
305
|
-
"textDecoration": "none",
|
|
306
|
-
"touchAction": "manipulation",
|
|
307
|
-
"userSelect": "none",
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
tabIndex={0}
|
|
311
|
-
type="button"
|
|
312
|
-
>
|
|
313
|
-
<span
|
|
314
|
-
className=""
|
|
315
|
-
style={
|
|
316
|
-
Object {
|
|
317
|
-
"MozOsxFontSmoothing": "grayscale",
|
|
318
|
-
"WebkitFontSmoothing": "antialiased",
|
|
319
|
-
"alignItems": "center",
|
|
320
|
-
"display": "inline-block",
|
|
321
|
-
"fontFamily": "Lato, \\"Noto Sans\\", sans-serif",
|
|
322
|
-
"fontSize": 16,
|
|
323
|
-
"fontWeight": "bold",
|
|
324
|
-
"lineHeight": "20px",
|
|
325
|
-
"overflow": "hidden",
|
|
326
|
-
"pointerEvents": "none",
|
|
327
|
-
"textOverflow": "ellipsis",
|
|
328
|
-
"whiteSpace": "nowrap",
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
>
|
|
332
|
-
Enable Server-side Mode
|
|
333
|
-
</span>
|
|
334
|
-
</button>
|
|
335
|
-
Sorry, no snapshot for you
|
|
336
|
-
</div>
|
|
337
|
-
`;
|