When I want to include a single directory component with a fallback for missing components, I use an array with multiple possibilities and the ignore_missing keyword.
This still logs an error Drupal\Core\Render\Component\Exception\ComponentNotFoundException: Unable to find component, while I would expect the ignore_missing keyword would silence this exception.
In the example below, in media.html.twig, I want to load a single directory component theme_name:media-{media_bundle}, with a fallback on theme_name:media if the former doesn't exist.
{{ include([
'theme_name:media-' ~ media.bundle()|clean_class,
'theme_name:media',
],{
content: content,
media: media,
component_attributes: attributes,
component_id: media.id(),
view_mode: view_mode,
logged_in: logged_in,
},
with_context = false,
ignore_missing = true
)}}
For media of bundle image, it looks for both theme:media-image and theme_name:media. In this case the component theme_name:media-image doesn't exist, but I have an existing component theme_name:media. In my logs I got a Drupal\Core\Render\Component\Exception\ComponentNotFoundException: Unable to find component "theme_name:media-image" in the component repository.