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

nextjs-image-optimization-loader

v1.3.0

Published

custom image loader for optimization without nextjs server

Downloads

16

Readme

WebP Image Optimizer Loader for Next.js

This Webpack loader optimizes images by converting them to WebP format and creating multiple sizes (responsive breakpoints) for use with the Next.js Image component. This allows the Next.js Image component's loader function to dynamically serve the appropriate image size based on the user's viewport, resulting in faster load times and improved performance.

Features

  • Automatic WebP Conversion: Automatically converts images to WebP format, a modern image format that provides superior compression.
  • Responsive Images: Generates multiple image sizes based on predefined breakpoints (e.g., sm, md, lg, xl).
  • Seamless Integration: Works out of the box with Next.js Image component by providing a custom loader function.

Installation

To install the package, run:

npm install nextjs-image-optimization-loader

Usage

  1. Create Custom Image loader file for Nextjs Image component (This is useful when you want to add loader for every image component in project. If not, use loader property in Image component each)

in your root folder, add:

// next-custom-image-loader.js
import RESULT from './result';

const SCREEN_BREAK_POINT = {
    sm: 640,
    md: 768,
    lg: 1024,
    xl: 1280,
};

const getSizeName = (width) => {
    const breakpoints = Object.keys(SCREEN_BREAK_POINT);

    for (const breakpoint of breakpoints) {
        if (width <= SCREEN_BREAK_POINT[breakpoint]) {
            return breakpoint;
        }
    }

    return 'optimized';
};

export default function NextCustomImageLoader({ src, width }) {
    const regex = /\/_next\/static\/media\/([^\/.]+)\.[^\/.]+(\.\w+)?$/;

    const isModuleImportMatch = src.match(regex);

    if (process.env.NODE_ENV === 'development') return src;

    if (isModuleImportMatch) {
        const fileName = isModuleImportMatch[1];
        const sizeName = getSizeName(width);

        if (RESULT[fileName]) {
            const fileSrc = RESULT[fileName][sizeName];

            if (fileSrc) {
                return fileSrc;
            } else {
                console.error(
                    `Image size ${sizeName} not found for ${fileName} in result.json`,
                );
            }
        }

        return src;
    }

    return src;
}
  1. Add these two inside next.config.js
    images: {
        loader: 'custom',
        loaderFile: './next-custom-image-loader.js',
        unoptimized: process.env.NODE_ENV === 'development',
    },

        webpack: (config, { dev, isServer }) => {

            config.module.rules.push({
            test: /\.(png|jpg|webp)$/i,
            use: [
                {
                    loader: 'nextjs-image-optimization-loader',
                    options: {
                        validationOnly:
                            process.env.SKIP_OPTIMIZATION === 'true'
                                ? true
                                : false,
                    },
                },
            ],
        });

        //Sometimes image file transformation stops because of cache
        config.cache = false;

        //Sometimes result.json file making stops because of webpack's paralle processing
        config.parallelism = 1;

      }

Options

The loader accepts the following options:

•	validationOnly: When set to true, the loader only validates images without processing.
•	screenBreakPoint: An object defining custom breakpoints for responsive image generation. Default breakpoints are:
•	  sm: 640px
•	  md: 768px
•	  lg: 1024px
•	  xl: 1280px

Contribution

Feel free to contribute to this project by submitting issues or pull requests. Your help is greatly appreciated!

License

This project is licensed under the MIT License. See the LICENSE file for details.