Issue with getting the originalFilename of an asset when using the getFileAsset function.
Based on the @sanity/asset-utils library documentation, I need to clarify an important point about the originalFilename option you're passing to getFileAsset:
The originalFilename option doesn't work the way you're expecting. Here's why:
The Issue
The @sanity/asset-utils package does not resolve information from the Sanity API - it only works with the information you pass to it. This is explicitly stated in the library's documentation:
"This package does not resolve any information from the Sanity APIs - it does no asynchronous operations and only has the information that you pass it."
What originalFilename Actually Does
Looking at the getVanityStub function, the originalFilename option is used for building URLs with vanity filenames (the human-readable filename at the end of asset URLs), not for retrieving the original filename from an asset.
The Solution
To get the originalFilename of an asset, you need to query it from the Sanity API directly. The asset document itself contains this field. Here's how:
import {getClient} from '@sanity/client'
const client = getClient({
projectId: 'xxxxxxx',
dataset: 'staging',
apiVersion: '2024-01-01',
useCdn: false
})
// Query the asset document to get originalFilename
const asset = await client.fetch(
`*[_id == $assetId][0]{
_id,
url,
originalFilename
}`,
{assetId: 'file-xxxxx-xxx'}
)
console.log(asset.originalFilename) // This will have the original filenameThe @sanity/asset-utils library is designed for parsing and building asset URLs/paths from known information, not for fetching asset metadata from the API. For metadata like originalFilename, size, mimeType, etc., you'll need to query the Sanity Content Lake directly.
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.