Discussion on using GROQ queries to download files and resolving asset references in block content.
12 replies
Last updated: May 3, 2023
J
So I came across this groq query
for downloading files. However, what if the
// GROQ query *[_type == 'movie'] { title, "manuscriptURL": manuscript.asset->url } // Then you can use the URL in HTML for example like this: // <a href={`${manuscriptURL}?dl=`}>Manuscript</a>
fileis added to your block content? Is it as simple as doing
"body": body[]{ ..., _type == 'file' => { ..., "documentUrl": asset->url } }
May 3, 2023, 3:59 AM
S
Neither 😉
You can generate full URLs to any asset — images and files — using just the asset’s ID, dataset and projectId with this library:
https://www.npmjs.com/package/@sanity/asset-utils
You can generate full URLs to any asset — images and files — using just the asset’s ID, dataset and projectId with this library:
https://www.npmjs.com/package/@sanity/asset-utils
May 3, 2023, 6:29 AM
J
So right now I am doing
Is there something for the size and original file name? I am also running into an issue doing
as I am getting but it is midnight here so I am probably missing something
attachments[]{ ..., "documentUrl": asset->url, "documentSize": asset->size, "documentOriginalFilename": asset->originalFilename, }
buildFileUrl(attachment._key, { dataset, projectId })
Error: Cannot use 'in' operator to search for 'originalFilename' in cc0020b7ff8d
May 3, 2023, 7:01 AM
S
Ah. The original file name and size you would need to resolve. And yes additionally resolve inside your block content.
On another note, try to avoid multiple `->`’s for the same document. That snippet is resolving the same query three times. Take a look at this section on
High performance GROQ
On another note, try to avoid multiple `->`’s for the same document. That snippet is resolving the same query three times. Take a look at this section on
High performance GROQ
May 3, 2023, 8:37 AM
J
Ahh I should do
// Merge a single subquery into the root level of the result *[_type == "category"] { title, "slug": slug.current, ...(parent-> { "parentTitle": title, "parentSlug": slug.current }) }
May 3, 2023, 2:55 PM
J
Actually it should be this right?
As I want to return all fields and resolve all fields in my attachments references. So I tried
but that returns
// Return all fields and resolve all fields in all 'categories' references *[_type == "product" && defined(salePrice) && salePrice < displayPrice]{ ..., categories[]-> }
"services": *[_type == "service" && references(^._id)]{ ..., attachments[]-> },
attachmentsas
null🤔
May 3, 2023, 3:01 PM
S
attachments[]->should resolve all fields from an array of references. If it’s returning null perhaps you’re targeting the wrong field or it is empty.
May 3, 2023, 3:08 PM
J
🤦♂️ it isn't a reference 😆
attachments: [ { asset: [Object], _type: 'file', name: 'Senior Connections Resource Guide', _key: 'cc0020b7ff8d' } ],
May 3, 2023, 3:09 PM
J
So I got the
buildFileUrlworking but just passing in the attachment. However, I want to get the file size if possible as well
May 3, 2023, 3:11 PM
S
You’ll need to resolve that from the asset reference.
May 3, 2023, 3:12 PM
J
Does this query look fine? It gives me what I want but was curious if there is any performance improvements I can make
May 3, 2023, 3:14 PM
S
I’d personally try to cut down on the number of
…spread operators that will end up sending you more data than you likely need. But it looks otherwise fine for now!
May 3, 2023, 3:15 PM
J
Ok one more groq question... Seeing as I can figure this issue out... How would I go about using
and then passing the URL for the portable text. And this time it does look like it's a reference
However, I would like to cut that
buildFileUrlfor a reference in block? So in my query I am currently doing
"body": body[]{ ..., _type == 'file' => { ..., "documentUrl": asset->url } }
{ asset: { _ref: 'file-f4005335534eb49e6edffea0b1596cd211b670e8-pdf', _type: 'reference' }, _type: 'file', description: 'Maricopa Community Center Senior Programming Calendar for May 2023', _key: '96b1944a9636', documentUrl: 'MY DOCUMENT URL' }
"documentUrl": asset->urlout and just use
buildFileUrlif possible but it's coming back as
undefined.undefinedif my query just returns
bodyinstead of doing
...
May 3, 2023, 3:22 PM
Sanity– build remarkable experiences at scale
Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.