Gathering detailed insights and metrics for next-sanity-image
Gathering detailed insights and metrics for next-sanity-image
Gathering detailed insights and metrics for next-sanity-image
Gathering detailed insights and metrics for next-sanity-image
Utility for using responsive images hosted on the Sanity.io CDN with the Next.js image component.
npm install next-sanity-image
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
149 Stars
67 Commits
20 Forks
10 Watching
2 Branches
14 Contributors
Updated on 28 Oct 2024
Minified
Minified + Gzipped
TypeScript (100%)
Cumulative downloads
Total Downloads
Last day
3.7%
3,802
Compared to previous day
Last week
8.8%
20,924
Compared to previous week
Last month
3.7%
88,168
Compared to previous month
Last year
14.5%
970,229
Compared to previous year
1
3
26
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
Version 5.0.0 of this library has removed support for the blur options. The reason for this is that this could not be correctly standardised from the library, the only way to support blur up was to request a low quality placeholder image from the Sanity CDN. Sanity already provides a base 64 lqip from the asset's metadata (https://www.sanity.io/docs/image-metadata#74bfd1db9b97).
Checkout the Responsive layout example on how to use the lqip in your Image component.
All next/image
component layouts are supported. 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 { createClient } 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 = createClient({ 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(configuredSanityClient, mySanityData.image); 15 16 return ( 17 <Img 18 {...imageProps} 19 style={{ width: '100%', height: 'auto' }} // layout="responsive" prior to Next 13.0.0 20 sizes="(max-width: 800px) 100vw, 800px" 21 placeholder="blur" 22 blurDataURL={mySanityData.image.asset.metadata.lqip} 23 /> 24 ); 25}; 26 27// Replace this with your logic for fetching data from the Sanity API. 28export const getServerSideProps = async function (context) { 29 const { slug = '' } = context.query; 30 31 const data = await configuredSanityClient.fetch( 32 `{ 33 "mySanityData": *[_type == "mySanityType" && slug.current == $slug][0] { 34 image { 35 asset->{ 36 ..., 37 metadata 38 } 39 } 40 } 41 }`, 42 { slug } 43 ); 44 45 return { props: data }; 46}; 47 48export default Page;
1// ... see "Responsive layout" 2 3const Page = ({ mySanityData }) => { 4 const imageProps = useNextSanityImage(configuredSanityClient, mySanityData.image); 5 6 return ( 7 <Img 8 {...imageProps} 9 style={{ maxWidth: '100%', height: 'auto' }} // layout="intrinsic" prior to Next 13.0.0 10 placeholder="blur" 11 blurDataURL={mySanityData.image.asset.metadata.lqip} 12 /> 13 ); 14}; 15 16// ... see "Responsive layout"
1// ... see "Responsive layout" 2 3const Page = ({ mySanityData }) => { 4 const imageProps = useNextSanityImage(configuredSanityClient, mySanityData.image); 5 6 return ( 7 <Img 8 {...imageProps} 9 placeholder="blur" 10 blurDataURL={mySanityData.image.asset.metadata.lqip} 11 /> 12 ); 13}; 14 15// ... 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(configuredSanityClient, mySanityData.image); 5 6 return ( 7 <Img 8 src={imageProps.src} 9 loader={imageProps.loader} 10 fill // layout="fill" prior to Next 13.0.0 11 objectFit="contain" 12 /> 13 ); 14}; 15 16// ... 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.
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.
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 // https://nextjs.org/docs/api-reference/next/image#loader 6 loader: ImageLoader 7}
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.
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(configuredSanityClient, mySanityData.image, { 14 imageBuilder: myCustomImageBuilder 15 }); 16 17 return <Img {...imageProps} layout="responsive" sizes="(max-width: 800px) 100vw, 800px" />; 18}; 19 20//...
<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
UseNextSanityImageBuilder
UseNextSanityImageBuilderOptions
UseNextSanityImageDimensions
No vulnerabilities found.
No security vulnerabilities found.