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 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-optimized-images

v2.1.0

Published

A light package to optimize images for React!

Downloads

229

Readme

react-optimized-images

npm version

A package to optimize images for React! It generates and uses responsive webp images with fallback to other formats, resulting in great performance on modern browsers and still supporting old browsers.

NOTE: If you created your app with CRA and it's not ejected or you don't have webpack as your bundler, you can use v1.

Table of contents

Features

  • Convert images to webp during build time.
  • Create smaller versions of images based on breakpoints to avoid unnecessary load of big images on smaller devices.
  • Lazy load images that are not in the viewport.
  • React Component to make use of all the features with ease.

NOTE: Currently only images that are located within the project will be optimized.

TODO

  • Add image optimization for external images.
  • Add image optimization for CSS background images.
  • Add support to older Node versions.
  • Add unit tests.
  • Add custom configuration at component level.

Requirements

  • Node v14.0.0 or above installed.
  • Webpack as your project bundler.

Installation

npm install react-optimized-images --save

or

yarn add react-optimized-images

Usage

Webpack plugin

This package generates images at build time, so you need to add the webpack plugin in your webpack.config.js.

const OptimizedImagesPlugin = require('react-optimized-images/plugin');

module.exports = {
  // ... webpack config
  plugins: [
    // ... other plugins
    new OptimizedImagesPlugin({ ...options }),
  ],
};

Options

| Name | Type | Default value | Description | | ----------- | ------- | ------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------- | | minWidth | number | 200 | Mininum image width to create smaller versions. | | breakpoints | array | Default value | Specifies the breakpoints to generate responsive images. You can add as many breakpoints as you want. | | enabled | boolean | true | If false, the images won't be generated and the component will render a regular image. | | lazy | boolean | false | If true, all the images in the Picture component will be lazy loaded by default. It can be overriden by the lazy prop in the component. |

Default breakpoints
[
  {
    maxWidth: 576, // Max screen width in px
    resizeTo: 50, // Percentage of original image to resize
    // Example: An image with 1000px would be resized to 500px in a 576px or smaller screen
  },
  {
    maxWidth: 992,
    resizeTo: 70,
  },
];

Complete custom configuration example:

const OptimizedImagesPlugin = require('react-optimized-images/plugin');

module.exports = {
  // ... webpack config
  plugins: [
    // ... other plugins
    new OptimizedImagesPlugin({
      minWidth: 300,
      breakpoints: [
        {
          maxWidth: 576,
          resizeTo: 50,
        },
        {
          maxWidth: 992,
          resizeTo: 80,
        },
      ],
      enabled: process.env.NODE_ENV === 'production',
      lazy: false,
    }),
  ],
};

Next.js plugin

This package also has a plugin for Next.js projects. In your next.config.js add:

const withOptimizedImages = require('react-optimized-images/next');

module.exports = withOptimizedImages();

Example with custom configuration:

const withOptimizedImages = require('react-optimized-images/next');

module.exports = withOptimizedImages({
  minWidth: 300,
  breakpoints: [
    {
      maxWidth: 576,
      resizeTo: 50,
    },
    {
      maxWidth: 992,
      resizeTo: 80,
    },
  ],
});

Or if you use next-compose-plugins:

const withPlugins = require('next-compose-plugins');
const optimizedImages = require('react-optimized-images/next');

module.exports = withPlugins([
  // other plugins
  [
    optimizedImages,
    /* Custom configuration
    {
       minWidth: 300
    },
    */
  ],
]);

Picture component

For an easier use of the generated images, you can use the Picture component, a wrapper for html <picture> tag. It looks for the optimized webp images and uses the default format as a fallback for older browsers, providing full support to all browsers.

Usage

import React from 'react';
import { Picture } from 'react-optimized-images';

import CoffeeJpg from '../assets/coffee.jpg';

export default function Home() {
  return (
    <div>
      <Picture src={CoffeeJpg} />
      {/* ..with Next v11.. */}
      <Picture src={CoffeeJpg.src} />
      {/* ..or if it comes from public folder */}
      <Picture src="/coffee.jpg" />
    </div>
  );
}

The output will be like

<picture>
  <source
    srcset="/[email protected]"
    media="(max-width: 576px)"
    type="image/webp"
  />
  <source
    srcset="/[email protected]"
    media="(max-width: 992px)"
    type="image/webp"
  />
  <source srcset="/coffee.webp" type="image/webp" />
  <source
    srcset="/[email protected]"
    media="(max-width: 576px)"
    type="image/jpeg"
  />
  <source
    srcset="/[email protected]"
    media="(max-width: 992px)"
    type="image/jpeg"
  />
  <source srcset="/coffee.jpg" type="image/jpeg" />
  <img src="/coffee.jpg" />
</picture>

Properties

It uses the same properties from HTML <img> tag. Along with some specific properties as shown below:

| Name | Type | Default value | Description | | ---- | ---- | ------------- | ----------- | | lazy | boolean | false | If true, the image will be loaded only when content is ready and the image is inside the viewport. | preview | ReactNode | undefined | A custom preview to be shown during image load if lazy is true. If preview is not set and height and width props are defined, a blurry version of the original image will be shown.

License

MIT © Marcelo Dornelles Junior