next-images vs next-optimized-images
Next.js Image Optimization Packages Comparison
1 Year
next-imagesnext-optimized-images
What's Next.js Image Optimization Packages?

Both 'next-images' and 'next-optimized-images' are npm packages designed to enhance image handling in Next.js applications. 'next-images' allows developers to import images directly into their components, simplifying the process of using images in projects. On the other hand, 'next-optimized-images' focuses on optimizing images for better performance by automatically compressing and serving them in the most efficient formats, thus improving load times and overall user experience. While both packages serve the purpose of handling images, they cater to different needs within the image management workflow in Next.js.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
next-images121,44795210.9 kB172 years agoMIT
next-optimized-images16,8542,228-1345 years agoMIT
Feature Comparison: next-images vs next-optimized-images

Image Importing

  • next-images:

    The 'next-images' package allows developers to import images directly into their Next.js components using standard JavaScript import syntax. This simplifies the process of using images, making it easy to manage and reference images throughout the application without additional configuration.

  • next-optimized-images:

    While 'next-optimized-images' does not focus on importing images, it allows you to optimize images after they are imported. It supports various image formats and automatically handles the conversion and optimization process, but does not change the way images are imported into components.

Image Optimization

  • next-images:

    'next-images' does not provide any built-in optimization features. It is primarily focused on simplifying the import process, leaving developers to manage image optimization manually or through other means.

  • next-optimized-images:

    This package excels in automatic image optimization. It compresses images, converts them to modern formats like WebP, and serves them in the most efficient way, significantly improving load times and performance.

Configuration Complexity

  • next-images:

    The configuration for 'next-images' is straightforward and requires minimal setup. Developers can quickly integrate it into their Next.js project without extensive configuration, making it user-friendly for those who want simplicity.

  • next-optimized-images:

    'next-optimized-images' requires more configuration compared to 'next-images'. Developers need to set up various options for optimization, such as specifying image formats and optimization levels, which can be more complex but offers greater control over the optimization process.

Performance Impact

  • next-images:

    Using 'next-images' may lead to larger image sizes being served if no additional optimization is applied. This can negatively impact page load times, especially in image-heavy applications.

  • next-optimized-images:

    This package significantly enhances performance by reducing image sizes and serving them in optimized formats. The automatic optimization process ensures that images are lightweight, leading to faster load times and improved user experience.

Use Case Suitability

  • next-images:

    Best suited for projects where image optimization is not a priority, or for smaller applications where simplicity is key. Ideal for developers who want a quick and easy way to manage images without diving into optimization details.

  • next-optimized-images:

    Ideal for larger applications or those with a focus on performance, especially e-commerce sites or media-heavy platforms. This package is perfect for developers looking to ensure their images are optimized for the best user experience.

How to Choose: next-images vs next-optimized-images
  • next-images:

    Choose 'next-images' if your primary need is to easily import and use images within your Next.js components without additional optimization features. It is suitable for projects where image optimization is not a critical concern or where you prefer to handle it manually.

  • next-optimized-images:

    Opt for 'next-optimized-images' if you require automatic image optimization and want to ensure that your images are served in the best formats for performance. This package is ideal for projects where load speed and performance are priorities, especially for image-heavy applications.

README for next-images

Next.js + Images

npm npm npm

Import images in Next.js (jpg, jpeg, png, svg, fig, ico, webp, jp2 and avif images by default).

Features

  • Load images from local computer
  • Load images from remote (CDN for example) by setting assetPrefix
  • Inline small images to Base64 for reducing http requests
  • Adds a content hash to the file name so images can get cached

If you also want image minimalization and optimization have a look at next-optimized-images

Installation

npm install --save next-images

or

yarn add next-images

Usage

Create a next.config.js in your project

// next.config.js
const withImages = require('next-images')
module.exports = withImages()

Optionally you can add your custom Next.js configuration as parameter

// next.config.js
const withImages = require('next-images')
module.exports = withImages({
  webpack(config, options) {
    return config
  }
})

