All files / components/custom strapi-image.tsx

90.9% Statements 10/11
91.66% Branches 11/12
100% Functions 2/2
100% Lines 7/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51                                                            1x 1x   1x                         5x 4x 3x 2x  
import Image from "next/image";
import { getStrapiURL } from "@/lib/utils";
 
interface StrapiImageProps {
  src: string;
  alt: string | null;
  className?: string;
  [key: string]: string | number | boolean | undefined | null;
}
 
/**
 * React component wrapper for Next.js Image with Strapi URL resolution.
 * 
 * KEY DIFFERENCES FROM strapiImage() utility:
 * - Renders a Next.js <Image> component (with optimization: resizing, lazy loading, format conversion)
 * - Used in JSX/component code for rendering blog images, product galleries, etc.
 * - Automatically handles responsive images and performance optimization
 * - Returns JSX instead of a string URL
 * 
 * USES NEXT_PUBLIC_STRAPI_BASE_URL (via getStrapiURL):
 * - This is the public domain URL accessible from the browser
 * - Essential for client-side rendering in production
 * - Set to https://strapiadmin.bitmutex.com in production
 */
export function StrapiImage({
  src,
  alt,
  className,
  ...rest
}: Readonly<StrapiImageProps>) {
  const imageUrl = getStrapiMedia(src);
  Iif (!imageUrl) return null;
 
  return <Image src={imageUrl} alt={alt || "No alt text provided."} className={className} {...rest} />;
}
 
/**
 * Helper function to construct image URLs with base URL handling.
 * Used internally by StrapiImage component.
 * 
 * Handles:
 * - Relative Strapi URLs (adds base URL)
 * - Absolute URLs and data URIs (returns as-is)
 * - Null/undefined values (returns null)
 */
export function getStrapiMedia(url: string | null) {
  if (url == null) return null;
  if (url.startsWith("data:")) return url;
  if (url.startsWith("http") || url.startsWith("//")) return url;
  return getStrapiURL() + url;
}