• Stars
    star
    585
  • Rank 76,419 (Top 2 %)
  • Language
    TypeScript
  • Created almost 2 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Multi-framework responsive image component

🌳 unpic-img

A cross-framework component for responsive, high-performance images using image CDNs

react React • vue Vue • solidjs SolidJS • svelte Svelte • astro Astro • preact Preact • qwik Qwik • webc WebC • angular Angular • lit Lit

Features

  • Just an <img> tag! No extra elements, no runtime JavaScript. Easy to style. No legacy hacks or workarounds.
  • Automatically generates correct srcset and sizes attributes for responsive images.
  • Handles responsive resizing of images, preserving aspect ratio and avoiding layout shift.
  • Uses native lazy loading and async decoding for offscreen images.
  • Handles different image layouts: fixed, constrained and full width.
  • Uses eager loading and high priority fetching for important images.
  • Delivers modern image formats, including WebP and AVIF if supported by the browser.
  • No build step or server-side rendering required for the images: uses your existing image CDN or CMS, with no additional configuration.
  • Uses unpic lib to support most image CDNs, including Cloudinary, Imgix, and Shopify.
  • Can generate a low-res background image for a blurred placeholder effect, or use with @unpic/placeholder for more options.

Usage

For details of usage, see the documentation.

FAQ

Why do I need this?

While it's easy to use an <img> tag for images, if you want to follow best practices and deliver the most performant image to your users then it can take a lot of work. Some frontend frameworks will automate this for you, but they often rely on slow pre-rendering of images, or on running image optimizers on your own site. They also generate complex HTML with wrappers and spacer elements that make images hard to style.

Most images on modern websites are hosted on a CDN or CMS that can resize images on the fly and deliver them at the edge. Despite this, most web frameworks will still download and resize the image at build time or on your server, rather than using the CDN, or just uses a single source image rather than handling multiple resolutions.

This library works with any frontend framework or none, and uses your existing image CDN or CMS, with no additional configuration.

For more details, see this post.

How does this work?

This library uses unpic to detect the image CDN, and then uses the CDN's URL API to resize and format images. It then generates the correct srcset and sizes attributes for the image. It uses new features built into modern browsers to handle lazy loading, fetch priority and decoding. It also uses pure CSS to handle responsive resizing of images, preserving aspect ratio and avoiding layout shift. Unlike most other image components, it does not use any client-side JavaScript by default, and generates just a single <img> tag without any wrapper divs or padding elements.

What HTML does this generate?

Generated HTML for a constrained image It turns this:
<Image
  src="https://cdn.shopify.com/static/sample-images/bath_grande_crop_center.jpeg"
  layout="constrained"
  width={800}
  height={600}
  alt="Shopify"
/>

...into this:

<img
  alt="Shopify"
  loading="lazy"
  decoding="async"
  sizes="(min-width: 800px) 800px, 100vw"
  srcset="
    https://cdn.shopify.com/static/sample-images/bath.jpeg?crop=center&amp;width=1600&amp;height=2133 1600w,
    https://cdn.shopify.com/static/sample-images/bath.jpeg?crop=center&amp;width=1280&amp;height=1707 1280w,
    https://cdn.shopify.com/static/sample-images/bath.jpeg?crop=center&amp;width=1080&amp;height=1440 1080w,
    https://cdn.shopify.com/static/sample-images/bath.jpeg?crop=center&amp;width=960&amp;height=1280   960w,
    https://cdn.shopify.com/static/sample-images/bath.jpeg?crop=center&amp;width=828&amp;height=1104   828w,
    https://cdn.shopify.com/static/sample-images/bath.jpeg?crop=center&amp;width=800&amp;height=1067   800w,
    https://cdn.shopify.com/static/sample-images/bath.jpeg?crop=center&amp;width=750&amp;height=1000   750w,
    https://cdn.shopify.com/static/sample-images/bath.jpeg?crop=center&amp;width=640&amp;height=853    640w
  "
  src="https://cdn.shopify.com/static/sample-images/bath.jpeg?width=800&amp;height=600&amp;crop=center"
  style="
        object-fit: cover;
        max-width: 800px;
        max-height: 600px;
        aspect-ratio: 1.33333 / 1;
        width: 100%;
      "
/>

Supported CDNs

You can use any image CDN supported by unpic lib, including:

  • Adobe Dynamic Media (Scene7)
  • Builder.io
  • Bunny.net
  • Cloudflare
  • Contentful
  • Cloudinary
  • Directus
  • Imgix, including Unsplash, DatoCMS, Sanity and Prismic
  • Kontent.ai
  • Shopify
  • Storyblok
  • Vercel / Next.js
  • WordPress.com and Jetpack Site Accelerator

License

Published under the MIT licence. © Matt Kane 2023.

More Repositories

1

trackless

Add a GDPR-friendly Google Analytics opt-in/opt-out button to your site
TypeScript
133
star
2

unpic

Universal image CDN translator
TypeScript
103
star
3

unpic-placeholder

Pure-CSS image placeholders
TypeScript
65
star
4

php-edge

Run PHP in WebAssembly on Netlify Edge Functions
TypeScript
58
star
5

react-artboard

A realistic paint component
TypeScript
55
star
6

canvas-paint-tool

Realistic canvas paint tool
JavaScript
53
star
7

og-edge

Generate Open Graph images with Deno and Netlify Edge Functions, no framework needed.
TypeScript
50
star
8

socket-io-actionscript

Socket.IO Actionscript 3 client
ActionScript
35
star
9

chalkstream

chalkstream
TypeScript
30
star
10

clevrlib

CLEVR Actionscript Library
ActionScript
15
star
11

php-stateless-cookies

Implements secure stateless cookies and user accounts.
PHP
11
star
12

vanilla-stream

React streaming with no meta-framework
TypeScript
9
star
13

sse-demo

Demo of server-sent events with Netlify Edge Functions
HTML
7
star
14

logarithmic-slider

Logarithmic UISlider subclass
Objective-C
6
star
15

get-pixels

🌈 🦕 Get the decoded pixel data from a JPG or PNG image
TypeScript
6
star
16

netligraph

TypeScript
6
star
17

airbed

Lightweight CouchDB client library for Adobe AIR
ActionScript
6
star
18

daneel

Template for an OpenAI chat bot app, built with React, Tailwind and TypeScript
TypeScript
4
star
19

remix-on-netlify

TypeScript
4
star
20

gatsby-plugin-static-image

TypeScript
3
star
21

localized-time

TypeScript
3
star
22

avalon

This repository was created by Gatsby Cloud
JavaScript
2
star
23

use-pwned-passwords

React custom hook to check passwords against the HaveIBeenPwned API
TypeScript
2
star
24

babel-jsx-utils

Utilities for working with Babel and JSX
TypeScript
2
star
25

aerian-headless-lightning

PHP
1
star
26

gatsby-sanity-benchmark

JavaScript
1
star
27

cdp-edge-inject

TypeScript
1
star
28

tweetdelete

JavaScript
1
star
29

using-static-images

JavaScript
1
star
30

shopp-google-checkout-currency-convert

A Shopp payment gateway with automatic currency conversion
1
star
31

departures

Live UK station departures and arrivals boards
TypeScript
1
star
32

adas-bake

Gatsby starter for a Contentful project.
JavaScript
1
star
33

ascorbic.github.com

HTML
1
star
34

vendor

Vendor submodules
1
star
35

doctrine-tracker

Just a tracker for the Doctrine svn server, so I can add it as a submodule in my /vendor/ project
PHP
1
star
36

gatsby-starter-rendering-modes

JavaScript
1
star
37

create-test

TypeScript
1
star
38

generate-fake-cms-data

TypeScript
1
star
39

gatsby-4-test-site

JavaScript
1
star