npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@erickluis00/image-optimizer

v1.1.3

Published

Image optimization for React using Hono as backend and Sharp as image processor

Downloads

20

Readme

image-optimizer

Next.js-like Image component with automatic optimization using Sharp. React and Hono friendly (currently)

Features

  • ✅ Automatic image optimization with Sharp
  • ✅ Server-side caching
  • ✅ External URLs and local images support
  • ✅ Lazy loading by default
  • ✅ Smooth loading transitions
  • ✅ Error handling with fallback UI
  • ✅ TypeScript support
  • ✅ SSR compatible
  • ✅ Zero dependencies (except Sharp)

Installation

npm install image-optimizer
# or
pnpm add image-optimizer
# or
yarn add image-optimizer

Important: This package has two entry points:

  • image-optimizer - Client-safe Image component (use in React components)
  • image-optimizer/server - Server-only middleware (use in Node.js server setup using HONO)

Usage

1. Setup Middleware (Server-side only)

import { Hono } from 'hono'
import { createImageMiddleware } from 'image-optimizer/server'

const app = new Hono()

// Basic usage with defaults
const imageMiddleware = createImageMiddleware()
imageMiddleware(app)

// Or with custom configuration
const imageMiddleware = createImageMiddleware({
  path: '/image',                    // Custom endpoint path (default: '/image')
  cacheControl: 'public, max-age=31536000',  // Custom cache header
  headers: {                       // Additional headers
    'X-Custom-Header': 'value'
  },
})
imageMiddleware(app)

2. Use Image Component (Client-side safe)

import { Image } from 'image-optimizer'

export function MyPage() {
  return (
    <div>
      {/* External URL */}
      <Image 
        src="https://example.com/photo.jpg" 
        alt="Photo" 
        width={800} 
        height={600} 
      />

      {/* Local path */}
      <Image 
        src="/images/local.jpg"
        alt="Local" 
        width={400} 
        height={300} 
      />

      {/* With custom format and quality */}
      <Image 
        src="https://example.com/photo.jpg" 
        alt="Photo" 
        width={1200} 
        format="avif" 
        quality={90}
        className="w-full"
      />

      {/* Priority loading (hero images) */}
      <Image 
        src="https://example.com/hero.jpg" 
        alt="Hero" 
        width={1920} 
        height={600} 
        priority
        quality={95}
      />

      {/* With callbacks */}
      <Image 
        src="https://example.com/photo.jpg" 
        alt="Photo" 
        width={600} 
        height={400}
        onLoad={() => console.log('Loaded!')}
        onError={() => console.error('Error!')}
      />
    </div>
  )
}

API

Image Component Props

| Prop | Type | Default | Description | |------|------|---------|-------------| | src | string | required | Image URL or local path | | alt | string | required | Alt text for accessibility | | width | number | undefined | Target width in pixels | | height | number | undefined | Target height in pixels | | quality | number | 80 | Image quality (1-100) | | format | 'webp' \| 'jpeg' \| 'png' \| 'avif' \| 'gif' | 'webp' | Output format | | loading | 'lazy' \| 'eager' | 'lazy' | Loading behavior | | priority | boolean | false | Load with high priority | | className | string | undefined | Additional CSS classes | | onLoad | () => void | undefined | Callback when loaded | | onError | () => void | undefined | Callback on error |

Middleware Configuration

| Option | Type | Default | Description | |--------|------|---------|-------------| | path | string | '/image' | Endpoint path for image optimization | | cacheControl | string | 'public, max-age=31536000, immutable' | Cache-Control header | | headers | Record<string, string> | {} | Additional custom headers |

Examples

Custom Middleware Path

const imageMiddleware = createImageMiddleware({
  path: '/api/optimize-image'
})
imageMiddleware(app)

Custom Cache Headers

const imageMiddleware = createImageMiddleware({
  cacheControl: 'public, max-age=86400, s-maxage=31536000',
  headers: {
    'X-Image-Processor': 'vike-sharp-image',
    'X-CDN': 'cloudflare'
  }
})
imageMiddleware(app)

License

MIT