Skip to content

Conversation

@ramonjd
Copy link
Member

@ramonjd ramonjd commented Dec 16, 2025

What?

Related to #72612 (comment) and partial follow up to #73071

Update MediaThumbnailView to gracefully handle images that fail to load by falling back to icon + filename display.

The fallback handles existing non-image file types, which is the current behaviour.

Why?

Just in case the image doesn't load, yo!

How?

Using the existing mediaType icon as fallback.

Testing Instructions

Fire up StoryBook

npm run storybook:dev

Head to http://localhost:50240/?path=/story/fields-media-fields--data-views-preview

Screenshot 2025-12-16 at 2 05 05 pm

Check that non-image icons are present (see the file icon above)

…error handling

- Added a FallbackView component to display an icon and filename when the image fails to load or is not of image type.
- Integrated state management to track image loading errors.
- Updated the storybook to include a sample media item representing a broken image, demonstrating the new error handling functionality.
@ramonjd ramonjd self-assigned this Dec 16, 2025
@ramonjd ramonjd added [Type] Enhancement A suggestion for improvement. [Feature] Media Anything that impacts the experience of managing media labels Dec 16, 2025
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds fallback handling to the MediaThumbnailView component to gracefully handle images that fail to load by displaying an icon and filename instead of a broken image.

Key Changes:

  • Introduced error state tracking with useState to detect image load failures
  • Refactored component to extract fallback UI into a reusable FallbackView component
  • Added onError handler to the <img> element to trigger fallback display
  • Added test data with a broken image URL to demonstrate the fallback behavior in Storybook

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
packages/media-fields/src/media_thumbnail/view.tsx Adds error handling with useState hook, extracts FallbackView component, and implements onError callback for graceful degradation
packages/media-fields/src/stories/index.story.tsx Adds sampleMediaItemBrokenImage test fixture with a non-existent image URL to demonstrate fallback behavior

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

import { getMediaTypeFromMimeType } from '../utils/get-media-type-from-mime-type';
import type { MediaItem } from '../types';

function FallbackView( {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this doesn't really add much to what was already there, it just forks when there's an image load error.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it as a separate component, though, it improves readability 👍

@github-actions
Copy link

github-actions bot commented Dec 16, 2025

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: ramonjd <[email protected]>
Co-authored-by: andrewserong <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

}
sizes={ config?.sizes || '100vw' }
alt={ featuredMedia.alt_text || featuredMedia.title.raw }
onError={ () => setImageError( true ) }
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The special sauce

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, that's awesome! TIL: I did not know the img tag supported a native error handler like this... great stuff

@github-actions
Copy link

Flaky tests detected in 6866a24.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/20255361375
📝 Reported issues:

Copy link
Contributor

@andrewserong andrewserong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding this in, it adds a really nice level of polish to the field 👍

Also looks good in Grid layout:

image

LGTM! 🚀

import { getMediaTypeFromMimeType } from '../utils/get-media-type-from-mime-type';
import type { MediaItem } from '../types';

function FallbackView( {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it as a separate component, though, it improves readability 👍

@ramonjd ramonjd merged commit 0c0869e into trunk Dec 18, 2025
37 of 38 checks passed
@ramonjd ramonjd deleted the add/media-field-thumbnail-error-handling branch December 18, 2025 00:13
@github-actions github-actions bot added this to the Gutenberg 22.4 milestone Dec 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Feature] Media Anything that impacts the experience of managing media [Type] Enhancement A suggestion for improvement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants