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

next-lqip-images

v0.1.0

Published

A Next.js plugin that helps you import images into your project and provide a low quality image to use as a placeholder.

Downloads

22

Readme

Next.js Lqip images

npm

A Next.js plugin that helps you import images into your project and provide either a low quality image or an array of dominant colors to use as a placeholder while the image loads.

Features

  • import local or CDN images into your Next.js project.
  • Provide a low quality image to use as a placeholder until the original downloads for better user experience.
  • Provide an array of hex values that represents the dominant colors of the image along with the width and height to use as a placeholder while the image loads.

Installation

npm install --save next-lqip-images

or

yarn add next-lqip-images

Usage

Add the following to the next.config.js at the root of your project.

const withLqipImages = require('next-lqip-images')

module.exports = withLqipImages()

you can also supply your custom Next.js configuration as well

const withLqipImages = require('next-lqip-images')

module.exports = withImages({
  // Your Next.js config goes here!!!
})

Or with the next-compose-plugins plugin if you are using multiple plugins

const withPlugins = require('next-compose-plugins')
const withLqipImages = require('next-lqip-images')

module.exports = withPlugins(
  [
    withLqipImages,
    {
      /// your options here!!!
    },
  ],

  /// other plugins here!!!
)

Now in your project you can either import images like normally

import image from 'path/to/your/image/here'
// this will give you back either a url or a base64 image uri
// based on how big the image is and your line file limit

export const myComponent = () => {
  return (
    <div>
      <img src={image} />
    </div>
  )
}

lqip

add the ?lqip query param at the end of your import to generate a low quality image placeholder

import {src, width, height, dataURI} from './image.jpg?lqip'

by default, the loader will return the placeholder in jpeg format for maximum browser support. it is however possible to switch to webp using the &webp query param, which will result in a much smaller image size

import image from './image.jpg?lqip&webp'

commonly, a blur is added to the image placeholder using css for better looks. the scale is used here together with an overflow: hidden on the parent to hide the artifacts around the edges

.placeholder {
  filter: blur(24px);
  transform: scale(1.1);
}

to avoid going throw that, you can just simply add the &blur query param to get a blurred placeholder image by default.

import image from './image.jpg?lqip&webp&blur'
import image2 from './image.png?lqip&blur'

Note: the query params are composable but the lqip must be added at the beginning!

the above mentioned imports will return the following:

{
  src: string // the source of the original image (using file-loader in the background)
  width: number // the width of the placeholder image
  height: number // the height of the placeholder image
  dataURI: string // the placeholder image Base64-URI
}

color palette

to get an array of the dominant colors to use as a placeholder instead, simply add the ?colors query param at the end of your imported image

import {src, width, height, colors} from './image.jpg?colors'

in this case, the returned values will be like following:

{
  src: string // the source of the original image (using file-loader in the background)
  width: number // the width of the original image
  height: number // the height of the original image
  colors: string[] // an array of the hex color codes representing the dominant colors of the image
}

Options

assetPrefix

Will prefix your assets with the provided URL (for example if you are using a CDN)

you can also enable the dynamic (runtime) asset prefix be setting dynamicAssetPrefix to true.

const withLqipImages = require('next-lqip-images')

module.exports = withLqipImages({
  assetPrefix: 'https://example.com',
  dynamicAssetPrefix: true,
})

InlineLimit

will return images smaller that the provided value as a base64 URI. default size is 2000

Example:

const withLqipImages = require('next-lqip-images')

module.exports = withLqipImages({
  inlineLimit: 12000,
})

in this case an image placeholder is most likely not necessary as the image will be included in your bundle.

Exclude

excludes folder from being handled by the plugin.

Example :

const path = require('path')
const withLqipImages = require('next-lqip-images')

module.exports = withLqipImages({
  exclude: path.resolve(__dirname, 'src/assets/svg'),
})

File Extensions

the file extensions should be handled by this plugin.

Using with TypeScript: If you exclude a file suffix you might need to use declaration merging or override dependencies for the same file suffixes as needed.

Example usage:

const withLqipImages = require('next-lqip-images')

module.exports = withLqipImages({
  fileExtensions: ['jpg', 'jpeg', 'png', 'gif'],
})

placeholderSize

default: 24

by setting this to a number, it will set the width and height of the placeholder image to a maximum of the provided number while maintaining the aspect ratio.

example:

const withLqipImages = require('next-lqip-images')

module.exports = withLqipImages({
  placeholderSize: 32,
})
// an example output would be
{
  src: '...', //image source
  width: 16, // placeholder width
  height: 32, // placeholder height
  dataURI: '...', // placeholder image URI
}

and by setting this to an Array of numbers, you can specify the width and height of the placeholder image.

example:

const withLqipImages = require('next-lqip-images')

module.exports = withLqipImages({
  placeholderSize: [32, 32],
})
// an example output would be
{
  src: '...', //image source
  width: 32, // placeholder width
  height: 32, // placeholder height
  dataURI: '...', // placeholder image URI
}

placeholderBlur

default: 2.4

you can also set the amount of blur you want to be applied to the images. I found that deviding the size by 10 is a good point to start with!

example:

const withLqipImages = require('next-lqip-images')

module.exports = withLqipImages({
  placeholderSize: 32,
  placeholderBlur: 3.2,
})

Typescript

For Typescript users, you will need to include a reference to the next-lqip-images types for your imports to work properly

in your next-env.d.ts file at the root of your project, add the following line:

/// <reference types="next" />
/// <reference types="next/types/global" />

+ /// <reference types="next-lqip-images" />