gutenberg icon indicating copy to clipboard operation
gutenberg copied to clipboard

Editor stuck in dirty mode due to received sanitized data

Open bvlgn opened this issue 7 years ago • 1 comments

Gutenberg 6.4 introduced the following bug: When (meta) data is saved along a post, which is sanitized server side, the returned updated data is not available/synced in the core/editor data store as it was pre-6.4.

Steps to reproduce the buggy behavior/output

  1. In WP 5.2.3 (with Gutenberg 6.4 or 6.5 active) register the test meta field: add_action( 'init', function() { register_post_meta( 'page', 'test_key', array( 'show_in_rest' => true, 'single' => true, 'type' => 'string', 'sanitize_callback' => 'sanitize_key', )); }); Please note the sanitize callback

  2. Add and publish a test page

  3. Open the console and enter: > wp.data.dispatch('core/editor').editPost({ meta: { test_key: 'TEST!!!' } })

  4. Press the [Update] button The post will be saved but please note that the button will stay active! ... so the document is still 'dirty'

  5. Paste in console: > wp.data.select('core/editor').getEditedPostAttribute('meta') The console will show: < {test_key: "TEST!!!"} So it still shows the old data If you have a look at the Network output you'll see that the back-end did return the expected sanitized data correctly though: network-response

Expected behavior/output If your repeat the same steps without Gutenberg active or with a pre-6.4 version then the console output will be as expected showing the sanitized data: < {test_key: "test"}

  • the [Update] button will be greyed-out after the post has been saved.
  • The document is no longer dirty.

Screenshots Console output (bugged) with Gutenberg 6.4 or 6.5: bug-console-content

Console output (correct) without Gutenberg or pre-6.4: no-bug-console-content

Notes: Please also note that the pre GB 6.4 test returns 'undefined' after the dispatch while the GB 6.5 test returns: 'Promise {: undefined}' Maybe that is an indication where the bug originates?

This bug leads to a very bad user experience in real world situations:

  • withSelect(TextControl) related to the meta won't be updated with the server returned sanitized value
  • the editor will remain in dirty state:
    • the Update button will stay active, no matter how often you click it.
    • when the user in the end moves away from the edited page they get a warning alert about lossing unsaved data.

I don't know of any work-around. Not sanitizing the data would be unsafe and also user unfriendly.

P.S. tested in real world situation and in simplified local test situation with blank WP 5.2.3 installation and WP 5.3beta1, with and without Gutenberg plugin active (tested with versions 6.3,6.4 and 6.5). No other plugins and Twenty Nineteen theme.

bvlgn avatar Sep 19 '19 23:09 bvlgn

Thanks Benny for opening that issue. we're working on it on this PR which will solve the issue #17532

youknowriad avatar Sep 24 '19 16:09 youknowriad