@@ -141,11 +141,16 @@ function maybeUpdateUiObjects<T>(
141
141
shouldBackgroundFetch ?: boolean ;
142
142
identifier : StableDocumentIdentifier | null ;
143
143
} ,
144
- document : ResourceDataDocument | ResourceErrorDocument ,
144
+ document : ResourceDataDocument | ResourceErrorDocument | null ,
145
145
isFromCache : boolean
146
146
) : T {
147
147
const { identifier } = options ;
148
148
149
+ if ( ! document ) {
150
+ assert ( `The CacheHandler expected response content but none was found` , ! options . shouldHydrate ) ;
151
+ return document as T ;
152
+ }
153
+
149
154
if ( isErrorDocument ( document ) ) {
150
155
if ( ! identifier && ! options . shouldHydrate ) {
151
156
return document as T ;
@@ -294,8 +299,13 @@ function fetchContentAndHydrate<T>(
294
299
isMut = true ;
295
300
// TODO should we handle multiple records in request.records by iteratively calling willCommit for each
296
301
const record = context . request . data ?. record || context . request . records ?. [ 0 ] ;
297
- assert ( `Expected to receive a list of records included in the ${ context . request . op } request` , record ) ;
298
- store . cache . willCommit ( record , context ) ;
302
+ assert (
303
+ `Expected to receive a list of records included in the ${ context . request . op } request` ,
304
+ record || ! shouldHydrate
305
+ ) ;
306
+ if ( record ) {
307
+ store . cache . willCommit ( record , context ) ;
308
+ }
299
309
}
300
310
301
311
if ( store . lifetimes ?. willRequest ) {
@@ -310,7 +320,15 @@ function fetchContentAndHydrate<T>(
310
320
store . _join ( ( ) => {
311
321
if ( isMutation ( context . request ) ) {
312
322
const record = context . request . data ?. record || context . request . records ?. [ 0 ] ;
313
- response = store . cache . didCommit ( record , document ) as ResourceDataDocument ;
323
+ if ( record ) {
324
+ response = store . cache . didCommit ( record , document ) as ResourceDataDocument ;
325
+
326
+ // a mutation combined with a 204 has no cache impact when no known records were involved
327
+ // a createRecord with a 201 with an empty response and no known records should similarly
328
+ // have no cache impact
329
+ } else if ( isCacheAffecting ( document ) ) {
330
+ response = store . cache . put ( document ) as ResourceDataDocument ;
331
+ }
314
332
} else {
315
333
response = store . cache . put ( document ) as ResourceDataDocument ;
316
334
}
@@ -517,3 +535,18 @@ function copyDocumentProperties(target: { links?: unknown; meta?: unknown; error
517
535
target . errors = source . errors ;
518
536
}
519
537
}
538
+
539
+ function isCacheAffecting < T > ( document : StructuredDataDocument < T > ) : boolean {
540
+ if ( ! isMutation ( document . request ) ) {
541
+ return true ;
542
+ }
543
+ // a mutation combined with a 204 has no cache impact when no known records were involved
544
+ // a createRecord with a 201 with an empty response and no known records should similarly
545
+ // have no cache impact
546
+
547
+ if ( document . request . op === 'createRecord' && document . response ?. status === 201 ) {
548
+ return document . content ? Object . keys ( document . content ) . length > 0 : false ;
549
+ }
550
+
551
+ return document . response ?. status !== 204 ;
552
+ }
0 commit comments