Skip to content

Commit

Permalink
Add optional chain to sizes indexing of media details in edit-site (W…
Browse files Browse the repository at this point in the history
…ordPress#68995)

This optional chaining is already present in 21 other lines in Gutenberg where `sizes` (in `media_details`) is indexed, but is missing on these two lines.

For some reason, `media_details` is an empty object for some of my attachments on my site, so this throws a TypeError. This change resolves this issue.

Co-authored-by: grgar <grgarside@git.wordpress.org>
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
  • Loading branch information
3 people authored Feb 2, 2025
1 parent 24fad40 commit c9b2569
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/edit-site/src/components/media/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import { useEntityRecord } from '@wordpress/core-data';
function Media( { id, size = [ 'large', 'medium', 'thumbnail' ], ...props } ) {
const { record: media } = useEntityRecord( 'root', 'media', id );
const currentSize = size.find(
( s ) => !! media?.media_details?.sizes[ s ]
( s ) => !! media?.media_details?.sizes?.[ s ]
);

const mediaUrl =
media?.media_details?.sizes[ currentSize ]?.source_url ||
media?.media_details?.sizes?.[ currentSize ]?.source_url ||
media?.source_url;

if ( ! mediaUrl ) {
Expand Down

0 comments on commit c9b2569

Please sign in to comment.