Skip to content

add support for SVG rasterization#15154

Closed
jcayzac wants to merge 6 commits intowithastro:mainfrom
jcayzac:svg-rasterization
Closed

add support for SVG rasterization#15154
jcayzac wants to merge 6 commits intowithastro:mainfrom
jcayzac:svg-rasterization

Conversation

@jcayzac
Copy link
Contributor

@jcayzac jcayzac commented Jan 9, 2026

Changes

The built-in image service now supports converting SVG to any other supported format.
Minimal change which was already marked as TODO in 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> and getImage().

/cc @withastro/maintainers-docs for feedback!

@changeset-bot
Copy link

changeset-bot bot commented Jan 9, 2026

🦋 Changeset detected

Latest 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

@github-actions github-actions bot added the pkg: astro Related to the core `astro` package (scope) label Jan 9, 2026
@codspeed-hq
Copy link

codspeed-hq bot commented Jan 9, 2026

CodSpeed Performance Report

Merging this PR will not alter performance

Comparing jcayzac:svg-rasterization (84289a0) with main (37ab65a)1

Summary

✅ 9 untouched benchmarks

Footnotes

  1. No successful run was found on main (7f449b7) during the generation of this report, so 37ab65a was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

delete options.format;
}

delete options.format;
Copy link
Member

Choose a reason for hiding this comment

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

Why the change here?

Copy link
Contributor Author

@jcayzac jcayzac Jan 9, 2026

Choose a reason for hiding this comment

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

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.

Copy link
Member

Choose a reason for hiding this comment

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

No, it was put there to ensure the mime type is correct, it was a fix to this issue #14962

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Princesseuh Got it. I'll revert it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Princesseuh Done.

Copy link
Member

@delucis delucis 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 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?

@jcayzac
Copy link
Contributor Author

jcayzac commented Jan 9, 2026

Fixed a few things:

  • svg > raster works simply by removing the guards enforcing format to be svg if the input is SVG.
  • svg > svg was broken because I stupidly removed some other check. Reverted now.
  • raster > svg still gives an unsupported conversion error too.

@jcayzac
Copy link
Contributor Author

jcayzac commented Jan 9, 2026

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.

Hi @delucis!

Could you clarify what you mean? Images only get rasterized if the user explicitly sets format to a raster format. They never get rasterized "by default", so I don't understand all the above comments.

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?

OpenGraph images and other microdata thumbnails used in many places don't support SVG. You need to provide raster equivalents to the crawlers.

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.

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.

@jcayzac
Copy link
Contributor Author

jcayzac commented Jan 9, 2026

Ah I have introduced a bug: webp is indeed added when format is omitted. It should not be added if the source format is svg, to preserve current behavior. Else, I'd need to enforce an explicit format for SVG, which would be a breaking change.

@jcayzac
Copy link
Contributor Author

jcayzac commented Jan 9, 2026

Fixed default format for SVG so nothing happens by default, preserving today's behavior.

@stelcodes
Copy link

@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.

@stelcodes
Copy link

Something to consider adding to the docs: Sharp will throw an error when attempting to rasterize SVGs with embedded fonts.

@Princesseuh Princesseuh changed the base branch from main to next January 12, 2026 17:12
@Princesseuh Princesseuh changed the base branch from next to main January 12, 2026 17:13
@Princesseuh
Copy link
Member

Moving this to next in #15180

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg: astro Related to the core `astro` package (scope)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants