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

400 bad request trying to autosave empty content #6556

Closed
2 tasks
bobbingwide opened this issue May 3, 2018 · 13 comments · Fixed by #10404
Closed
2 tasks

400 bad request trying to autosave empty content #6556

bobbingwide opened this issue May 3, 2018 · 13 comments · Fixed by #10404
Labels
Backwards Compatibility Issues or PRs that impact backwards compatability [Feature] Saving Related to saving functionality REST API Interaction Related to REST API [Type] Bug An existing feature does not function as intended

Comments

@bobbingwide
Copy link
Contributor

Issue Overview

When adding a new post I got an unexpected "Updating failed" message.
The console log showed 400 bad request.
The response being

{"code":"empty_content","message":"Content, title, and excerpt are empty.","data":{"status":400}}

Steps to Reproduce (for bugs)

  1. Add a new post.
  2. Insert a Classic block
  3. Wait a while

Expected Behavior

Either no attempt to save the post or allow the empty content to be saved.

Current Behavior

Autosave kicks in. Since none of the following have been entered... Title, Content and Excerpt
we get the 400 Bad request. which leads to the display of the unexpected "Updating failed" message.

Possible Solution

Screenshots / Video

Related Issues and/or PRs

#1201

Todos

  • Tests
  • Documentation
@danielbachhuber danielbachhuber added [Type] Bug An existing feature does not function as intended REST API Interaction Related to REST API labels May 17, 2018
@danielbachhuber danielbachhuber added this to the WordPress 5.0 milestone May 17, 2018
@danielbachhuber
Copy link
Member

Thanks for the report, @bobbingwide. I can reproduce this issue:

image

More specifically, I think this issue happens because an empty Classic block is sent to the server as '' (empty string), while every other block (even when empty) includes the block HTML comment data.

Building on #1203, I'd suggest we add some special case behavior to isEditedPostSaveable that treats empty Classic blocks as not saveable.

@bobbingwide
Copy link
Contributor Author

I can imagine scenarios where the content of the title, post and excerpt is automatically created by plugins from meta data.

I have one CPT in my Golf Society's website where the posts are generated with content of <!--more-->.
Another CPT just contain's <!--more-->[bw_fields].

e.g. https://www.anchorgolf.com/result/28-apr-hurtmore-winner-herb-miller/

The posts are created programatically. Perhaps empty content should be allowed.

@bobbingwide
Copy link
Contributor Author

bobbingwide commented May 17, 2018

I've just tested. The Classic editor allows you to save posts with empty titles and content.
image

So if we don't allow an empty post to be saved this would be a backwards compatibility / documentation issue.
In this example I can't save the post using Gutenberg that I'd previously saved using the Classic editor.

image

Should I raise a separate issue?

@danielbachhuber
Copy link
Member

I've just tested. The Classic editor allows you to save posts with empty titles and content.

I'm not quite sure I agree with this result. I'm able to "save" an empty post:

image

However, if I saved it with an empty title and content, it's not accessible in Edit Posts:

image

In the database, it's saved as an auto-draft:

image

So if we don't allow an empty post to be saved this would be a backwards compatibility / documentation issue.

I understand how this is a change in behavior that would need to be documented. Can you explain to me how it's a break in backwards compatibility?

@danielbachhuber danielbachhuber added the Backwards Compatibility Issues or PRs that impact backwards compatability label May 17, 2018
@bobbingwide
Copy link
Contributor Author

Can you explain to me how it’s a break in backwards compatibility?
Tricky. It depends on your understanding of the label. For me, if it’s something that I could do in the previous version but can’t do in the current version, then it’s a regression... a backward compatibility issue.

If this regression is then considered to be a bug then it needs to be worked on.
If it’s not a bug, then there should be a requirement and a proposed solution, and some tests that explain the scenarios for the change. Failing that some documentation.

In this case the documentation might cover how to publish an empty post using the block editor.
I’ll have to check the steps I performed to create my empty post.

@danielbachhuber
Copy link
Member

Thanks for clarifying, @bobbingwide. We'll get this sorted out one way or another.

@bobbingwide
Copy link
Contributor Author

bobbingwide commented May 19, 2018

To create an empty post, with a nice slug.

  1. Create post normally with Title and Content. Save
  2. Blank out Title and Content. Save

Or, for a numeric slug, omit the title.
Note: I’m having problems reproducing in different environments. Need to investigate further.
I believe that, in order to be able to create an empty post the post type should not support excerpt.
If it does, then the Classic editor reinstates the deleted content after the update, but Gutenberg doesn’t until the whole post is reloaded.

@danielbachhuber
Copy link
Member

danielbachhuber commented Jun 15, 2018

Coming back to this, I think there's one specific scenario to solve for: the user should never experience an "Updating failed" state like this:

image

However unlikely, it should be possible for a user to:

  1. Create a new post in Gutenberg.
  2. Switch the first block to a Classic Block.
  3. Hit "Save Draft" and have the post saved.

But, this isn't currently possible because of:

image

I'm not sure which technical solution we should apply yet.

@aduth
Copy link
Member

aduth commented Jul 5, 2018

Additional technical context / possible solution at #7727

@danielbachhuber
Copy link
Member

While @pento and I were chatting on Tuesday, we discovered some additional odd behavior of the Classic Editor:

  1. Create a new post in the Classic Editor.
  2. Add some text to the body and leave the title blank.
  3. Hit "Save" and see the post is saved successfully.
  4. Remove the text from the body.
  5. Hit "Save" again and see that the post isn't saved successfully — the text re-appears in the body.

saveempty

@danielbachhuber
Copy link
Member

For the purposes of replicating Classic Editor behavior, I'd be fine the solution proposed in #7727 — prevent any saving if the title is empty and the editor has an empty Classic Block.

@danielbachhuber
Copy link
Member

@aduth @youknowriad Do either of you have a preferred implementation for this?

@danielbachhuber
Copy link
Member

From @aduth in Slack:

/**
* Returns true if the edited post has content. A post has content if it has at
* least one block or otherwise has a non-empty content property assigned.
*
* @param {Object} state Global application state.
*
* @return {boolean} Whether post has content.
*/
export function isEditedPostEmpty( state ) {
return (
! getBlockCount( state ) &&
! getEditedPostAttribute( state, 'content' )
);
}

! getBlockCount( state ) is technically an optimization to avoid serializing.
Here, we need to check that if count is 1, and the 1 block is fallback, to allow the serializer to run to see if its the empty string result

@mtias mtias added this to the WordPress 5.0 milestone Oct 12, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backwards Compatibility Issues or PRs that impact backwards compatability [Feature] Saving Related to saving functionality REST API Interaction Related to REST API [Type] Bug An existing feature does not function as intended
Projects
None yet
4 participants