Problem/Motivation
When SVG files are uploaded to an image field and the field is configured to use a Responsive Image formatter, the rendered output breaks.
Image styles do not generate derivatives for SVG files.
As a result, when a Responsive Image style is applied, Drupal attempts to generate responsive image source URLs for SVG files, which leads to broken derivative URLs in the rendered markup.
This issue commonly occurs when using the svg_image contributed module (or similar modules that allow SVG uploads in image fields), but the underlying problem is that the Responsive Image formatter does not defensively handle unsupported image types.
Currently:
SVG files can be uploaded to image fields.
Responsive Image styles can be applied to those fields.
The rendered output includes invalid derivative URLs for SVG images.
The SVG image therefore appears broken on the frontend.
Additionally, since SVG files often contain an internal <title> element for accessibility, there is an opportunity to improve UX by optionally allowing the system to use that title and bypass the required alt text field when appropriate.
Steps to reproduce
Install Drupal core (tested on 10.x/11.x).
Install and enable the svg_image contributed module (or any module that allows SVG uploads in image fields).
Create or edit a content type and add an image field.
Configure the field display to use a Responsive Image formatter.
Configure a Responsive Image style for the field.
Upload an SVG file to the image field.
View the rendered entity.
Result:
The responsive image <source> elements reference derivative URLs that do not exist.
The SVG image renders incorrectly or appears broken.
Expected result:
SVG images should either:
Be rendered without responsive image derivatives, or
Fall back to the original file URL, or
Use a defined fallback image style.
Proposed resolution
Add defensive handling in the Responsive Image formatter to detect unsupported image types (such as SVG) and avoid generating responsive image derivative URLs for them.
Possible approaches:
- Detect SVG file extensions or MIME types and bypass responsive derivative generation.
- Render the original file URL directly when image styles do not support the file type.
- Allow configuration of a fallback image style for unsupported formats.
Optional enhancement:
Detect a valid <title> element within the SVG file and optionally use it as the alt text, allowing sites to relax the required alt text constraint for SVG images where appropriate.
Note:
When using the svg_image_responsive submodule (provided by svg_image), this issue does not occur because it replaces the Responsive Image formatter. However, adding defensive checks in core would improve robustness and prevent broken output even when contrib workarounds are not installed.
Remaining tasks
- Determine whether the fix belongs in core Responsive Image formatter or should remain in contrib.
- Implement defensive detection for unsupported image types.
- Add automated test coverage for SVG handling in responsive image formatting.
- Confirm no regressions for supported raster image formats (JPG, PNG, WebP, etc.).
- Review and testing.
User interface changes
None required for the core defensive fix.
If the optional SVG title-to-alt feature is implemented, it may introduce minor UI adjustments to allow configuration of that behavior.
Introduced terminology
None.
API changes
No new public APIs are required.
The change would introduce additional internal validation/handling in the Responsive Image formatter to prevent generation of invalid derivatives for unsupported image types.
The change should be backward compatible and only affect cases where unsupported image formats (such as SVG) are used with Responsive Image styles.
| Comment | File | Size | Author |
|---|---|---|---|
| #15 | drupal-responsive_image_svg_support-2957924-15.patch | 941 bytes | djdevin |
| #11 | drupal-responsive_image_svg_support-2957924-11.patch | 865 bytes | azinck |
Issue fork drupal-2957924
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #3
mgiffordExtra bonus would be if it looked for a good
titlewithin the SVG and allowed you to skip the required alt text.Comment #4
jhedstromThat would be pretty slick, but also probably a feature request against the image field (since it would benefit non-responsive images too.)
Comment #5
djsagar commentedHi,
Try this module hope it's working for you https://www.drupal.org/project/responsive_svg
Thanks!
Comment #11
azinck commentedHere's a patch. It displays the original file if the image style doesn't support SVG. This is different from the approach used in #2652138: Image styles do not play nicely with SVGs on of hiding the image by default, but I don't fully understand why that decision was made. Also, doing something similar here would require more substantial refactoring of the responsive_image module's API because of the way it delegates grabbing the image styles to _responsive_image_image_style_url().
Anyhow, this is a first pass. Feedback welcome.
Comment #12
smustgrave commentedThis issue is being reviewed by the kind folks in Slack, #need-reveiw-queue. We are working to keep the size of Needs Review queue [2700+ issues] to around 200
Thank you for the patch.
This issue will need an issue summary update following the standard template I believe.
Also could use a test case.
Personally I wonder if such a feature should be configurable.
Comment #14
mahmoud barhouma commentedThank you for the patch. #11
Comment #15
djdevinI feel this is a bug and not a feature request?
_responsive_image_image_style_url is deprecated in 11.3, code moved to ResponsiveImageBuilder
Comment #16
smustgrave commentedSure, possible to get test coverage though?
Also will need an issue summary for the standard template
Comment #18
alorencComment #24
saidatomComment #25
saidatomComment #27
saidatomSteps to reproduce:
@herve001 also noticed that ImageUrlFormatter::viewElements() has the same unguarded buildUrl() call
Comment #28
herved commentedRegarding svg_image, I believe this patch is not necessary when using svg_image_responsive (submodule of svg_image). It can be enabled and replaces the responsive_image formatter class.
However, the proposed changes introduce a defensive check against unsupported image types, which seems like a good idea nonetheless.
Comment #29
claudiu.cristeaComment #30
smustgrave commentedHave no reviewed but summary is incomplete, needs to use the issue template fully.
Comment #31
sivaji_ganesh_jojodae commentedComment #32
saidatomComment #33
smustgrave commentedThink this should get sub-maintainer sign off.
Comment #34
smustgrave commentedBumping to framework now.
Comment #35
anybodyI agree with #28 - and shouldn't we try implementing a solution in core for SVG in general, not only for responsive images but for image styles in general? Replacing svg_image module.
Is there an issue for that yet, which I didn't find? Then we should link it here.
I'm thinking of something like a skip for image styles (via code like here or as setting or as a "Skip" image effect that skips the following effects - all if first - if the image is an SVG or GIF)
Comment #36
anybodySorry the issue title tricked me. Looking into the code this also covers image styles already, not like I've written in #35 but maybe good enough for core?
Comment #37
anybodyComment #38
natts