useShortCodeTransform: Fetch media data in single request#40945
Merged
Conversation
Mamaduka
commented
May 9, 2022
| return select( coreStore ).getMediaItems( { | ||
| include: imageIds.join( ',' ), | ||
| per_page: -1, | ||
| orderby: 'include', |
Member
Author
There was a problem hiding this comment.
We need this to preserve the original image order from the shortcode.
Mamaduka
commented
May 9, 2022
| return select( coreStore ).getMediaItems( { | ||
| include: imageIds.join( ',' ), | ||
| per_page: -1, | ||
| orderby: 'include', |
Member
Author
There was a problem hiding this comment.
This way, data cache keys are the same, so we re-use data from the store instead of re-fetching it.
Mamaduka
commented
May 9, 2022
Comment on lines
+43
to
+53
| return newImageData.map( ( imageData ) => { | ||
| return { | ||
| id: imageData.id, | ||
| type: 'image', | ||
| url: imageData.source_url, | ||
| mime: imageData.mime_type, | ||
| alt: imageData.alt_text, | ||
| link: imageData.link, | ||
| caption: imageData?.caption?.raw, | ||
| }; | ||
| } ); |
Member
Author
There was a problem hiding this comment.
I moved this outside of the mapSelect function to avoid returning a new array reference and causing unnecessary re-renders.
|
Size Change: -12 B (0%) Total Size: 1.24 MB
ℹ️ View Unchanged
|
glendaviesnz
approved these changes
May 10, 2022
glendaviesnz
left a comment
Contributor
There was a problem hiding this comment.
Worked as advertised for me, and now just makes a single call to get all the data for every image in shortcode rather then a call for each image 🎉
Member
Author
|
Thanks for testing, @glendaviesnz. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What?
PR reduces the number of HTTP requests made by
useShortCodeTransformwhen fetching media data for transformation.Similar to #34389.
How?
Updates hook to use
getMediaItems.Testing Instructions
Screenshots or screencast