And in your components or pages simply import your images:

export default () => <div>
  <img src={require('./my-image.jpg')} />
</div>

or

import img from './my-image.jpg'

export default () => <div>
  <img src={img} />
</div>

Options

assetPrefix

You can serve remote images by setting assetPrefix option.

Dynamic (runtime) asset prefixes are also supported, you can enable this feature by setting dynamicAssetPrefix to true.

Example usage:

// next.config.js
const withImages = require('next-images')
module.exports = withImages({
  assetPrefix: 'https://example.com',
  dynamicAssetPrefix: true,
  webpack(config, options) {
    return config
  }
})

InlineImageLimit

Inlines images with sizes below inlineImageLimit to Base64. Default value is 8192.

Example usage:

// next.config.js
const withImages = require('next-images')
module.exports = withImages({
  inlineImageLimit: 16384,
  webpack(config, options) {
    return config
  }
})

Exclude

Folders that you want to exclude from the loader. Useful for svg-react-loader for example.

Example usage:

// next.config.js
const path = require('path');
const withImages = require('next-images')
module.exports = withImages({
  exclude: path.resolve(__dirname, 'src/assets/svg'),
  webpack(config, options) {
    return config
  }
})

File Extensions

You have the power to specifiy the file extensions you'd like to pass to this loader configuration. This is helpful for adding image types that behave similarly, but are not included by default. It's also helpful in the same way that exclude is helpful, because you can exclude all SVGs (not just one from a specific folder).

TypeScript Users: If you exclude a file suffix, please note our shipped types declaration file will be incorrect. You'll want to use declaration merging or override dependencies for the same file suffixes as needed.

Please note: If you have issues with a file suffix not included in our default list (["jpg", "jpeg", "png", "svg", "gif", "ico", "webp", "jp2", "avif"]), we won't be able to guarantee bug support.

Example usage:

// next.config.js
const withImages = require('next-images')
module.exports = withImages({
  fileExtensions: ["jpg", "jpeg", "png", "gif"],
  webpack(config, options) {
    return config
  }
})

Name

You can change the structure of the generated file names by passing the name option:

// next.config.js
const withImages = require('next-images')
module.exports = withImages({
  name: "[name].[hash:base64:8].[ext]",
  webpack(config, options) {
    return config
  }
})

The default value is "[name]-[hash].[ext]". Documentation for available tokens like [name], [hash], etc can be found in webpack/loader-utils

ES Modules

By default, file-loader generates JS modules that use the ES modules syntax. There are some cases in which using ES modules is beneficial, like in the case of module concatenation and tree shaking.

ES Modules are disabled by default. You can enable them by using esModule config option:

const withImages = require('next-images')
module.exports = withImages({
  esModule: true,
  webpack(config, options) {
    return config
  }
})

By enabling ES modules you should change your require statements and get default property out of them:

<img src={require("./img.png").default}>

import statement should be as before.

import img from "./img.png";

Typescript

Typescript doesn't know how interpret imported images. next-images package contains definitions for image modules, you need to add reference to next-images types into a type definition file, e.g. additional.d.ts, and then reference this from tsconfig.json.

// additional.d.ts
/// <reference types="next-images" />
// tsconfig.json
{
  ...
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", "additional.d.ts"],
  ...
}

See the Next.js docs for more information.

With next/image

Base4/Data URL encoding is not supported when using the next/image component for image optimization. To deactivate inline images you can set the inlineImageLimit to false:

// next.config.js
const withImages = require('next-images')
module.exports = withImages({
  inlineImageLimit: false
})

Bonus

Try out some of these awesome NextJS dashboard templates developed by Creative Team and support this project indirectly :)

https://www.creative-tim.com/product/nextjs-argon-dashboard-pro/?ref=next-images

https://www.creative-tim.com/product/nextjs-material-kit-pro/?ref=next-images

https://www.creative-tim.com/product/nextjs-material-kit/?ref=next-images