Skip to content

Commit

Permalink
Merge pull request #25 from BKWLD/catch-sanity-url-builder-errors
Browse files Browse the repository at this point in the history
Workaround Sanity url builder error when asset is empty
  • Loading branch information
weotch authored Aug 22, 2023
2 parents ec07afb + e86bf27 commit 20d94b5
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"npmClient": "yarn",
"$schema": "node_modules/lerna/schemas/lerna-schema.json",
"version": "0.3.5"
"version": "0.3.6-alpha.2"
}
2 changes: 1 addition & 1 deletion packages/sanity-next/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@react-visual/sanity-next",
"version": "0.3.5",
"version": "0.3.6-alpha.2",
"description": "Image and video renderer for Sanity + Next.js projects",
"author": "Bukwild <[email protected]>",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion packages/sanity-next/src/lib/sourceMapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function aspectRatioFromSource(
): number | undefined {
if (!source) return undefined
if (typeof source != 'object' || !('asset' in source)) return undefined
return source.asset.metadata?.dimensions?.aspectRatio
return source.asset?.metadata?.dimensions?.aspectRatio
}

// Make object-position values from the hotspot data
Expand Down
13 changes: 11 additions & 2 deletions packages/sanity-next/src/lib/urlBuilding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function makeImageUrl(
source?: SanityImageSource,
options?: imageUrlBuildingOptions
): string | undefined {
if (!source) return undefined
if (!source || !sourceHasAsset(source)) return undefined
return makeImageBuilder(source, options).url()
}

Expand Down Expand Up @@ -53,14 +53,23 @@ export function makeImageBuilder(source: SanityImageSource, {
export function makeImageLoader(
source?: SanityImageSource
): ImageLoader | undefined {
if (!source) return undefined
if (!source || !sourceHasAsset(source)) return undefined
return ({ width, quality }: ImageLoaderProps): string => {
let builder = makeImageBuilder(source, { width })
if (quality) builder = builder.quality(quality)
return builder.url()
}
}

// Check if the source has a populated asset field. This was necessary because
// UlrBuilder fatally errors when the image source has an asset property
// with a null value. And this was the case when uploading images with the
// preview tab open.
export function sourceHasAsset(source: SanityImageSource): boolean {
if (!source) return false
return typeof source == 'object' && 'asset' in source && source.asset
}

// Return the URL of an asset
export function makeFileUrl(
source?: SanityFileSource
Expand Down

0 comments on commit 20d94b5

Please sign in to comment.