How to get SVGs from Sanity like images?
Yes. Sanity accepts SVG as an image asset, and its CDN serves the original file. Querying the asset gives you a URL, not the markup. To use the SVG as real inline markup in React (with styling, currentColor, animation and so on), you can either fetch the original file from that URL and inline it, or store the SVG source in a text field. If an <img> tag is enough, just use the asset URL. SVGs stay sharp at any size either way.
Option 1: Upload as an image, then fetch the markup
SVG is listed among the supported image asset types (Assets). Image transformations only work on JPEG, PNG, WebP, PJPG, TIFF, AVIF and GIF, so URL parameters like w, h or rect won't do anything useful for an SVG. Request the plain base URL, which "serves the original asset" (Image transformations).
// schemaTypes/icon.ts
import {defineField, defineType} from 'sanity'
export const icon = defineType({
name: 'icon',
type: 'document',
fields: [
defineField({name: 'title', type: 'string'}),
defineField({name: 'image', type: 'image'}),
],
})// fetch the URL with GROQ, then the SVG text from the CDN
import {createClient} from '@sanity/client'
const client = createClient({
projectId: 'YOUR_PROJECT_ID',
dataset: 'production',
apiVersion: '2023-05-03',
useCdn: true,
})
export async function getIcon(id: string) {
const doc = await client.fetch(
`*[_type == "icon" && _id == $id][0]{title, "url": image.asset->url}`,
{id},
)
const svg = await fetch(doc.url).then((res) => res.text())
return {title: doc.title, svg}
}export function InlineSvg({svg}: {svg: string}) {
return <span dangerouslySetInnerHTML={{__html: svg}} />
}The URL follows the pattern https://cdn.sanity.io/images/<project id>/<dataset>/<asset id>-<width>x<height>.svg.
Option 2: Store the SVG source as text
Sanity's SVG in React guide uses this approach: paste the markup into a text field (Text type) and inject it in the frontend. The query then returns the markup directly, with no second request.
defineField({
name: 'svg',
type: 'text',
validation: (rule) => rule.required(),
})// query: *[_type == "icon"]{ _id, title, svg }
export function Icon({svg}: {svg: string}) {
return <svg dangerouslySetInnerHTML={{__html: svg}} />
}If you inject into an <svg> element as above, store only the inner elements, without the outer <svg> tag. Otherwise, wrap the markup in a <span> or <div>. The guide also shows how to put several stored SVGs into <symbol> elements and reuse them with <use href="#id" />, which builds an SVG sprite.
Notes
- Inlining markup with
dangerouslySetInnerHTMLruns whatever is in that SVG. Only allow it for content from trusted editors, or sanitize it first. - Uploading SVGs through the Studio image field is easier for editors. A text field is easier to query and lets you edit the markup in place. Choose based on who maintains the SVGs.
- If you don't need to style the SVG from code,
<img src={url} />with the asset URL is the simplest option (Presenting images).
Show original thread12 replies
Was this answer helpful?
Sanity – Build the way you think, not the way your CMS thinks
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.