diff --git a/lerna.json b/lerna.json index fca1be7..ed6c0b1 100644 --- a/lerna.json +++ b/lerna.json @@ -1,5 +1,5 @@ { "npmClient": "yarn", "$schema": "node_modules/lerna/schemas/lerna-schema.json", - "version": "0.3.5" + "version": "0.3.6-alpha.2" } diff --git a/packages/sanity-next/package.json b/packages/sanity-next/package.json index 6766513..9b4e76b 100644 --- a/packages/sanity-next/package.json +++ b/packages/sanity-next/package.json @@ -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 ", "license": "MIT", diff --git a/packages/sanity-next/src/lib/sourceMapping.ts b/packages/sanity-next/src/lib/sourceMapping.ts index e638623..148e3b3 100644 --- a/packages/sanity-next/src/lib/sourceMapping.ts +++ b/packages/sanity-next/src/lib/sourceMapping.ts @@ -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 diff --git a/packages/sanity-next/src/lib/urlBuilding.ts b/packages/sanity-next/src/lib/urlBuilding.ts index 1f3917e..665a97e 100644 --- a/packages/sanity-next/src/lib/urlBuilding.ts +++ b/packages/sanity-next/src/lib/urlBuilding.ts @@ -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() } @@ -53,7 +53,7 @@ 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) @@ -61,6 +61,15 @@ export function makeImageLoader( } } +// 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