[Blocks] Add preload to fetch (#18785)
Dan Abramov committed
Apr 30, 2020 at 02:23 UTC
3a9c373521eae259acbfcf98f0f9a543a6934c10
2 files changed
+17
-3
fixtures/blocks/src/Post.js
+5
-1
@@ -7,10 +7,14 @@
7
8
import * as React from 'react';
9
import {block, Suspense} from 'react';
10
-import {fetch} from 'react-data/fetch';
10
+import {fetch, preload} from 'react-data/fetch';
11
import loadComments from './Comments';
12
13
function load(params) {
14
+ if (params.id < 3) {
15
+ const nextId = params.id + 1;
16
+ preload('http://localhost:3001/posts/' + nextId);
17
+ }
18
const postResponse = fetch('http://localhost:3001/posts/' + params.id);
19
return {
20
post: postResponse.json(),
packages/react-data/src/fetch/ReactDataFetch.js
+12
-2
@@ -119,7 +119,7 @@ Response.prototype = {
119
},
120
};
121
122
-export function fetch(url: string, options: mixed): Object {
122
+function preloadResult(url: string, options: mixed): Result {
123
const map = readResultMap();
124
let entry = map.get(url);
125
if (!entry) {
@@ -134,7 +134,17 @@ export function fetch(url: string, options: mixed): Object {
134
entry = toResult(thenable);
135
map.set(url, entry);
136
}
137
- const nativeResponse = (readResult(entry): any);
137
+ return entry;
138
+}
139
+
140
+export function preload(url: string, options: mixed): void {
141
+ preloadResult(url, options);
142
+ // Don't return anything.
143
+}
144
+
145
+export function fetch(url: string, options: mixed): Object {
146
+ const result = preloadResult(url, options);
147
+ const nativeResponse = (readResult(result): any);
148
if (nativeResponse._reactResponse) {
149
return nativeResponse._reactResponse;
150
} else {