Skip to content

Commit 379ca6c

Browse files
committedApr 4, 2024
add tests and fixes for empty 201/204
1 parent 9b3aa9c commit 379ca6c

File tree

3 files changed

+549
-8
lines changed

3 files changed

+549
-8
lines changed
 

‎packages/store/src/-private/cache-handler.ts

+37-4
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,16 @@ function maybeUpdateUiObjects<T>(
141141
shouldBackgroundFetch?: boolean;
142142
identifier: StableDocumentIdentifier | null;
143143
},
144-
document: ResourceDataDocument | ResourceErrorDocument,
144+
document: ResourceDataDocument | ResourceErrorDocument | null,
145145
isFromCache: boolean
146146
): T {
147147
const { identifier } = options;
148148

149+
if (!document) {
150+
assert(`The CacheHandler expected response content but none was found`, !options.shouldHydrate);
151+
return document as T;
152+
}
153+
149154
if (isErrorDocument(document)) {
150155
if (!identifier && !options.shouldHydrate) {
151156
return document as T;
@@ -294,8 +299,13 @@ function fetchContentAndHydrate<T>(
294299
isMut = true;
295300
// TODO should we handle multiple records in request.records by iteratively calling willCommit for each
296301
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+
}
299309
}
300310

301311
if (store.lifetimes?.willRequest) {
@@ -310,7 +320,15 @@ function fetchContentAndHydrate<T>(
310320
store._join(() => {
311321
if (isMutation(context.request)) {
312322
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+
}
314332
} else {
315333
response = store.cache.put(document) as ResourceDataDocument;
316334
}
@@ -517,3 +535,18 @@ function copyDocumentProperties(target: { links?: unknown; meta?: unknown; error
517535
target.errors = source.errors;
518536
}
519537
}
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

Comments
 (0)
Failed to load comments.