@@ -6,8 +6,6 @@ import { LazyDataState, DataHookOptions } from '../types';
66import useFetchRequirements from './useFetchRequirements' ;
77import useCacheSubscription , { LoadingSymbol } from './useCacheSubscription' ;
88
9- const simpleCache : Record < string , any > = { } ;
10-
119const 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