Skip to content

Commit 5d06c68

Browse files
MamadukajsnajdrThelmachidocpapazoglou
authored
Core Data: Avoid stale values when in autosave payloads (#76337)
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org> Co-authored-by: jsnajdr <jsnajdr@git.wordpress.org> Co-authored-by: Thelmachido <thelmachido@git.wordpress.org> Co-authored-by: cpapazoglou <cpapazoglou@git.wordpress.org>
1 parent 9a5a2da commit 5d06c68

2 files changed

Lines changed: 60 additions & 33 deletions

File tree

packages/core-data/src/actions.js

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -678,40 +678,25 @@ export const saveEntityRecord =
678678
? select.getRawEntityRecord( kind, name, recordId )
679679
: {};
680680

681+
// Most of this autosave logic is very specific to posts.
682+
// This is fine for now as it is the only supported autosave,
683+
// but ideally this should all be handled in the back end,
684+
// so the client just sends and receives objects.
681685
if ( isAutosave ) {
682-
// Most of this autosave logic is very specific to posts.
683-
// This is fine for now as it is the only supported autosave,
684-
// but ideally this should all be handled in the back end,
685-
// so the client just sends and receives objects.
686-
const currentUser = select.getCurrentUser();
687-
const currentUserId = currentUser
688-
? currentUser.id
689-
: undefined;
690-
const autosavePost = await resolveSelect.getAutosave(
691-
persistedRecord.type,
692-
persistedRecord.id,
693-
currentUserId
694-
);
695-
// Autosaves need all expected fields to be present.
696-
// So we fallback to the previous autosave and then
697-
// to the actual persisted entity if the edits don't
698-
// have a value.
699-
let data = {
700-
...persistedRecord,
701-
...autosavePost,
702-
...record,
703-
};
704-
data = Object.keys( data ).reduce(
686+
// Build the autosave payload from the persisted
687+
// record and the incoming edits. The previous autosave
688+
// is intentionally excluded to avoid stale values
689+
// overriding reverted fields.
690+
const merged = { ...persistedRecord, ...record };
691+
const data = [
692+
'title',
693+
'excerpt',
694+
'content',
695+
'meta',
696+
].reduce(
705697
( acc, key ) => {
706-
if (
707-
[
708-
'title',
709-
'excerpt',
710-
'content',
711-
'meta',
712-
].includes( key )
713-
) {
714-
acc[ key ] = data[ key ];
698+
if ( key in merged ) {
699+
acc[ key ] = merged[ key ];
715700
}
716701
return acc;
717702
},
@@ -721,7 +706,7 @@ export const saveEntityRecord =
721706
// because it can lead to unexpected results. An example would be to
722707
// have a draft post and change the status to publish.
723708
status:
724-
data.status === 'auto-draft'
709+
merged.status === 'auto-draft'
725710
? 'draft'
726711
: undefined,
727712
}

test/e2e/specs/editor/various/preview.spec.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,48 @@ test.describe( 'Preview', () => {
144144
await previewPage.close();
145145
} );
146146

147+
// See: https://github.com/WordPress/gutenberg/issues/33758.
148+
test( 'should not use stale autosave data after reverting title', async ( {
149+
editor,
150+
page,
151+
previewUtils,
152+
} ) => {
153+
const editorPage = page;
154+
155+
// Create and publish a post.
156+
await editor.canvas
157+
.locator( 'role=textbox[name="Add title"i]' )
158+
.fill( 'Sample Page' );
159+
await editor.insertBlock( {
160+
name: 'core/paragraph',
161+
attributes: { content: 'original content' },
162+
} );
163+
await editor.publishPost();
164+
165+
// Close the panel.
166+
await page.click( 'role=button[name="Close panel"i]' );
167+
168+
// Change the title and preview to trigger an autosave.
169+
await editor.canvas
170+
.locator( 'role=textbox[name="Add title"i]' )
171+
.fill( 'Sample Page 2' );
172+
const previewPage = await editor.openPreviewPage( editorPage );
173+
const previewTitle = previewPage.locator( 'role=heading[level=1]' );
174+
await expect( previewTitle ).toHaveText( 'Sample Page 2' );
175+
176+
// Return to editor, revert the title, and preview again.
177+
await editorPage.bringToFront();
178+
await editor.canvas
179+
.locator( 'role=textbox[name="Add title"i]' )
180+
.fill( 'Sample Page' );
181+
await previewUtils.waitForPreviewNavigation( previewPage );
182+
183+
// Preview should show the reverted title, not the stale autosave.
184+
await expect( previewTitle ).toHaveText( 'Sample Page' );
185+
186+
await previewPage.close();
187+
} );
188+
147189
// Verify correct preview. See: https://github.com/WordPress/gutenberg/issues/33616
148190
test( 'should display the correct preview when switching between published and draft statuses', async ( {
149191
editor,

0 commit comments

Comments
 (0)