Assets

Serving files

Non-image assets are delivered by their own route, with the same content-addressed ids and the same origin-derived URLs.

Files — PDFs, archives, plain text — use the same upload path and the same id derivation as images, and are delivered by their own route:

text
/files/default/production/<asset>.txt

In a schema they are ordinary file fields or array members, and they can carry their own metadata:

ts
defineField({
  name: 'attachments',
  type: 'array',
  of: [
    defineArrayMember({
      type: 'file',
      fields: [defineField({ name: 'description', type: 'string' })],
    }),
  ],
})

Downloads

Sanity's ?dl= flag is not yet honoured by the delivery route. Until it is, use the browser-side equivalent:

tsx
<a href={fileUrl} download>
  {description}
</a>

What to hold in a document

A reference to the asset, never a URL. URLs are derived from the request origin at read time, and a URL stored in a document is a hostname that will eventually be the wrong one.