Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for #33616: Draft posts not previewable #1

Closed

Conversation

jorgecontreras
Copy link

@jorgecontreras jorgecontreras commented Nov 11, 2021

Description

  • Fixes #33616: Draft post not previewable if the draft was previously published.
  • During preview link creation, check if current post is in draft status.

How has this been tested?

  1. Open the edit page for a brand new post
  2. Add a title and body and hit preview in a new tab
  3. Notice the preview works great
  4. Publish the post
  5. Update the title and body and preview a couple times
  6. Notice preview still works great
  7. Switch the post to a draft
  8. Update the title and content and hit preview
  9. Confirmed that the title and body in the preview match the content from the Editor.
  10. Published the article again and confirmed that the preview still works great.

Screenshots

Types of changes

Bug fix

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • I've tested my changes with keyboard and screen readers.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.
  • I've updated all React Native files affected by any refactorings/renamings in this PR (please manually search all *.native.js files for terms that need renaming or removal).

Jorge Contreras added 2 commits November 11, 2021 13:26
- Fixes issue with draft post not previwable if the draft was previously published.
- During preview link creation, check if current post is in draft status.
- Add end to end test for issue WordPress#33616: Draft post not previewable if the draft was previously published.
Copy link

@getdave getdave left a comment

Choose a reason for hiding this comment

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

Thanks for the PR and nice work on providing an e2e test 👏 👏

From the PR description I'm not completely clear on how you've determined that the solution in this PR is the most appropriate.

I'm also not clear on what you believe causes the problem.

Some more context around how you've arrived at this solution would be much appreciated.

Thanks again for contributing!

🙇‍♂️

@@ -839,7 +839,7 @@ export function getEditedPostPreviewLink( state ) {
}

let previewLink = getAutosaveAttribute( state, 'preview_link' );
if ( ! previewLink ) {
if ( ! previewLink || 'draft' === getCurrentPost( state ).status ) {
Copy link

Choose a reason for hiding this comment

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

I'd like to understand more here about why "draft" status requires a unique case. What flow causes previewLink to be a falsy value? Is there no auto save? Does the autosave not get updated in certain circumstances? Is the REST API responding with the incorrect preview_link?

Copy link

Choose a reason for hiding this comment

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

I'd also consider adding a reference to the Issue for context.

Copy link
Author

Choose a reason for hiding this comment

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

hi @getdave ! Thanks for the feedback!

One thing I noticed about Draft posts is that they do not update the Autosave record in the Database.

Screen Shot 2021-12-10 at 2 40 45 PM

In the screenshot we can see the records for the post. The first row is the Autosave that was generated when the Post was published and previewed. The other 3 records are changes to the post that were made after switching to Draft mode. The previewLink seems to be generated based on the autosave record, so I thought adding this status check could be a good way to generate a valid previewLink.

Another observation is that after switching to Draft, and reloading the page. The autosave record gets deleted from the Database, and everything starts working fine again.

So based on these findings, I think other alternatives are:
a) Ensure that the autosave record gets updated also for Drafts, not only for Published.
b) Delete the autosave record asynchronously when switching to draft.
c) The getAutosaveAttribute is marked as deprecated since 5.6, so upgrading to the recommended getAutosave() would be worth exploring too.

As I am a new contributor, I don't have much understanding about the implications of any of these alternatives, or if we are talking about updates to the Wordpress core, so any advice is appreciated.

Thanks!

Copy link

Choose a reason for hiding this comment

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

hi @getdave ! Thanks for the feedback!

You're welcome!

The previewLink seems to be generated based on the autosave record

Can you validate this? What code path is used and where? I'd like to understand more about what actually generates the preview link?

...so I thought adding this status check could be a good way to generate a valid previewLink.

It does seem to but like you I'm also not 100% clear on this part of the system. When does the auto save record get created? Some possible avenues to explore

  • does an autosave get generated when working on a post that has not been published (i.e. has always been a draft)?
  • does an autosave get generated when working on a post that has been reverted from published to draft?
  • what code paths force the creation of a new autosave record?
  • is the autosave record "stale" when switching to draft?

Depending on the answers to the above, I wonder whether we are missing generation of an autosave record when switching from Publish to Draft status?

Ensure that the autosave record gets updated also for Drafts, not only for Published.

This is a good question. I'd need to understand more about the purpose of autosaves and their expected behaviour. Initially it would seem logical that autosaves would be generated for draft posts. After all if you've changed the post to "draft" status then you still want you changes to be autosaved right?

Delete the autosave record asynchronously when switching to draft.

I think the above above with updating the record sounds better.

The getAutosaveAttribute is marked as deprecated since 5.6, so upgrading to the recommended getAutosave() would be worth exploring too.

Worth a look. It would be nice to upgrade the code path to use non-deprecated methods. I would probably do this in a separate PR.

As I am a new contributor, I don't have much understanding about the implications of any of these alternatives, or if we are talking about updates to the Wordpress core, so any advice is appreciated.

I also don't have much context on autosaves so we'll have to dig into this together. I'll see if I can get some more advice on this as well.

