StrapiImage
Optimized image component for rendering Strapi media with Next.js Image optimization.
Import
import { StrapiImage } from 'strapi-nextgen-framework';Usage
<StrapiImage
data={imageData}
nextImageProps={{
priority: true,
className: "rounded-lg",
}}
/>Props
data (required)
Type: StrapiMedia['data']['attributes'] | null | undefined
The image data from Strapi. Can be extracted from your GraphQL query response.
type StrapiMediaAttributes = {
url: string;
alternativeText?: string | null;
width?: number | null;
height?: number | null;
formats?: {
thumbnail?: MediaFormat;
small?: MediaFormat;
medium?: MediaFormat;
large?: MediaFormat;
} | null;
};Example:
const imageData = page.heroImage?.data?.attributes;
<StrapiImage data={imageData} />nextImageProps (optional)
Type: Partial<ImageProps>
Pass-through props for Next.js <Image /> component. All standard Next.js Image props are supported.
Common props:
| Prop | Type | Description |
|---|---|---|
priority | boolean | Load image eagerly (for LCP images) |
quality | number | Image quality (1-100, default 75) |
placeholder | 'blur' | 'empty' | Placeholder while loading |
fill | boolean | Fill parent container |
sizes | string | Responsive image sizes |
className | string | CSS class name |
style | CSSProperties | Inline styles |
onLoad | () => void | Callback when image loads |
onError | () => void | Callback on error |
Example with multiple props:
<StrapiImage
data={imageData}
nextImageProps={{
priority: true,
quality: 90,
className: "w-full h-auto rounded-xl shadow-2xl",
sizes: "(max-width: 768px) 100vw, (max-width: 1200px) 50vw, 33vw",
}}
/>Behavior
Automatic Responsive Images
StrapiImage automatically uses Strapi's responsive image formats:
- Checks for
formatsin the image data - Uses the best format for different screen sizes
- Falls back to original if no formats exist
Accessibility
- ✅ Automatically includes
alttext fromalternativeText - ✅ Falls back to empty string if not provided (decorative image)
- ✅ Proper ARIA attributes for screen readers
Performance
- ✅ Lazy loading by default (set
priority={true}for above-fold images) - ✅ Next.js automatic image optimization
- ✅ Responsive srcSet generation
- ✅ Automatic format selection (WebP, AVIF)
Error Handling
- ✅ Returns
nullifdataisnullorundefined - ✅ Console warning in development if required fields missing
- ✅ Graceful degradation if formats unavailable
Examples
Basic Usage
import { strapi } from '@/lib/strapi';
import { StrapiImage } from 'strapi-nextgen-framework';
import { GetPageDocument } from '@/graphql/generated';
export default async function Page() {
const data = await strapi.getPage('about', GetPageDocument);
const image = data.page?.data?.attributes?.image?.data?.attributes;
return (
<div>
<StrapiImage data={image} />
</div>
);
}Hero Image (LCP)
For above-the-fold images, use priority={true}:
<StrapiImage
data={heroImage}
nextImageProps={{
priority: true, // Load eagerly for LCP
className: "w-full h-[600px] object-cover",
}}
/>Fill Mode (Background)
For images that fill a container:
<div className="relative w-full h-[400px]">
<StrapiImage
data={backgroundImage}
nextImageProps={{
fill: true,
className: "object-cover",
sizes: "100vw",
}}
/>
</div>Responsive Sizes
Optimize for different screen sizes:
<StrapiImage
data={productImage}
nextImageProps={{
sizes: "(max-width: 640px) 100vw, (max-width: 1024px) 50vw, 33vw",
className: "w-full h-auto",
}}
/>With Placeholder Blur
<StrapiImage
data={galleryImage}
nextImageProps={{
placeholder: "blur",
blurDataURL: "/blur-placeholder.jpg",
}}
/>Gallery Grid
<div className="grid grid-cols-3 gap-4">
{images.map((img) => (
<StrapiImage
key={img.id}
data={img.attributes}
nextImageProps={{
className: "rounded-lg hover:scale-105 transition-transform",
sizes: "(max-width: 768px) 50vw, 33vw",
}}
/>
))}
</div>TypeScript
The component is fully typed with TypeScript:
import type { StrapiMedia } from 'strapi-nextgen-framework';
interface PageProps {
image: StrapiMedia;
}
function MyComponent({ image }: PageProps) {
return <StrapiImage data={image.data?.attributes} />;
}Common Issues
Image Not Showing
Problem: Image component renders but no image appears
Solutions:
- Check
NEXT_PUBLIC_STRAPI_URLin.env.local - Verify image exists in Strapi Media Library
- Check browser console for 403/404 errors
- Ensure
urlfield is in your GraphQL query
TypeScript Errors
Problem: Type errors on data prop
Solutions:
- Run
npm run codegento regenerate types - Check that your GraphQL query includes all required fields:
image { data { attributes { url alternativeText width height } } }
Next.js Image Error
Problem: "Invalid src prop"
Solutions:
- Add Strapi domain to
next.config.js:images: { remotePatterns: [ { protocol: 'http', hostname: 'localhost', port: '1337', }, ], },
See Also
- Next.js Image Documentation (opens in a new tab)
- Strapi Media Library (opens in a new tab)
- StrapiRenderer - For dynamic components