Block Editor: Reusable blocks fetching causes "Unsaved Changes" prompt #14910
Description
Related: #14766
Describe the bug
Any action which triggers reusable blocks to become fetched will trigger the editor's mechanism to prompt the user when they leave the page.
This includes:
- Fetch as a result of autocompleters (selecting any paragraph block)
- Fetch as a result of toggling the Inserter
- Fetch as a result of a reusable block being present in the post's content
To reproduce
- Navigate to Posts > Add New
- Add a paragraph
- Save the post
- Reload the page
- Select the paragraph block
- Reload the page, or attempt to navigate away
- Observe the prompt
Or:
- Navigate to Posts > Add New
- Click the inserter
- Reload the page, or attempt to navigate away
- Observe the prompt
Or:
- Navigate to Posts > Add New
- Insert a reusable block
- Save the post
- Reload the page
- Reload the page, or attempt to navigate away
- Observe the prompt
Expected behavior
A prompt should not be shown.
Additional context
There is a fair amount of debugging information available at:
- Merely selecting a block after load creates undo level #14766 (comment)
- Merely selecting a block after load creates undo level #14766 (comment)
- Merely selecting a block after load creates undo level #14766 (comment)
Specifically, the comment at #14766 (comment) describe that the reason this occurs is that when the block editor receives reusable blocks as a result of the fetch, it considers this a change in its own state, which surfaces up to the editor and is accounted for in considering to show the prompt upon leaving the page.
It is not easy to solve because:
RECEIVE_BLOCKS
may in-fact include dirtying changes, so it cannot be skipped for consideration in whether the BlockEditorProvider call theonInput
oronChange
props.- The block editor module must receive blocks data for the reusable blocks, otherwise it cannot be shown in the editor (the implementation of the reusable block uses
select( 'core/block-editor' ).getBlock
). - It can't be done as part of the normal "sync" operations between editor and block editor because technically these reusable blocks are not part of the blocks value (they are not necessarily in post content).
Ultimately this ties into the issues of #7119 and related explorations at #14367 and #14715 , where the block editor state is not responsible for holding data of the reusable blocks. These however are much larger tasks, and a more immediate fix should be explored.
I am currently considering a fix which would update the blocks autocompleter to trigger the reusable blocks fetch only when generating its own options. However, as noted above, this only accounts for one of three possible workflows to trigger reusable blocsk fetching.
cc @youknowriad in case you have thoughts.
Activity