@@ -55,19 +55,24 @@ export async function insertForUrl(url: string, data: mixed) {
5555 responseForCachePolicy ( resp ) ,
5656 )
5757
58- return cacheItem ( { key, response : resp , policy} )
58+ return cacheItem ( { key, response : resp , policy, bundled : true } )
5959}
6060
6161// Does the magic: stores a Request into AsyncStorage
62- type CacheItemArgs = { key : string , response : Response , policy : CachePolicy }
63- async function cacheItem ( { key, response, policy} : CacheItemArgs ) {
62+ type CacheItemArgs = { key : string , response : Response , policy : CachePolicy , bundled ?: boolean }
63+ async function cacheItem ( { key, response, policy, bundled } : CacheItemArgs ) {
6464 response = await serializeResponse ( response )
6565
66+ let strResponse = JSON . stringify ( response )
6667 await AsyncStorage . multiSet ( [
67- [ `${ ROOT } :${ key } :response` , JSON . stringify ( response ) ] ,
68+ [ `${ ROOT } :${ key } :response` , strResponse ] ,
6869 [ `${ ROOT } :${ key } :policy` , JSON . stringify ( policy . toObject ( ) ) ] ,
6970 [ `${ ROOT } :${ key } :ttl` , JSON . stringify ( policy . timeToLive ( ) ) ] ,
7071 ] )
72+
73+ if ( bundled ) {
74+ await AsyncStorage . setItem ( `${ ROOT } :${ key } :bundled` , strResponse )
75+ }
7176}
7277
7378// Does more magic: gets a Request from AsyncStorage
@@ -99,6 +104,15 @@ export async function cachedFetch(request: Request): Promise<Response> {
99104 let key = `urlcache:${ url } `
100105 let { response : oldResponse , policy : oldPolicy } = await getItem ( key )
101106
107+ if ( process . env . NODE_ENV === 'development' ) {
108+ let bundledResponse = await AsyncStorage . getItem ( `${ ROOT } :${ key } :bundled` )
109+ if ( bundledResponse ) {
110+ debug && console . log ( `fetch(${ request . url } ): in dev mode; returning bundled data` )
111+ let { body, ...init } = JSON . parse ( bundledResponse )
112+ return new Response ( body , init )
113+ }
114+ }
115+
102116 // If nothing has ever been cached, go fetch it
103117 if ( ! oldPolicy ) {
104118 debug && console . log ( `fetch(${ request . url } ): no policy cached; fetching` )
0 commit comments