-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Improve performance of the gallery block, by removing unnecessary requests. #6325
Conversation
1f3f654
to
1ba505a
Compare
@jorgefilipecosta – Initially, I really liked the approach you were taking with the changes to the gallery frame. Would you mind either opening a new PR with those changes or adding them back here? |
Hi @joemcgill, thank you for having a look at this PR, your PR #2488 was more ambitious, as it changed the frame of the gallery to the edit mode which I think we should do. |
componentWillReceiveProps( { isSelected, image, url } ) { | ||
if ( image && ! url ) { | ||
componentWillReceiveProps( { isSelected, image } ) { | ||
if ( image ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Won't this cause issues on subsequent re-renders of the component where image
is assigned, but url
is as well? In other words, do we risk an infinite loop of setAttributes
-> componentWillReceiveProps
-> setAttributes
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @aduth, I don't think so, because of this condition in the with select image: id && ! url ? getMedia( id ) : null,
, it guarantees that as soon as we have an URL the image will become null so the setAttributes function will not be called again.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So what is the image
prop representing as proposed here? It's very unclear to me. Would it be accurate to say that it's "a nullable value only set when the ID is known, the URL is not known, and a REST API request is in progress, then proceeding to be unset immediately once the response is received and the URL becomes known"? If so, how can we... make that more clear.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @aduth, thank you for the review, you are right, it is not easy to understand this logic. I did some changes in order to try to make the logic easier.
f30a564
to
48c8d25
Compare
I've pushed a suggestion improving the comments, @jorgefilipecosta. I have one issue with this, though, due to the component's eagerness to satisfy
This is because attributes keep getting re-set once we undo. The following patch helps visualize: diff --git a/core-blocks/gallery/gallery-image.js b/core-blocks/gallery/gallery-image.js
index f38d2ad23..854925896 100644
--- a/core-blocks/gallery/gallery-image.js
+++ b/core-blocks/gallery/gallery-image.js
@@ -80,6 +80,10 @@ class GalleryImage extends Component {
this.props.setAttributes( attributesToSet );
}
+ if ( ! attributesToSet && this.props.attributesToSet ) {
+ console.log( 'dropped attributesToSet', this.props.attributesToSet );
+ }
+
// unselect the caption so when the user selects other image and comeback
// the caption is not immediately selected
if ( this.state.captionSelected && ! isSelected && this.props.isSelected ) { |
f0bebb4
to
0fa30de
Compare
0fa30de
to
d646d1b
Compare
Hi @mcsf,
Unfortunately, it looks like this was an already existing problem without an easy fix. To solve it I refactored the code to handle the requests directly in the transform similar to what we do with the image upload. |
d646d1b
to
0dd9e53
Compare
0dd9e53
to
dc12353
Compare
I'm concerned that we're making substantial concessions and making the fix more complex than it should when maybe the root problem lies elsewhere. Right nowTo the point: in order to avoid getting stuck in an "undo loop" (described here), we are manually setting up a subscription to all data changes until all our images are loaded, then committing all of their newfound data via a single Instead, what if we look at this from the angle of content history and Undo? Why we should look elsewherePrior to these changes, each indivual
Counter-proposalThis is but a lengthy way to conclude that It's a potentially dumb idea that would also require an interface change — at its simplest, it could mean a hidden option in gutenberg/editor/store/reducer.js Lines 151 to 170 in a5d6a53
With this in mind, there's two ways forward: a pretty naïve one and a more robust one:
This all requires messing with the |
@jorgefilipecosta do you have a decision on @mcsf 's suggestions above? Is this PR worth keeping or should we close it? |
@catehstn: This is still basically blocked by #8119, which has been explored in a couple of ways but so far never with a satisfying solution. Also see #11504 (comment). I'd like to explore another solution and am trying to slot in some dev time for that. |
Thank you for checking this PR @catehstn. @mcsf summarized the status well. Although this problem is not the same as the undo problems, the way the undo problems are solved will affect the changes required here so it is blocked until the other issue is solved. I added the Status Blocked tag (I should have done it sooner). |
The original issue #4032 was addressed in an alternative way so this PR is not relevant anymore. |
Description
This PR adds a check in GalleryImage block to verify if we already have the URL if yes the media is not requested. Until now the media was requested anyway even if the value was not used. So we were requesting each image of the gallery without a need.
This PR had improvements to the gallery frame because I did not notice PR #2488. I prefer the changes in that PR so I remove frame changes from this PR and I will try to rebase the existing PR.
Fixes: #4032
How has this been tested?
Add a gallery to post, save the post, reload the post verify the images are not requested.
See the conversion from shortcodes still works, for example by pasting
[gallery ids="10868,10875,10872"]
.