add support for SVG rasterization#15154
Conversation
🦋 Changeset detectedLatest commit: 84289a0 The changes in this PR will be included in the next version bump. Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
CodSpeed Performance ReportMerging this PR will not alter performanceComparing Summary
Footnotes |
| delete options.format; | ||
| } | ||
|
|
||
| delete options.format; |
There was a problem hiding this comment.
The previous code only existed to pass the verifyOptions() call. It makes no sense anymore now that the check on the svg input format is gone.
There was a problem hiding this comment.
No, it was put there to ensure the mime type is correct, it was a fix to this issue #14962
delucis
left a comment
There was a problem hiding this comment.
Thanks for the PR!
I think this may be problematic as is. There are quite a few SVG features which Sharp doesn't support and because the default format is WebP, that could lead to user images breaking with the default props.
A main goal of the image APIs is to optimise file size to improve page load times for users, so I'd also be a bit nervous that this change could mean default props producing larger output file sizes than input, which is undesirable. There are some circumstances where rasterizing an SVG can produce smaller output (small icons with relatively complex paths e.g.) but it's not super common.
It could also be a bit awkward to work with images in this model if you didn't want SVGs to be rasterized. For example, if a user can provide an image in any format, you'd have to sniff out the SVGs and use conditionals to explicitly render them as such.
I think it might be good to better understand the use case here and why rasterizing SVGs would be valuable. Could you share a bit more?
|
Fixed a few things:
|
Hi @delucis! Could you clarify what you mean? Images only get rasterized if the user explicitly sets
OpenGraph images and other microdata thumbnails used in many places don't support SVG. You need to provide raster equivalents to the crawlers.
That's definitely not a use case. I don't know why anybody would want to rasterize an SVG image they're showing on their website. It's not what people rasterize SVG images for. |
|
Ah I have introduced a bug: |
|
Fixed default format for SVG so nothing happens by default, preserving today's behavior. |
|
@delucis Regarding use case, I'd like to generate a cache-bustable PNG favicon from an SVG "source of truth" favicon since SVG favicon's are not well supported. I can also imagine generating cache-bustable OpenGraph PNGs from SVG sources as well. These are two areas where having a raster image is actually preferred. |
|
Something to consider adding to the docs: Sharp will throw an error when attempting to rasterize SVGs with embedded fonts. |
Co-authored-by: Chris Swithinbank <[email protected]>
|
Moving this to next in #15180 |
Changes
The built-in image service now supports converting SVG to any other supported format.
Minimal change which was already marked as
TODOin code.Testing
Waiting to see how the team feels about this change before I commit any time to adding tests (and welcoming any advice as to how to implement those should the rest be OK).
Docs
New feature, will need some update to the image service docs,
<Image>andgetImage()./cc @withastro/maintainers-docs for feedback!