Describe the bug
Dispatching the setAvailableMetaBoxesPerLocation() action causes multiple AJAX requests to post.php to be made.
These are the requests that cause meta boxes to update—they happen when the post is published.
To Reproduce
js
wp.data.dispatch( 'core/edit-post' ).setAvailableMetaBoxesPerLocation( {"side":[],"normal":[{"id":"postcustom","title":"Custom Fields"}],"advanced":[]} );
post.phpExpected behavior
Only _one_ request to post.php should have been made.
Additional context
This happens because, in the SET_META_BOXES_PER_LOCATIONS effect, we subscribe() to the store without first removing any existing subscription:
Calling subscribe() returns an unsubscribe function, so fixing this should just be a matter of storing the return value of subscribe() and calling it next time.
Hi @noisysocks
Is there any update on this issue? We are having to implement some "hacky" jQuery to add in metaboxes and metabox "sortable" locations. This is not ideal, as React destroys the HTML during renders.
@elliotcondon: It needs a developer to look into it. Would you like to take a crack and open a PR? 🙂
Hi @noisysocks
I would love to, but find the code architecture of Gutenberg far to confusing. I wouldn't know where to start.
Calling
subscribe()returns anunsubscribefunction, so fixing this should just be a matter of storing the return value ofsubscribe()and calling it next time.
What is best for SET_META_BOXES_PER_LOCATIONS? To run the subscribed callback only once per effect? Or to just make sure that never are there more than two subscriptions at once? Depending on that, one would either:
SET_META_BOXES_PER_LOCATIONS( action, store ) {
const unsubscribe = subscribe( () => {
…
unsubscribe();
} );
}
or
let unsubscribe;
SET_META_BOXES_PER_LOCATIONS( action, store ) {
if ( unsubscribe ) {
unsubscribe();
}
unsubscribe = subscribe( () => {
…
} );
}
What is best for
SET_META_BOXES_PER_LOCATIONS? To run the subscribed callback only once per effect? Or to just make sure that never are there more than two subscriptions at once? Depending on that, one would either
On playing around with it, seems like it has to be the second option - if unsubscribing within the the subscription as in the first option the metabox save doesn't seem to run at all. I have a draft PR with the second approach up at https://github.com/WordPress/gutenberg/pull/17522
Thanks so much @glendaviesnz!
Most helpful comment
On playing around with it, seems like it has to be the second option - if unsubscribing within the the subscription as in the first option the metabox save doesn't seem to run at all. I have a draft PR with the second approach up at https://github.com/WordPress/gutenberg/pull/17522