-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #188 from bluecadet/fix/sanity
add sanity fixes
- Loading branch information
Showing
7 changed files
with
129 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@bluecadet/launchpad-content": minor | ||
--- | ||
|
||
Update mediaDownloader and sanityImageUrlTransform defaults |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
# Transforming Sanity Images | ||
|
||
When working with Sanity.io images, you can leverage Sanity's built-in image transformation capabilities instead of using the `sharp` plugin. This is particularly useful for handling Sanity's hotspot and crop features. | ||
|
||
## Fetching Images with GROQ | ||
|
||
First, ensure your GROQ query includes all necessary image fields: | ||
|
||
```typescript{12-17} | ||
import { defineConfig } from '@bluecadet/launchpad-core'; | ||
import { sanitySource } from '@bluecadet/launchpad-content'; | ||
export default defineConfig({ | ||
content: { | ||
sources: [ | ||
sanitySource({ | ||
id: 'content', | ||
projectId: 'your-project-id', | ||
queries: [{ | ||
id: 'pages', | ||
query: `*[_type == "page"]{ | ||
image { | ||
..., | ||
asset-> | ||
} | ||
}` | ||
}] | ||
}) | ||
], | ||
plugins: [ | ||
mediaDownloader() | ||
] | ||
} | ||
}); | ||
``` | ||
|
||
The `asset->` reference is crucial for accessing the full image data, including hotspot and crop information. | ||
|
||
## Using the Image URL Transform Plugin | ||
|
||
Add the `sanityImageUrlTransform` plugin to transform image references into URLs: | ||
|
||
```typescript{14-22} | ||
import { defineConfig } from '@bluecadet/launchpad-core'; | ||
import { sanityImageUrlTransform, sanitySource } from '@bluecadet/launchpad-content'; | ||
export default defineConfig({ | ||
content: { | ||
sources: [ | ||
sanitySource({ | ||
id: 'content', | ||
projectId: 'your-project-id', | ||
queries: [/* ... */] | ||
}) | ||
], | ||
plugins: [ | ||
sanityImageUrlTransform({ | ||
projectId: 'your-project-id', | ||
dataset: 'production', | ||
buildUrl: (builder) => builder | ||
.width(800) | ||
.format('webp') | ||
.fit('crop') | ||
.crop('center') | ||
}), | ||
mediaDownloader() | ||
] | ||
} | ||
}); | ||
``` | ||
|
||
>[!TIP] | ||
>It's added _before_ the mediaDownloader (unlike the `sharp` plugin) because it modifies | ||
## Available Transformations | ||
|
||
Sanity's image URL builder supports many transformations: | ||
|
||
```typescript | ||
import { defineConfig } from '@bluecadet/launchpad-core'; | ||
import { sanityImageUrlTransform, sanitySource } from '@bluecadet/launchpad-content'; | ||
|
||
export default defineConfig({ | ||
content: { | ||
sources: [ | ||
sanitySource({ | ||
id: 'content', | ||
projectId: 'your-project-id', | ||
queries: [/* ... */] | ||
}) | ||
], | ||
plugins: [ | ||
sanityImageUrlTransform({ | ||
projectId: 'your-project-id', | ||
dataset: 'production', | ||
buildUrl: (builder) => builder | ||
.width(800) // Set width | ||
.height(600) // Set height | ||
.format('webp') // Convert format | ||
.quality(80) // Adjust quality | ||
.auto('format') // Auto-select best format | ||
.fit('crop') // Crop fitting | ||
.crop('center') // Crop position | ||
.blur(10) // Apply blur | ||
}), | ||
mediaDownloader() | ||
] | ||
} | ||
}); | ||
``` | ||
|
||
## Resources | ||
|
||
- [Sanity Image URLs Reference](https://www.sanity.io/docs/image-url) | ||
- [Image Image Presentation Documentation](https://www.sanity.io/docs/presenting-images) | ||
- [sanityImageUrlTransform Plugin](../reference/content/plugins/sanity-image-url-transform.md) | ||
- [Sanity Content Source](../reference/content/sources/sanity-source.md) | ||
|
||
Unlike the `sharp` plugin, Sanity's image transformations are performed on their CDN, reducing your build time and server load. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters