next-export-optimize-images

repository·main·Indexed 19 days ago

https://github.com/dc7290/next-export-optimize-images

A library for Next.js that enables image optimization at build time for static exports (next export). It allows the use of next/image features without a running Node.js server by processing images during the build process. Key features include format conversion (e.g., PNG to WebP/AVIF), external image downloading, and high-performance processing using sharp. It provides specialized components like Picture, RemoteImage, and RemotePicture to support various optimization scenarios and is compatible with TypeScript and the Next.js App Router.

Tokens
10.5K
Snippets
42
Records
55
Agent score
60%

What's inside next-export-optimize-images

  1. Overview of next-export-optimize-images features

    main

    This package enables the full benefits of next/image for static Next.js exports (using next export) by performing image optimization during the build process rather than at runtime.

    Key features include:

    • Build-time optimization: Processes images during the build to support static hosting.
    • Format conversion: Automatically converts images to modern formats (e.g., png → webp).
    • External image support: Downloads external images locally for optimization.
    • High performance: Uses sharp for fast processing and implements caching to prevent redundant optimizations.
    • Compatibility: Supports TypeScript and the Next.js App Router.
  2. Optimize images at build time with next-export-optimize-images

    main
    The next-export-optimize-images library allows you to use the full benefits of next/image even when using next export (static site generation). It solves the limitation where Next.js image optimization typically requires a running server by performing the optimization during the build process instead.
  3. Compare next-export-optimize-images with next-image-export-optimizer

    main

    When deciding between next-export-optimize-images and next-image-export-optimizer, consider these trade-offs:

    Use next-export-optimize-images if you:

    • Want a simple setup.
    • Need to support multiple image formats within a <Picture> component.
    • Want to optimize images in advance during next build while using a Node.js server.
    • Need easy remote image optimization.

    next-image-export-optimizer characteristics:

    • Processes images within a specified directory (e.g., public/images).
    • Limitations: Settings can be complex/cumbersome, it does not support all next/image options, and it only handles one file extension.
  4. Optimize remote images via remoteImages configuration

    main

    To optimize remote images, you can list specific URLs in the remoteImages array within your export-images.config.js. Images listed here, as well as those used with the RemoteImage component, will be downloaded locally and optimized during the export process.

    Note: If you use the standard Image component from next-export-optimize-images/image with a remote URL, that URL must be present in the remoteImages config to be optimized.

    /**
     * @type {import('next-export-optimize-images').Config}
     */
    const config = {
      remoteImages: ['https://example.com/image01.jpg', 'https://example.com/image02.jpg'],
    }
    
    module.exports = config
    import Image from 'next-export-optimize-images/image'
    import RemoteImage from 'next-export-optimize-images/remote-image'
    
    function Component() {
      return (
        <>
          {/* Optimized because it is in remoteImages config */}
          <Image src="https://example.com/image01.jpg" alt="" />
          <Image src="https://example.com/image02.jpg" alt="" />
          
          {/* Optimized because it uses RemoteImage component */}
          <RemoteImage src="https://example.com/image03.jpg" alt="" />
        </>
      )
    }
  5. Key features of next-export-optimize-images

    main

    The library provides the following capabilities for static Next.js exports:

    • Build-time optimization: Images are processed during the build, making them compatible with static hosting.
    • Full next/image support: All standard options available in the next/image component are supported.
    • Format conversion: Automatically converts images to modern formats (e.g., png to webp).
    • External image downloading: Can download images from external URLs and store them locally for optimization.
    • High performance: Uses sharp for fast image processing.
    • Caching: Implements a cache to prevent redundant optimization tasks.
    • Modern Next.js support: Fully supports TypeScript and the App Router.
  6. Compare next-export-optimize-images with next-optimized-images

    main

    When deciding between next-export-optimize-images and next-optimized-images, consider these trade-offs:

    Use next-export-optimize-images if you:

    • Want to use the standard next/image component for optimization.
    • Are using responsive-loader but are concerned about bloated bundle sizes caused by long srcSet strings.

    next-optimized-images characteristics:

    • Uses webpack's loader feature for optimization.
    • Does not require the use of next/image.
    • Note: This library has not been updated since August 9, 2020, and can lead to larger bundle sizes when handling srcSet.
  7. Use external images with the Image component

    main

    Once configured in export-images.config.js, you can use external images with the standard Image component.

    Behavioral differences:

    • In Production: The library optimizes the images at build time. The rendered <img> tag will use local paths pointing to the optimized chunks (e.g., src="/_next/static/chunks/images/...") and include a srcset for different resolutions.
    • In Development: No optimization or downloading occurs. The <img> tag will point directly to the external URL with query parameters for width (e.g., src="https://example.com/image.png?width=3840").
    <Image src="https://next-export-optimize-images.vercel.app/og.png" width="1280" height="640" alt="" />
  8. Configure withExportImages in next.config.js

    main

    Wrap your Next.js configuration with withExportImages. Note that withExportImages is an asynchronous function that returns a Promise. You must either wrap the configuration first or await the Promise before applying other plugins to ensure correct execution.

    If you are using CommonJS (next.config.js), you can wrap your config directly or use an async function to handle the Promise resolution when combining with other plugins.

    const withExportImages = require('next-export-optimize-images')
    
    module.exports = withExportImages({
      output: 'export',
      // write your next.js configuration values.
    })