Skip to content

Commit

Permalink
feat: cache rest entries
Browse files Browse the repository at this point in the history
  • Loading branch information
YvesRijckaert committed Apr 6, 2023
1 parent b08d9af commit 50f2b8e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
10 changes: 7 additions & 3 deletions src/live-updates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,20 @@ export class LiveUpdates {
locale,
contentType,
}: MergeEntityProps): Entity {
// TODO: https://contentful.atlassian.net/browse/TOL-1033
// TODO: https://contentful.atlassian.net/browse/TOL-1025
const entryId = (dataFromPreviewApp as any).sys.id;
const cachedData = this.storage.get(entryId) || dataFromPreviewApp;

const updatedData = rest.updateEntry(
contentType,
dataFromPreviewApp as any,
//@ts-expect-error -- ..
cachedData,
updateFromEntryEditor as any,
locale
);

// Cache the updated data for future updates
this.storage.set(entryId, updatedData);

return updatedData;
}

Expand Down
5 changes: 2 additions & 3 deletions src/rest/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,9 @@ export function updateEntry(
updateFromEntryEditor: EntryProps,
locale: string
): Entity & { sys: SysProps } {
const { fields } = contentType;
if (dataFromPreviewApp.sys.id === updateFromEntryEditor.sys.id) {
return mergeFields(fields, dataFromPreviewApp, updateFromEntryEditor, locale);
return mergeFields(contentType.fields, dataFromPreviewApp, updateFromEntryEditor, locale);
} else {
return updateNestedRef(fields, dataFromPreviewApp, updateFromEntryEditor, locale);
return updateNestedRef(contentType.fields, dataFromPreviewApp, updateFromEntryEditor, locale);
}
}

0 comments on commit 50f2b8e

Please sign in to comment.