Giter VIP home page Giter VIP logo

next-image-export-optimizer's Introduction

Next-Image-Export-Optimizer

Use Next.js advanced <Image/> component with the static export functionality. Optimizes all static images in an additional step after the Next.js static export.

  • Reduces the image size and page load times drastically through responsive images
  • Fast image transformation using sharp.js (also used by the Next.js server in production)
  • Conversion of JPEG and PNG files to the modern WEBP format by default
  • Serve the exported React bundle only via a CDN. No server required
  • Minimal configuration necessary
  • Supports TypeScript

This library makes a few assumptions:

  • All images that should be optimized are stored inside the public folder like public/images
  • Currently only local images are supported for optimization

Installation

npm install next-image-export-optimizer

Configure the library in your Next.js configuration file:

// next.config.js
module.exports = {
  images: {
    loader: "custom",
    nextImageExportOptimizer: {
      imageFolderPath: "public/images",
      exportFolderPath: "out",
      imageSizes: [16, 32, 48, 64, 96, 128, 256, 384],
      deviceSizes: [640, 750, 828, 1080, 1200, 1920, 2048, 3840],
      quality: 75,
    },
  },
  env: {
    storePicturesInWEBP: true,
  },
};
  1. Add the above configuration to your next.config.js

  2. Specify the folder where all the images are stored. Defaults to public/images

  3. Change the export command from next build && next export to next build && next export && npx next-image-export-optimizer

  4. Change the <Image /> component to the <ExportedImage /> component of this library.

    Example:

    // Old
    import Image from "next/image";
    
    <Image
      src="images/VERY_LARGE_IMAGE.jpg"
      alt="Large Image"
      layout="fill"
      objectFit="cover"
    />;
    
    // New
    import ExportedImage from "next-image-export-optimizer";
    
    <ExportedImage
      src="images/VERY_LARGE_IMAGE.jpg"
      alt="Large Image"
      layout="fill"
      objectFit="cover"
    />;
  5. In the development mode, the original image will be served as the optimized images are created at build time only. In the exported, static React app, the responsive images are available as srcset and dynamically loaded by the browser

Live example

You can see a live example of the use of this library at reactapp.dev/next-image-export-optimizer

How it works

The <ExportedImage /> component of this library wraps around the <Image /> component of Next.js. Using the custom loader feature, it generates a srcset for different resolutions of the original image. The browser can then load the correct size based on the current viewport size.

In the development mode, the <ExportedImage /> component falls back to the original image.

To get the optimized images you alter the Next.js export command from

next build && next export

to

next build && next export && npx next-image-export-optimizer

All images in the specified folder will be optimized and reduced versions will be created based on the requested widths.

The image transformation operation is optimized as it uses hashes to determine whether an images has already been optimized or not.

next-image-export-optimizer's People

Contributors

matobeno1 avatar niels-io avatar nielsapp avatar tmlamb avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.