-
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
useShortCodeTransform: Fetch media data in single request #40945
Conversation
return select( coreStore ).getMediaItems( { | ||
include: imageIds.join( ',' ), | ||
per_page: -1, | ||
orderby: 'include', |
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.
We need this to preserve the original image order from the shortcode.
@@ -33,6 +33,7 @@ export default function useGetMedia( innerBlockImages ) { | |||
return select( coreStore ).getMediaItems( { | |||
include: imageIds.join( ',' ), | |||
per_page: -1, | |||
orderby: 'include', |
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.
This way, data cache keys are the same, so we re-use data from the store instead of re-fetching it.
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, | ||
}; | ||
} ); |
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.
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
|
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.
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 🎉
Thanks for testing, @glendaviesnz. |
What?
PR reduces the number of HTTP requests made by
useShortCodeTransform
when fetching media data for transformation.Similar to #34389.
How?
Updates hook to use
getMediaItems
.Testing Instructions
Screenshots or screencast