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

react-aws-sih-loader

v1.0.19

Published

React image loader component for AWS ServerlessImageHandler (SIH)

Downloads

26

Readme

React Image Loader Component for AWS ServerlessImageHandler (SIH)

Small React / Next.js component to load amd lazy load large images from Amazon S3 via AWS ServerlessImageHandler.

See demos at https://react-aws-sih-loader-nextjs-demo.now.sh/.

Source code at https://github.com/sztulev/react-aws-sih-loader.

Introduction

Amazon S3 may be one option to host large images of a Next.js website or React app. The AWS Serverless Image Handler (SIH) can come handy to dynamically transform those images and adjust their size and to significantly improve the perfomance of your website. This npmjs package exports several React component that can help you transforming and loading images from AWS SIH API. In order to use it, at the minimum, you will need a AWS Account and AWS SIH deployed. Follow this link to find out more details on how to setup an AWS SIH solution.

About AWS Serverless Image Handler

The AWS Serverless Image Handler automatically deploys and configures an AWS serverless architecture that uses Amazon S3 for storage, AWS Lambda and the open source image processing suite Sharp for image manipulation, and Amazon CloudFront for global content delivery. For more details on the AWS ServerlessImageHandler, see AWS SIH Documentation

Installation

npm install --save react-aws-sih-loader@latest

or:

yarn add react-aws-sih-loader

IMPORTANT: Under the hood, it makes use of hooks, therefore, using it requires React >= 16.8.

Usage

Use AWSSIHImg to resize and load a single image. The below example will resize the given image to width of 640px and displays it as an <img> tag.

import React from 'react';
import { AWSSIHImg as Img } from 'react-aws-sih-loader';

function MySIHImg() {
    return (
        <Img src="large-photo-01.JPG" 
            config={{
                endpoint: 'https://cxjchicdsxdfl3.cloudfront.net',
                bucket: 'bucket-for-my-images',
                width: 640, 
                normalize: true,
                resizeMode:'cover',
            }} />
    )
}

Use AWSSIHLazyLoadImg if you want display an image placeholder before the large image gets fully loaded. It wraps <img> in a <div> tag with a low-res image in the background while the large image is being transformed and loaded.

import React from 'react';
import { AWSSIHLazyLoadImg as Img } from 'react-aws-sih-loader';

function MySIHImg() {
    return (
        <Img src="large-photo-01.JPG" 
            config={{
                endpoint: 'https://cxjchicdsxdfl3.cloudfront.net',
                bucket: 'bucket-for-my-images',
                width: 640, 
                normalize: true
            }} />
    )
}

AWSSIHBackgroundImage and AWSSIHLazyLoadBackgroundImage can be used to add background images to content.

import React from 'react';
import { AWSSIHBackgroundImage as BckImg } from 'react-aws-sih-loader';

function MySIHImg() {
    return (
        <BckImg src="static/img/large-photo-02.JPG" 
            config={{
                endpoint: 'https://cxjchicdsxdfl3.cloudfront.net',
                bucket: 'bucket-for-my-images',
                width: 2048, 
                normalize: true
            }}>
            <p>Hello</p>
        </BckImg>
    )
}

AWSSIHContext component provides a common configuration option at any higher level. It uses React's Context in the background to pass down configuration items.

import React from 'react';
import { AWSSIHImg as Img, AWSSIHContext } from 'react-aws-sih-loader';

function MySIHImg() {
  return (
        <AWSSIHContext config={{
            endpoint: 'https://cxjchicdsxdfl3.cloudfront.net',
            bucket: 'bucket-for-my-images',
            width: 2048, 
            normalize: true
            }}>
            <ul>
                <li>
                    <Img src="static/img/large-photo-01.JPG" />
                </li>
                <li>
                    <Img src="static/img/large-photo-02.JPG" />
                </li>
                <li>
                    <Img src="static/img/large-photo-03.JPG" />
                <li>
            </ul>
        </AWSSIHContext>
  )
}

Furthermore, multiple AWSSIHContext components can be embedded down the chain, so configuration items can be added or overwritten at lower levels.

import React from 'react';
import { AWSSIHImg as Img, AWSSIHContext } from 'react-aws-sih-loader';

function MySIHImg() {
  return (
        <AWSSIHContext config={{
            endpoint: 'https://cxjchicdsxdfl3.cloudfront.net',
            }}>
            <div>
                <AWSSIHContext config={{
                    bucket: 'ultra-wide-images',
                    width: 2048
                    }}>
                    <Img src="static/img/large-photo-01.JPG" />
                    <Img src="static/img/large-photo-02.JPG" />
                </AWSSIHContext>
            </div>
            <div>
                <AWSSIHContext config={{
                    bucket: 'regular-images',
                    width: 640
                    }}>
                    <Img src="static/img/photo-03.JPG" />
                    <Img src="static/img/photo-04.JPG" />
                </AWSSIHContext>
            </div>
        </AWSSIHContext>
  )
}

Configuration

The config property object can be applied on any component. endpoint and bucket values must be provided in the configuration chain.

  • endpoint required the CloudFront distribution endpoint created by AWS SIH
  • bucket required the Amazon S3 bucket to load the original image from
  • width target width of the image after transformation
  • height target height of the image after transformation (if none of width or height is provided, the image will be fetch in its original size),
  • resizeMode 'cover', 'contain', 'fill, 'outside', 'inside' (see Sharp docs)
  • normalize true or false, enhance output image contrast
  • grayscale true or false, convert image to grayscale
  • previewWidth width of the preview image when using lazy loading components
  • previewHeight default value: 50. height of the preview image when using lazy loading components
  • previewResizeMode same as resizeMode, but applied on the preview image when using lazy loading components
  • previewGrayscale true or false, convert the preview image to grayscale
  • debug true or false, enable printing the API request onto the console.