Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 18 additions & 33 deletions packages/core-data/src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -677,40 +677,25 @@ export const saveEntityRecord =
? select.getRawEntityRecord( kind, name, recordId )
: {};

// Most of this autosave logic is very specific to posts.
// This is fine for now as it is the only supported autosave,
// but ideally this should all be handled in the back end,
// so the client just sends and receives objects.
if ( isAutosave ) {
// Most of this autosave logic is very specific to posts.
// This is fine for now as it is the only supported autosave,
// but ideally this should all be handled in the back end,
// so the client just sends and receives objects.
const currentUser = select.getCurrentUser();
const currentUserId = currentUser
? currentUser.id
: undefined;
const autosavePost = await resolveSelect.getAutosave(
persistedRecord.type,
persistedRecord.id,
currentUserId
);
// Autosaves need all expected fields to be present.
// So we fallback to the previous autosave and then
// to the actual persisted entity if the edits don't
// have a value.
let data = {
...persistedRecord,
...autosavePost,
...record,
};
data = Object.keys( data ).reduce(
// Build the autosave payload from the persisted
// record and the incoming edits. The previous autosave
// is intentionally excluded to avoid stale values
// overriding reverted fields.
const merged = { ...persistedRecord, ...record };
const data = [
'title',
'excerpt',
'content',
'meta',
].reduce(
( acc, key ) => {
if (
[
'title',
'excerpt',
'content',
'meta',
].includes( key )
) {
acc[ key ] = data[ key ];
if ( key in merged ) {
acc[ key ] = merged[ key ];
}
return acc;
},
Expand All @@ -720,7 +705,7 @@ export const saveEntityRecord =
// because it can lead to unexpected results. An example would be to
// have a draft post and change the status to publish.
status:
data.status === 'auto-draft'
merged.status === 'auto-draft'
? 'draft'
: undefined,
}
Expand Down
42 changes: 42 additions & 0 deletions test/e2e/specs/editor/various/preview.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,48 @@ test.describe( 'Preview', () => {
await previewPage.close();
} );

// See: https://github.com/WordPress/gutenberg/issues/33758.
test( 'should not use stale autosave data after reverting title', async ( {
editor,
page,
previewUtils,
} ) => {
const editorPage = page;

// Create and publish a post.
await editor.canvas
.locator( 'role=textbox[name="Add title"i]' )
.fill( 'Sample Page' );
await editor.insertBlock( {
name: 'core/paragraph',
attributes: { content: 'original content' },
} );
await editor.publishPost();

// Close the panel.
await page.click( 'role=button[name="Close panel"i]' );

// Change the title and preview to trigger an autosave.
await editor.canvas
.locator( 'role=textbox[name="Add title"i]' )
.fill( 'Sample Page 2' );
const previewPage = await editor.openPreviewPage( editorPage );
const previewTitle = previewPage.locator( 'role=heading[level=1]' );
await expect( previewTitle ).toHaveText( 'Sample Page 2' );

// Return to editor, revert the title, and preview again.
await editorPage.bringToFront();
await editor.canvas
.locator( 'role=textbox[name="Add title"i]' )
.fill( 'Sample Page' );

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After I fill "Sample Page" here, the editor will detect that the title is the same as the persisted title and will remove the title field from the edits? Now only the recent autosave has the "Sample Page 2" title. And it gets mistakenly carried over to the next autosave.

Just checking my understanding 🙂

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's correct.

await previewUtils.waitForPreviewNavigation( previewPage );

// Preview should show the reverted title, not the stale autosave.
await expect( previewTitle ).toHaveText( 'Sample Page' );

await previewPage.close();
} );

// Verify correct preview. See: https://github.com/WordPress/gutenberg/issues/33616
test( 'should display the correct preview when switching between published and draft statuses', async ( {
editor,
Expand Down
Loading