WordPress / gutenberg

The Block Editor project for WordPress and beyond. Plugin is available from the official repository.
https://wordpress.org/gutenberg/
Other
10.29k stars 4.11k forks source link

Previewing titles doesn't work properly #21191

Closed TylerBarnes closed 6 months ago

TylerBarnes commented 4 years ago

Describe the bug When previewing with a new title, and then immediately after with the original title, the preview title doesn't update back to the original title. This bug isn't present when using the "Classic Editor" plugin.

To reproduce

  1. Open Gutenberg on the "Sample Page"
  2. Change the title to "Sample Page 2" without updating the page
  3. Click preview
  4. Change the title back to "Sample Page" without updating the page
  5. Click preview
  6. Observe that the preview still says "Sample Page 2" but should say "Sample Page"

Expected behavior Every time "Preview" is clicked, it should show the correctly revisioned title.

Desktop:

Additional context

talldan commented 4 years ago

The reproduction steps only work with a published page (if you've already deleted Sample Page).

paaljoachim commented 3 years ago

Testing.

WordPress 5.7.2. Gutenberg plugin 10.6.2. Twenty Twenty One.

Went to a published page. With the name: "A post - Reuse" Changed the title to "A page - Reuse" -> previewed. I saw the new title. "A page - Reuse" Went back and changed the title back to "A post - Reuse" -> previewed. I saw the old title. "A page - Reuse"

It seems that every second change I can also see the change on the frontend.

dsas commented 3 years ago

Wordpress (gutenberg) will auto-save the editor state before previewing and then presumably the preview will always show the most recent save. It adds an autosave using the autosave rest API /wp/v2/posts/$postid/autosave

Following the repro steps @paaljoachim gives on trunk (d827fc5b9), the first preview generates an API request with a body of {"status":"publish","title":"A page - Reuse","content":"","excerpt":""} and the second preview generates a completely identical API request i.e. it's sending the same title both times, despite being changed.

Using react dev tools, AutosaveMonitor isDirty prop is set to false if the content matches the saved version and true otherwise, I assume this is related.

dsas commented 3 years ago

When saving in saveEntityRecord (core-data's actions.js) let data = { ...persistedRecord, ...autosavePost, ...record } merges three records, persistedRecord will have the initially saved title, autosavePost will have the previously saved one, and record will have the latest title, unless it matches the initially saved title. This means the bug needs to be fixed earlier than saving, presumably as one of the events that fire while changing the title.

cpapazoglou commented 3 years ago

Paraphrasing @dsas:

This problem reproduces only when you set the Title back to the original saved one. In that case, autosave doesn't pick up the change possibly because there is no actual change to the title and therefore it sends along the last autosaved one.