Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workaround Sanity url builder error when asset is empty #25

Merged
merged 6 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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