Skip to content

Commit eb196a2

Browse files
committed
fix(core): fix possible memory leak on SSR and validate JSON on non-raw request
1 parent ef03bde commit eb196a2

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

packages/react-isomorphic-data/src/common/Client.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export const createDataClient = (
1616
headers: headers || {},
1717
fetchPolicy: fetchPolicy || 'cache-first',
1818
toBePrefetched: {},
19+
__internal: {},
1920
addSubscriber: (key: string, callback: Function) => {
2021
if (!(subscribers[key] instanceof Map)) {
2122
subscribers[key] = new Map();

packages/react-isomorphic-data/src/common/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export interface DataClient {
1010
fetchPolicy: 'cache-first' | 'cache-and-network' | 'network-only';
1111
test: boolean;
1212
headers: Record<string, any>;
13+
__internal: Record<string, any>;
1314
addSubscriber: (key: string, callback: Function) => void;
1415
removeSubscriber: (key: string, callback: Function) => void;
1516
notifySubscribers: (key: string) => void;

packages/react-isomorphic-data/src/hooks/utils/useBaseData.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import { LazyDataState, DataHookOptions } from '../types';
66
import useFetchRequirements from './useFetchRequirements';
77
import useCacheSubscription, { LoadingSymbol } from './useCacheSubscription';
88

9-
const simpleCache: Record<string, any> = {};
10-
119
const useBaseData = <T, > (
1210
url: string,
1311
queryParams: Record<string, any> = {},
@@ -48,7 +46,6 @@ const useBaseData = <T, > (
4846
const delay = Date.now() - Number(client.initTime);
4947

5048
if (delay <= client.ssrForceFetchDelay) {
51-
console.log('set to cache-first')
5249
fetchPolicy = 'cache-first'; // force to 'cache-first' policy when using ssrForceFetchDelay
5350
}
5451
}
@@ -59,6 +56,9 @@ const useBaseData = <T, > (
5956
promiseRef.current = fetcher(fullUrl, finalFetchOpts)
6057
.then((result) => result.text())
6158
.then((data) => {
59+
if (!raw) {
60+
JSON.parse(data); // check if valid JSON or not, will throw error if it is not
61+
}
6262
// this block of code will cause 2 re-renders because React doesn't batch these 2 updates
6363
// https://twitter.com/dan_abramov/status/887963264335872000?lang=en
6464
// For React 16.x we can use `unstable_batchedUpdates()` to solve this
@@ -105,7 +105,7 @@ const useBaseData = <T, > (
105105
});
106106

107107
return promiseRef.current;
108-
}, [addToCache, finalFetchOpts, fullUrl, isSSR, useTempData, fetcher, setState]);
108+
}, [addToCache, finalFetchOpts, fullUrl, isSSR, useTempData, fetcher, setState, raw]);
109109

110110
const memoizedFetchData = React.useCallback((): Promise<any> => {
111111
const currentDataInCache = retrieveFromCache(fullUrl);
@@ -190,13 +190,17 @@ const useBaseData = <T, > (
190190
return usedData;
191191
} else {
192192
// else, we want the data in json form
193-
if (!simpleCache[usedData]) {
194-
simpleCache[usedData] = JSON.parse(usedData);
193+
if (!client.__internal[usedData]) {
194+
try {
195+
client.__internal[usedData] = JSON.parse(usedData);
196+
} catch (err) {
197+
client.__internal[usedData] = null;
198+
}
195199
}
196200

197-
return simpleCache[usedData];
201+
return client.__internal[usedData];
198202
}
199-
}, [usedData, raw]);
203+
}, [usedData, raw, client]);
200204

201205
return [
202206
memoizedFetchData,

0 commit comments

Comments
 (0)