Gathering detailed insights and metrics for @fevernova90/next-sanity-image
Gathering detailed insights and metrics for @fevernova90/next-sanity-image
Gathering detailed insights and metrics for @fevernova90/next-sanity-image
Gathering detailed insights and metrics for @fevernova90/next-sanity-image
Utility for using responsive images hosted on the Sanity.io CDN with the Next.js image component.
npm install @fevernova90/next-sanity-image
Typescript
Module System
Node Version
NPM Version
TypeScript (92.88%)
JavaScript (7.12%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
151 Stars
69 Commits
22 Forks
9 Watchers
2 Branches
14 Contributors
Updated on Jun 24, 2025
Latest Version
0.0.4
Package Id
@fevernova90/next-sanity-image@0.0.4
Unpacked Size
26.57 kB
Size
6.69 kB
File Count
7
NPM Version
8.5.1
Node Version
16.14.0
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
1
3
23
Utility for using images hosted on the Sanity.io CDN with the Next.js image component. This library:
next/image
component.next/image
props.npm install --save next-sanity-image
This library also expects you to pass in a SanityClient instance, if you haven't installed this already:
npm install --save @sanity/client
Finally configure your next.config.js to allow loading images from the Sanity.io CDN
1module.exports = { 2 images: { 3 domains: ['cdn.sanity.io'], 4 loader: 'custom' 5 } 6};
All next/image
component layouts are supported (https://nextjs.org/docs/api-reference/next/image#layout). Below you can find a usage example for each of the supported layouts.
It's recommended to use the responsive layout for the best compatibility with different devices and resolutions. It's required to set the sizes
attribute using this layout (https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#attr-sizes).
1import sanityClient from '@sanity/client'; 2import Img from 'next/image'; 3import { useNextSanityImage } from 'next-sanity-image'; 4 5// If you're using a private dataset you probably have to configure a separate write/read client. 6// https://www.sanity.io/help/js-client-usecdn-token 7const configuredSanityClient = sanityClient({ 8 projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID, 9 dataset: process.env.NEXT_PUBLIC_SANITY_DATASET, 10 useCdn: true 11}); 12 13const Page = ({ mySanityData }) => ( 14 const imageProps = useNextSanityImage( 15 configuredSanityClient, 16 mySanityData.image 17 ); 18 19 return ( 20 <Img {...imageProps} layout="responsive" sizes="(max-width: 800px) 100vw, 800px" /> 21 ); 22); 23 24// Replace this with your logic for fetching data from the Sanity API. 25export const getServerSideProps = async function (context) { 26 const { slug = '' } = context.query; 27 28 const data = await configuredSanityClient.fetch( 29 `{ 30 "mySanityData": *[_type == "mySanityType" && slug.current == $slug][0] { 31 image 32 } 33 }`, 34 { slug } 35 ); 36 37 return { props: data }; 38}; 39 40export default Page;
1// ... see "Responsive layout" 2 3const Page = ({ mySanityData }) => ( 4 const imageProps = useNextSanityImage( 5 configuredSanityClient, 6 mySanityData.image 7 ); 8 9 return ( 10 <Img {...imageProps} layout="intrinsic" /> 11 ); 12); 13 14// ... see "Responsive layout"
1// ... see "Responsive layout" 2 3const Page = ({ mySanityData }) => ( 4 const imageProps = useNextSanityImage( 5 configuredSanityClient, 6 mySanityData.image 7 ); 8 9 return ( 10 <Img {...imageProps} layout="fixed" /> 11 ); 12); 13 14// ... see "Responsive layout"
Omit the width
and height
props returned from useNextSanityImage
when using a fill layout, as this fills the available space of the parent container. You probably also want to set the objectFit
prop to specify how the object resizes inside the container.
1// ... see "Responsive layout" 2 3const Page = ({ mySanityData }) => ( 4 const imageProps = useNextSanityImage( 5 configuredSanityClient, 6 mySanityData.image 7 ); 8 9 return ( 10 <Img src={imageProps.src} loader={imageProps.loader} layout="fill" objectFit="contain" /> 11 ); 12); 13 14// ... see "Responsive layout"
Blur-up placeholders are enabled by default as of Next.js 11.0.0. It's possible to customize the blur amount, quality and width of the placeholder image by modifying the options of useNextSanityImage
. If you require more advanced image transformations, check out the chapter on Image transformations.
1// ... see "Responsive layout"
2
3const Page = ({ mySanityData }) => (
4 const imageProps = useNextSanityImage(
5 configuredSanityClient,
6 mySanityData.image,
7 {
8 blurUpImageWidth: 124,
9 blurUpImageQuality: 40,
10 blurUpAmount: 24
11 }
12 );
13
14 return (
15 <Img {...imageProps} layout="responsive" sizes="(max-width: 800px) 100vw, 800px" />
16 );
17);
18
19// ... see "Responsive layout"
React hook which handles generating a URL for each of the defined sizes in the image sizes and device sizes Next.js options.
SanityClient
Pass in a configured instance of the SanityClient, used for building the URL using the @sanity/image-url builder.
SanityImageSource
| null
A reference to a Sanity image asset, can be retrieved by using the Sanity API. You can pass in any asset that is also supported by the image() method of @sanity/image-url. This parameter can be set to null
in order to not load any image.
Enables the blur-up placeholder image. Defaults to true.
The quality of the blur-up placeholder image, ranging from 0 - 100. Defaults to 30.
The width of the blur-up placeholder image (in pixels). Defaults to 64.
The amount of blur applied to the blur-up placeholder image, ranging from 0 - 100. Defaults to 50.
function(/* see below */)
property | type | description |
---|---|---|
imageUrlBuilder | ImageUrlBuilder | @sanity/image-url builder to apply image transformations. |
options | UseNextSanityImageBuilderOptions | Options object with relevant context passed to the callback, see properties below. |
options.width | number | null | The width for the current srcSet entry, if set to null this is the entry for the src fallback attribute. |
options.originalImageDimensions | { width: number, height: number, aspectRatio: number } : UseNextSanityImageDimensions | Object containing dimensions of the original image passed to the image parameter. |
options.croppedImageDimensions | { width: number, height: number, aspectRatio: number } : UseNextSanityImageDimensions | The cropped dimensions of the image, if a crop is supplied. Otherwise, the same as originalImageDimensions . |
options.quality | number | null | The quality of the image as passed to the quality prop of the next/image component. |
An optional function callback which allows you to customize the image using the ImageUrlBuilder
. This function is called for every entry in the image sizes and device sizes, and is used to define the URL's outputted in the srcSet
attribute of the image.
Defaults to:
1(imageUrlBuilder, options) => { 2 return imageUrlBuilder 3 .width(options.width || Math.min(options.originalImageDimensions.width, 1920)) 4 .quality(options.quality || 75) 5 .fit('clip'); 6};
For an example on how to use this, read the chapter on Image transformations.
function(/* see below */)
property | type | description |
---|---|---|
imageUrlBuilder | ImageUrlBuilder | @sanity/image-url builder to apply image transformations. |
options | UseNextSanityImageBuilderOptions | Options object with relevant context passed to the callback, see properties below. |
options.width | number | null | The width for the current srcSet entry, if set to null this is the entry for the src fallback attribute. |
options.originalImageDimensions | { width: number, height: number, aspectRatio: number } : UseNextSanityImageDimensions | Object containing dimensions of the original image passed to the image parameter. |
options.croppedImageDimensions | { width: number, height: number, aspectRatio: number } : UseNextSanityImageDimensions | The cropped dimensions of the image, if a crop is supplied. Otherwise, the same as originalImageDimensions . |
options.quality | number | null | The quality of the image as passed to the quality prop of the next/image component. |
options.blurAmount | number | null | The amount of blur applied to the image (ranging from 0 - 100). |
An optional function callback which allows you to customize the blur-up placeholder image using the ImageUrlBuilder
. This function is called for every entry in the image sizes and device sizes, and is used to define the URL outputted in the blurDataURL
attribute of the image.
It's recommended to use the blurUpImageQuality
, blurUpImageWidth
and/or blurUpAmount
options to modify the blur-up placeholder. Only use this image builder when you also want to apply other transformations to the blur-up placeholder image.
Please note that it's recommended to keep the width and quality of the blur-up placeholder low, as this placeholder image will be replaced directly after load!
Defaults to:
1(imageUrlBuilder, options) => { 2 return imageUrlBuilder 3 .width(options.width || 64) 4 .quality(options.quality || 30) 5 .blur(options.blurAmount || 50) 6 .fit('clip'); 7};
For more information on how to use this, read the chapter on Image transformations.
If the image
parameter is set to null
, the return value of this hook will also be null
. This allows you to handle any conditional rendering when no image is loaded. If an image
is set, to following result (UseNextSanityImageProps
) will be returned:
1{ 2 src: string, 3 width: number, 4 height: number, 5 6 // Properties below change based on the specified 'enableBlurUp' option 7 placeholder: 'blur' | 'empty', 8 blurDataURL?: string, 9 10 // https://nextjs.org/docs/api-reference/next/image#loader 11 loader: ImageLoader 12}
Custom transformations to the resulting image can be made by implementing the imageBuilder
callback function. Note that it's recommended to implement a memoized callback, either by implementing the function outside of the component function scope or by making use of useCallback
. Otherwise the props will be recomputed for every render.
The same can be done for the blur-up placeholder image by using the blurUpImageBuilder
option.
1//... 2 3const myCustomImageBuilder = (imageUrlBuilder, options) => { 4 return imageUrlBuilder 5 .width(options.width || Math.min(options.originalImageDimensions.width, 800)) 6 .blur(20) 7 .flipHorizontal() 8 .saturation(-100) 9 .fit('clip'); 10}; 11 12const Page = ({ mySanityData }) => ( 13 const imageProps = useNextSanityImage( 14 configuredSanityClient, 15 mySanityData.image, 16 { imageBuilder: myCustomImageBuilder } 17 ); 18 19 return ( 20 <Img {...imageProps} layout="responsive" sizes="(max-width: 800px) 100vw, 800px" /> 21 ); 22); 23 24//...
<img />
element with a srcSet
attribute, the width
and height
prop being returned by the React hook is uniform for each size. Cropping an image is possible using the ImageUrlBuilder
, however you have to return an image with the same aspect ratio for each of the defined sizes. Art direction is currently not supported (both by next/image and this library).If the functionality mentioned above is desired, please file an issue stating your specific use case so we can look at the desired behavior and possibilities.
The following types are exposed from the library:
ImageUrlBuilder
UseNextSanityImageProps
UseNextSanityImageOptions
UseNextSanityBlurUpImageBuilder
UseNextSanityBlurUpImageBuilderOptions
UseNextSanityImageBuilder
UseNextSanityImageBuilderOptions
UseNextSanityImageDimensions
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
3 existing vulnerabilities detected
Details
Reason
Found 7/30 approved changesets -- score normalized to 2
Reason
2 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 1
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
Project has not signed or included provenance with any releases.
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More