Repository navigation
Autosave: fix conflict between remote and local autosaves #17501
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0dc8e87
Add failing E2E tests
mcsf 6ff2ccd
Fix Promise.all idiom, make it actually parallel
mcsf 6e4c3c1
Don't parallelise, improve reliability
mcsf 7953a55
WIP: add e2e for clearing local autosave
mcsf 18273e1
Testing: Utils: Add toggleOfflineMode
mcsf 2fdfc1a
Editor store: Controls: Add local autosave handlers
mcsf 4c8b4e8
LocalAutosaveMonitor: Purge local only on successful remote autosave
mcsf 7ed5225
WIP: Avoid server/local autosave conflicts
mcsf 072a828
Defer to remote autosaves. Address feedback.
mcsf 1b4e8ab
Rename to __experimentalLocalAutosave
mcsf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| let _isOfflineMode = false; | ||
|
|
||
| export async function toggleOfflineMode( isOffline ) { | ||
| _isOfflineMode = isOffline; | ||
| page.setOfflineMode( isOffline ); | ||
| } | ||
|
|
||
| export function isOfflineMode() { | ||
| return _isOfflineMode; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,10 @@ import { parse } from '@wordpress/blocks'; | |
| * Internal dependencies | ||
| */ | ||
| import AutosaveMonitor from '../autosave-monitor'; | ||
| import { | ||
| localAutosaveGet, | ||
| localAutosaveClear, | ||
| } from '../../store/controls'; | ||
|
|
||
| const requestIdleCallback = window.requestIdleCallback ? window.requestIdleCallback : window.requestAnimationFrame; | ||
|
|
||
|
|
@@ -37,49 +41,6 @@ const hasSessionStorageSupport = once( () => { | |
| } | ||
| } ); | ||
|
|
||
| /** | ||
| * Function returning a sessionStorage key to set or retrieve a given post's | ||
| * automatic session backup. | ||
| * | ||
| * Keys are crucially prefixed with 'wp-autosave-' so that wp-login.php's | ||
| * `loggedout` handler can clear sessionStorage of any user-private content. | ||
| * | ||
| * @see https://github.com/WordPress/wordpress-develop/blob/6dad32d2aed47e6c0cf2aee8410645f6d7aba6bd/src/wp-login.php#L103 | ||
| * | ||
| * @param {string} postId Post ID. | ||
| * @return {string} sessionStorage key | ||
| */ | ||
| function postKey( postId ) { | ||
| return `wp-autosave-block-editor-post-${ postId }`; | ||
| } | ||
|
|
||
| /** | ||
| * Custom hook which returns a callback function to be invoked when a local | ||
| * autosave should occur. | ||
| * | ||
| * @return {Function} Callback function. | ||
| */ | ||
| function useAutosaveCallback() { | ||
| const { | ||
| postId, | ||
| getEditedPostAttribute, | ||
| } = useSelect( ( select ) => ( { | ||
| postId: select( 'core/editor' ).getCurrentPostId(), | ||
| getEditedPostAttribute: select( 'core/editor' ).getEditedPostAttribute, | ||
| } ) ); | ||
|
|
||
| return useCallback( () => { | ||
| const saveToSessionStorage = () => { | ||
| window.sessionStorage.setItem( postKey( postId ), JSON.stringify( { | ||
| post_title: getEditedPostAttribute( 'title' ), | ||
| content: getEditedPostAttribute( 'content' ), | ||
| excerpt: getEditedPostAttribute( 'excerpt' ), | ||
| } ) ); | ||
| }; | ||
| requestIdleCallback( saveToSessionStorage ); | ||
| }, [ postId ] ); | ||
| } | ||
|
|
||
| /** | ||
| * Custom hook which manages the creation of a notice prompting the user to | ||
| * restore a local autosave, if one exists. | ||
|
|
@@ -88,45 +49,62 @@ function useAutosaveNotice() { | |
| const { | ||
| postId, | ||
| getEditedPostAttribute, | ||
| } = useSelect( ( select ) => ( { | ||
| postId: select( 'core/editor' ).getCurrentPostId(), | ||
| getEditedPostAttribute: select( 'core/editor' ).getEditedPostAttribute, | ||
| } ) ); | ||
| remoteAutosave, | ||
| hasFetchedAutosave, | ||
| } = useSelect( ( select ) => { | ||
| const _postId = select( 'core/editor' ).getCurrentPostId(); | ||
|
youknowriad marked this conversation as resolved.
Resolved
|
||
| const postType = select( 'core/editor' ).getCurrentPostType(); | ||
| const user = select( 'core' ).getCurrentUser(); | ||
| return { | ||
| postId: _postId, | ||
| getEditedPostAttribute: select( 'core/editor' ).getEditedPostAttribute, | ||
| remoteAutosave: select( 'core' ).getAutosave( postType, _postId, user.id ), | ||
| hasFetchedAutosave: select( 'core' ).hasFetchedAutosaves( postType, _postId ) && user.id, | ||
| }; | ||
| } ); | ||
|
|
||
| const { createWarningNotice, removeNotice } = useDispatch( 'core/notices' ); | ||
| const { editPost, resetEditorBlocks } = useDispatch( 'core/editor' ); | ||
|
|
||
| useEffect( () => { | ||
| let autosave = window.sessionStorage.getItem( postKey( postId ) ); | ||
| if ( ! autosave ) { | ||
| if ( ! hasFetchedAutosave ) { | ||
| return; | ||
| } | ||
|
|
||
| let localAutosave = localAutosaveGet( postId ); | ||
| if ( ! localAutosave ) { | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| autosave = JSON.parse( autosave ); | ||
| localAutosave = JSON.parse( localAutosave ); | ||
| } catch ( error ) { | ||
| // Not usable if it can't be parsed. | ||
| return; | ||
| } | ||
|
|
||
| const { post_title: title, content, excerpt } = autosave; | ||
| const { post_title: title, content, excerpt } = localAutosave; | ||
| const edits = { title, content, excerpt }; | ||
|
|
||
| // Only display a notice if there is a difference between what has been | ||
| // saved and that which is stored in sessionStorage. | ||
| const hasDifference = Object.keys( edits ).some( ( key ) => { | ||
| return edits[ key ] !== getEditedPostAttribute( key ); | ||
| } ); | ||
|
|
||
| if ( ! hasDifference ) { | ||
| // If there is no difference, it can be safely ejected from storage. | ||
| window.sessionStorage.removeItem( postKey( postId ) ); | ||
| { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this needed? a way to create a scope?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it was for scope, and to share the WIP quickly |
||
| // Only display a notice if there is a difference between what has been | ||
| // saved and that which is stored in sessionStorage. | ||
| const hasDifference = Object.keys( edits ).some( ( key ) => { | ||
| return edits[ key ] !== getEditedPostAttribute( key ); | ||
| } ); | ||
|
|
||
| if ( ! hasDifference ) { | ||
| // If there is no difference, it can be safely ejected from storage. | ||
| localAutosaveClear( postId ); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| if ( remoteAutosave ) { | ||
| return; | ||
| } | ||
|
|
||
| const noticeId = uniqueId( 'wpEditorAutosaveRestore' ); | ||
|
|
||
| createWarningNotice( __( 'The backup of this post in your browser is different from the version below.' ), { | ||
| id: noticeId, | ||
| actions: [ | ||
|
|
@@ -140,7 +118,7 @@ function useAutosaveNotice() { | |
| }, | ||
| ], | ||
| } ); | ||
| }, [ postId ] ); | ||
| }, [ postId, hasFetchedAutosave ] ); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -150,24 +128,34 @@ function useAutosavePurge() { | |
| const { | ||
| postId, | ||
| isDirty, | ||
| isAutosaving, | ||
| didError, | ||
| } = useSelect( ( select ) => ( { | ||
| postId: select( 'core/editor' ).getCurrentPostId(), | ||
| postType: select( 'core/editor' ).getCurrentPostType(), | ||
| isDirty: select( 'core/editor' ).isEditedPostDirty(), | ||
| isAutosaving: select( 'core/editor' ).isAutosavingPost(), | ||
| didError: select( 'core/editor' ).didPostSaveRequestFail(), | ||
| } ) ); | ||
|
|
||
| const lastIsDirty = useRef( isDirty ); | ||
| const lastIsAutosaving = useRef( isAutosaving ); | ||
|
|
||
| useEffect( () => { | ||
| if ( lastIsDirty.current && ! isDirty ) { | ||
| window.sessionStorage.removeItem( postKey( postId ) ); | ||
| if ( lastIsAutosaving.current && ! isAutosaving && ! didError ) { | ||
| localAutosaveClear( postId ); | ||
| } | ||
|
|
||
| lastIsDirty.current = isDirty; | ||
| }, [ isDirty ] ); | ||
| lastIsAutosaving.current = isAutosaving; | ||
| }, [ isDirty, isAutosaving, didError ] ); | ||
| } | ||
|
|
||
| function LocalAutosaveMonitor() { | ||
| const autosave = useAutosaveCallback(); | ||
| const { __experimentalLocalAutosave } = useDispatch( 'core/editor' ); | ||
| const autosave = useCallback( () => { | ||
| requestIdleCallback( __experimentalLocalAutosave ); | ||
| }, [] ); | ||
| useAutosaveNotice(); | ||
| useAutosavePurge(); | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.