Skip to content

Commit

Permalink
Add optional chain to sizes indexing of media details in edit-site
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
grgar committed Feb 1, 2025
1 parent 24fad40 commit 9daaa82
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 9daaa82

Please sign in to comment.