
Product
Introducing Repository Access Permissions and Custom Roles
Socket now supports Custom Roles and Repository Access Permissions so organizations can control who can access specific repositories and actions.
next-image-fallback
Advanced tools
Drop-in replacement for next/image with automatic fallback support.
Drop-in replacement for next/image with automatic fallback support.
npm install next-image-fallback
# or
yarn add next-image-fallback
# or
pnpm add next-image-fallback
import { NextImageFallback } from 'next-image-fallback';
export default function AvatarExample() {
return (
<NextImageFallback
src="https://example.com/avatars/user-123.png"
fallbackSrc="/images/avatar-placeholder.png"
alt="User avatar"
width={128}
height={128}
/>
);
}
When both primary and fallback images fail, you can display a beautiful CSS-based placeholder instead of a broken image icon:
import { NextImageFallback, ALTERNATIVE_FALLBACK } from 'next-image-fallback';
export default function AvatarExample() {
return (
<NextImageFallback
src="https://example.com/avatars/user-123.png"
fallbackSrc="/images/avatar-placeholder.png"
alt="User avatar"
width={128}
height={128}
// Alternative fallback options
alternativeFallback="gradient" // 'gradient' | 'waves' | 'mono'
primaryAltColor="#6366f1" // Primary color (hex, rgb, or named)
secondaryAltColor="#8b5cf6" // Secondary color for gradient/waves
altErrorMessage="Image unavailable" // Custom error message
altTextColor="#ffffff" // Text color
onAlternativeFallback={() => console.log('Showing placeholder')}
/>
);
}
import { NextImageFallback, ALTERNATIVE_FALLBACK } from 'next-image-fallback';
// Use predefined constants
<NextImageFallback
src="/image.png"
alt="test"
width={200}
height={200}
alternativeFallback={ALTERNATIVE_FALLBACK.GRADIENT} // or WAVES, MONO
/>
| Type | Description |
|---|---|
gradient | Smooth diagonal gradient from primary to secondary color |
waves | Multi-layered radial gradient creating a wave-like effect |
mono | Solid single-color background using primary color |
// Using hex colors
<NextImageFallback
src="/image.png"
alt="test"
width={200}
height={200}
alternativeFallback="gradient"
primaryAltColor="#ff6b6b"
secondaryAltColor="#4ecdc4"
/>
// Using RGB colors
<NextImageFallback
src="/image.png"
alt="test"
width={200}
height={200}
alternativeFallback="waves"
primaryAltColor="rgb(99, 102, 241)"
secondaryAltColor="rgb(139, 92, 246)"
/>
// Using named colors
<NextImageFallback
src="/image.png"
alt="test"
width={200}
height={200}
alternativeFallback="mono"
primaryAltColor="slategray"
/>
next/image props are supported (ImageProps).| Prop | Type | Default | Description |
|---|---|---|---|
fallbackSrc | ImageProps['src'] | - | Alternative image to display when the primary one fails |
onFallback | () => void | - | Callback fired exactly once when switching to the fallback source |
| Prop | Type | Default | Description |
|---|---|---|---|
alternativeFallback | 'gradient' | 'waves' | 'mono' | - | Placeholder type to display when both images fail |
primaryAltColor | string | '#6366f1' | Primary color (supports hex, rgb, or named colors) |
secondaryAltColor | string | '#8b5cf6' | Secondary color for gradient/waves |
altErrorMessage | string | 'Image unavailable' | Custom error message displayed on placeholder |
altTextColor | string | '#ffffff' | Text color for the error message |
onAlternativeFallback | () => void | - | Callback fired when the alternative fallback is shown |
currentSrc state starting with src.fallbackSrc (if provided and not already used) and calls onFallback.naturalWidth === 0 in onLoadingComplete for environments where broken images still trigger completion.onError and onLoadingComplete handlers are invoked as well.src change.alternativeFallback is set and both images fail, a CSS-based placeholder is rendered with customizable colors and error message.next/image does not support fallbacks out of the box. Handling onError manually across a codebase is noisy, and broken image URLs are common in user-generated content or expired remote assets. next-image-fallback provides a lightweight, reusable wrapper so you can add a single fallbackSrc prop and move on.
New: The alternative fallback feature ensures users never see ugly broken image icons – they see a beautiful, branded placeholder instead.
The component works in both App Router and Pages Router projects on Next.js 13+. It simply wraps next/image, so static imports, remote patterns configured in next.config.js, width/height, and fill all work as usual.
Server rendering is unaffected; fallback logic only runs in the browser once the image loads or errors.
src change to avoid infinite loops.next.config.js still apply; ensure the fallback source is allowed (static asset, public folder, or configured remote).The package is fully typed. Import types as needed:
import type {
NextImageFallbackProps,
AlternativeFallbackType
} from 'next-image-fallback';
nipunarambukkanage
MIT © 2025 Nipuna Rambukkanage
FAQs
Drop-in replacement for next/image with automatic fallback support.
We found that next-image-fallback demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Socket now supports Custom Roles and Repository Access Permissions so organizations can control who can access specific repositories and actions.

Product
Socket MCP now lets AI assistants review org alerts, investigate threats using the Socket threat feed, and inspect package files in addition to dependency scoring.

Product
Socket Firewall blocks malicious VS Code and Open VSX extensions before install, protecting developers from compromised editor marketplaces.