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

@outsmartly/plugin-image-optimizations

v0.0.74

Published

Image optimization plugin for the Outsmartly Platform. Image resizing, HTTP/2 Push, and more.

Downloads

2

Readme

@outsmartly/plugin-image-optimizations

Outsmartly's image optimization plugin provides a convenient mechanism to optimize images.

The plugin combines an image resizing and compression service of your choice—such as Cloudinary, imgix, or others—with intelligent device detection and HTTP/2 Push (or Link rel=preload), all served from your first-party domain; for even more performance gains.

With it, you'll be able to serve different sizes, compression levels, or even totally different images based on device type. Add the data-outsmartly-preload attribute to any images that are critical (e.g. above the fold) and Outsmartly will also automatically push the right image to the browser too.

Here's an example, if you were using Cloudinary:

export default function ExamplePage() {
  return (
    <>
      <h1>Example</h1>
      <img
        src={imgSrc({
          mobile: 'w_200,e_sharpen/remote_media/my-example.jpg',
          desktop: 'w_600,e_sharpen/remote_media/my-example.jpg',
        })}
        // Uses HTTP/2 Push (or Link rel=preload if not supported)
        data-outsmartly-preload
      />
    </>
  );
}

Install

npm install --save @outsmartly/plugin-image-optimizations
# or
yarn add @outsmartly/plugin-image-optimizations

Setup

Inside your outsmartly.config.js you can call the imageOptimizationPlugin() function and pass the result into the plugins array like so:

import { imageOptimizationPlugin } from '@outsmartly/plugin-image-optimizations';

export default {
  host: 'your-project.outsmartly.app',
  environments: [
    {
      name: 'production',
      origin: 'https://your-origin.com',
    },
  ],
  plugins: [imageOptimizationPlugin()],
  //        ^^^^^^^^^^^^^^^^^^^^^^^^^
};

React App

The imageOptimizationFormatter() factory function generates another function you then use inside your components to generate your <img src> URLs:

import { imageOptimizationFormatter } from '@outsmartly/plugin-image-optimizations';

// Export this inside a utils.js file or similar,
// then you can import it throughout your app
export const imgSrc = imageOptimizationFormatter({
  baseURL: 'https://some-image-service.com/example/image/upload/',
});

export default function ExamplePage() {
  return (
    <>
      <h1>Example</h1>
      <img
        src={imgSrc({
          // mobile/tablet are optional
          mobile: 'w_200,e_sharpen/remote_media/my-example.jpg',
          tablet: 'w_400/remote_media/my-example.jpg',
          desktop: 'w_600/remote_media/my-example.jpg',
        })}
        // Uses HTTP/2 Push (or Link rel=preload if not supported)
        data-outsmartly-preload
      />
    </>
  );
}

Note that mobile and tablet are optional, but desktop is not as it is used as the fallback.

Options

interface FormatterOptions {
  /**
   * Required. The remote service you wish for Outsmartly's CDN edge servers to
   * call to get the image.
   */
  baseURL: string;

  /**
   * Optional. Change the base path used. Defaults to: '/outsmartly-images'
   */
  path?: string;
}

interface Choices {
  // Only 'desktop' is required, and will be used as the fallback
  mobile?: string;
  tablet?: string;
  desktop: string;
}

type imageOptimizationFormatter = (options: FormatterOptions) => (choices: Choices) => string;

interface PluginOptions {
  /**
   * Optional. Change the base path used. Defaults to: '/outsmartly-images'
   */
  path?: string;
}

type imageOptimizationPlugin = (options?: PluginOptions) => Plugin;

FAQ

Does it HTTP/2 Push the correctly chosen image? mobile, tablet, desktop?

Yes it does! 🧙‍♂️

What about background-image and background-size: cover?

Because screen readers and search engine bots don't typically "see" CSS background images, they are best suited for when the image has no semantic meaning. In contrast, for images like your Hero image above the fold, visualizations, diagrams, and similar, it's important to use an <img> element.

If you need behavior similar to background-size: cover, modern browsers (not IE11) support object-fit: cover.

What about browser/proxy caching?

We set the Vary: User-Agent and Cache-Control: no-transform headers, so browsers (and others) will not accidentally show the wrong image.