Skip to content

Commit

Permalink
Catch errors when fetching image URL
Browse files Browse the repository at this point in the history
  • Loading branch information
weotch committed Aug 22, 2023
1 parent ec07afb commit 1d16db1
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions packages/sanity-next/src/lib/urlBuilding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function makeImageUrl(
options?: imageUrlBuildingOptions
): string | undefined {
if (!source) return undefined
return makeImageBuilder(source, options).url()
return getBuilderUrl(makeImageBuilder(source, options))
}

// Add common conventions when building URLs to images
Expand Down Expand Up @@ -57,7 +57,19 @@ export function makeImageLoader(
return ({ width, quality }: ImageLoaderProps): string => {
let builder = makeImageBuilder(source, { width })
if (quality) builder = builder.quality(quality)
return builder.url()
return getBuilderUrl(builder)
}
}

// Get the URL from an imageBuilder or gracefully fail. This is to solve for
// issues I experienced in Sanity preview preview mode when uploading images.
// The `asset` property of the image source would be empty and this would
// cause the `imageBuilder` to fatally error.
function getBuilderUrl(builder: ImageUrlBuilder): string {
try { return builder.url() }
catch(e) {
console.error(e)
return ''
}
}

Expand Down

0 comments on commit 1d16db1

Please sign in to comment.