From 1d16db1916ca5763e7d8e6f1ffd7aa072b5977ef Mon Sep 17 00:00:00 2001 From: Robert Reinhard Date: Mon, 21 Aug 2023 22:22:37 -0700 Subject: [PATCH 1/6] Catch errors when fetching image URL --- packages/sanity-next/src/lib/urlBuilding.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/packages/sanity-next/src/lib/urlBuilding.ts b/packages/sanity-next/src/lib/urlBuilding.ts index 1f3917e..ed4000c 100644 --- a/packages/sanity-next/src/lib/urlBuilding.ts +++ b/packages/sanity-next/src/lib/urlBuilding.ts @@ -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 @@ -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 '' } } From 77a3c70eba6ba139f1df638755cc5347a3d1c5c8 Mon Sep 17 00:00:00 2001 From: Robert Reinhard Date: Mon, 21 Aug 2023 22:27:27 -0700 Subject: [PATCH 2/6] v0.3.6-alpha.0 --- lerna.json | 2 +- packages/sanity-next/package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lerna.json b/lerna.json index fca1be7..8c8f3f1 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.0" } diff --git a/packages/sanity-next/package.json b/packages/sanity-next/package.json index 6766513..031456f 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.0", "description": "Image and video renderer for Sanity + Next.js projects", "author": "Bukwild ", "license": "MIT", From 09d4e2ade14e247a146df8b5a59f0b3a88b443db Mon Sep 17 00:00:00 2001 From: Robert Reinhard Date: Mon, 21 Aug 2023 22:48:19 -0700 Subject: [PATCH 3/6] Switching to asset property detection approach --- packages/sanity-next/src/lib/urlBuilding.ts | 25 +++++++++------------ 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/packages/sanity-next/src/lib/urlBuilding.ts b/packages/sanity-next/src/lib/urlBuilding.ts index ed4000c..665a97e 100644 --- a/packages/sanity-next/src/lib/urlBuilding.ts +++ b/packages/sanity-next/src/lib/urlBuilding.ts @@ -24,8 +24,8 @@ export function makeImageUrl( source?: SanityImageSource, options?: imageUrlBuildingOptions ): string | undefined { - if (!source) return undefined - return getBuilderUrl(makeImageBuilder(source, options)) + if (!source || !sourceHasAsset(source)) return undefined + return makeImageBuilder(source, options).url() } // Add common conventions when building URLs to images @@ -53,24 +53,21 @@ 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 getBuilderUrl(builder) + return builder.url() } } -// 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 '' - } +// 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 From 4a44613aed350a85d04e3b67d83a6e19777c8824 Mon Sep 17 00:00:00 2001 From: Robert Reinhard Date: Mon, 21 Aug 2023 22:54:29 -0700 Subject: [PATCH 4/6] v0.3.6-alpha.1 --- lerna.json | 2 +- packages/sanity-next/package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lerna.json b/lerna.json index 8c8f3f1..6b36172 100644 --- a/lerna.json +++ b/lerna.json @@ -1,5 +1,5 @@ { "npmClient": "yarn", "$schema": "node_modules/lerna/schemas/lerna-schema.json", - "version": "0.3.6-alpha.0" + "version": "0.3.6-alpha.1" } diff --git a/packages/sanity-next/package.json b/packages/sanity-next/package.json index 031456f..553a625 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.6-alpha.0", + "version": "0.3.6-alpha.1", "description": "Image and video renderer for Sanity + Next.js projects", "author": "Bukwild ", "license": "MIT", From 23f1f7ded11cb1d8d234f3815aa44b89245c510c Mon Sep 17 00:00:00 2001 From: Robert Reinhard Date: Mon, 21 Aug 2023 22:58:05 -0700 Subject: [PATCH 5/6] Test that the asset has a value --- packages/sanity-next/src/lib/sourceMapping.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From e86bf27ed4d025ae805153f5fcef093062de4300 Mon Sep 17 00:00:00 2001 From: Robert Reinhard Date: Mon, 21 Aug 2023 22:58:21 -0700 Subject: [PATCH 6/6] v0.3.6-alpha.2 --- lerna.json | 2 +- packages/sanity-next/package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lerna.json b/lerna.json index 6b36172..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.6-alpha.1" + "version": "0.3.6-alpha.2" } diff --git a/packages/sanity-next/package.json b/packages/sanity-next/package.json index 553a625..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.6-alpha.1", + "version": "0.3.6-alpha.2", "description": "Image and video renderer for Sanity + Next.js projects", "author": "Bukwild ", "license": "MIT",