Thanks for all your work here 🙇‍♂️

Copy link
Author

Choose a reason for hiding this comment

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

hi @getdave,

Answering your questions:

does an autosave get generated when working on a post that has not been published (i.e. has always been a draft)?
No, autosave records get generated only for published posts.

does an autosave get generated when working on a post that has been reverted from published to draft?
No, an autosave record is not generated in this case. The autosave record that was created when the post was published remains in the Database. Subsequent updates to the draft post do not update the autosave record.

what code paths force the creation of a new autosave record?
New autosave record gets created only for published post, when clicking on the 'Preview in new tab' option from the dropdown. This is handled in the post-preview-button.js:

// Request an autosave. This happens asynchronously and causes the component
		// to update when finished.
		if ( this.props.isDraft ) {
			this.props.savePost( { isPreview: true } );
		} else {
			this.props.autosave( { isPreview: true } );
		}

is the autosave record "stale" when switching to draft?
Yes, the autosave record that was previously generated for the published post remains in the Database, but additional edits to the draft post do not get reflected into the autosave record. Any new edits to the draft are reflected into the original record (the parent post). Reloading the page deletes the stale record.

Thanks,

  • Jorge.

Copy link
Author

Choose a reason for hiding this comment

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

I'd like to understand more here about why "draft" status requires a unique case. What flow causes previewLink to be a falsy value? Is there no auto save? Does the autosave not get updated in certain circumstances? Is the REST API responding with the incorrect preview_link?

What I'm observing is that there is no autosave for draft posts. The reason that the preview is not up to date with most recent edits is because there is a stale autosave record if the post switches from published to draft.

Copy link

@getdave getdave Jan 10, 2022

Choose a reason for hiding this comment

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

Ok so is this correct?:

  • the autosave exists because the post was first published and auto saves are only created for published posts.
  • the post is switched to Draft which causes autosaves not to be updated on further post content "change"
  • when requesting a preview, because the auto-save exists the code uses the preview link from that (stale) auto save
  • the rendered preview is thus out of date with the current post content

So the solution is if the post is a draft don't use the autosave preview link from the autosave.

If i'm correct then let's add a code comment explaining why we're checking the Draft status. That comment should also contain a link to this PR.

Copy link
Author

Choose a reason for hiding this comment

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

Exactly.

I have added a brief comment and a link to the PR and to the issue.

packages/e2e-tests/specs/editor/various/preview.test.js Outdated Show resolved Hide resolved
packages/e2e-tests/specs/editor/various/preview.test.js Outdated Show resolved Hide resolved
Jorge added 2 commits December 12, 2021 16:04
- Include a link to the issue that the test validates.
- Use aria-label instead of css class as a selector for Title field.
- Update test title to be more descriptive about the case under test.
@getdave
Copy link

getdave commented Jan 11, 2022

This is now looking good. Thank you for all the hard work!

I've requested a confidence check from @talldan just for a final 👍 then we should be good to merge.

@jorgecontreras
Copy link
Author

This is now looking good. Thank you for all the hard work!

I've requested a confidence check from @talldan just for a final 👍 then we should be good to merge.

Thank you @getdave for your support and feedback!

@talldan
Copy link

talldan commented Jan 12, 2022

This looks great. Thanks for working on the fix, and for adding a test. The explanation in the comment makes sense to me, nice work tracking down this flaw and fixing it.

Is the PR in the right repo? I think the PR should be created in the main WordPress/gutenberg repository. This is just a fork of that repo.

If you could go ahead and create a PR there and drop a link to it, then Dave or I will be happy to approve. The comment in the code will have to be updated to reflect the URL of the new PR. I would also suggest summarising the explanation from the comments in the description of the new PR for anyone else that may have questions about it.

@getdave
Copy link

getdave commented Jan 12, 2022

Re-running the failing tests as I think the failures are erroneous.

@talldan I think @jorgecontreras is following guidance from https://developer.wordpress.org/block-editor/contributors/code/git-workflow/#overview.

Fork the Gutenberg repository.
Clone the forked repository.
Create a new branch.
Make code changes.
Confirm tests pass.
Commit the code changes within the newly created branch.
Push the branch to the forked repository.
Submit a pull request to the Gutenberg repository.

Will that not work here?

@getdave
Copy link

getdave commented Jan 13, 2022

@jorgecontreras Looks like it's as Dan said that the PR needs to be raised in the main Gutenberg repo and it should be your branch from this forked repo.

Screen Shot 2022-01-13 at 10 50 51

@jorgecontreras
Copy link
Author

@jorgecontreras Looks like it's as Dan said that the PR needs to be raised in the main Gutenberg repo and it should be your branch from this forked repo.

Screen Shot 2022-01-13 at 10 50 51

Got it. I have created the PR on the main wordpress/gutenberg repository:
WordPress#37952

Please let me know if this looks good to you.

Thanks @talldan and @getdave!

@talldan
Copy link

talldan commented Jan 14, 2022

Merged in WordPress#37952

@talldan talldan closed this Jan 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Draft posts are not previewable if the draft was previously published
3 participants