Git Product home page Git Product logo

netlify-ipx's Introduction

Netlify Optimized Images

On Demand image optimazation for Netlify using ipx.

๐Ÿ˜บ Online demo: https://netlify-ipx.netlify.app

Usage

Add @netlify/ipx as devDependency:

# npm
npm i -D @netlify/ipx

# yarn
yarn add --dev @netlify/ipx

Create netlify/functions/ipx.ts:

import { createIPXHandler } from "@netlify/ipx";

export const handler = createIPXHandler({
  domains: ["images.unsplash.com"],
});

Now you can use IPX to optimize both local and remote assets โœจ

Resize /test.jpg (in dist):

<img src="/.netlify/functions/ipx/w_200/static/test.jpg" />

Resize and change format for a remote url:

<img
  src="/.netlify/functions/ipx/f_webp,w_450/https://images.unsplash.com/photo-1514888286974-6c03e2ca1dba"
/>

Remote Patterns

Instead of setting an allowlist on domains, you may wish to use the option remotePatterns. This method allows wildcards in hostname and pathname segments.

remotePatterns is an array that contains RemotePattern objects:

remotePatterns: [
  {
    protocol: 'https' // or 'http' - not required
    hostname: 'example.com' // required
    port: '3000' // not required
    pathname: '/blog/**' // not required
  }
]

To use remote patterns, create netlify/functions/ipx.ts:

import { createIPXHandler } from "@netlify/ipx";

export const handler = createIPXHandler({
  remotePatterns: [
    {
      protocol: "https",
      hostname: "images.unsplash.com",
    },
  ],
});

hostname and pathname may contain wildcards:

remotePatterns: [
  {
    hostname: '*.example.com' // * = match a single path segment or subdomain
    pathname: '/blog/**' // ** = match any number of path segments or subdomains
  }
]

Local development

  • Clone repository
  • Install dependencies with yarn install
  • Build the project with yarn build
  • Run netlify development server with yarn dev.
  • Open http://localhost:8888

License

MIT

netlify-ipx's People

Contributors

renovate[bot] avatar ascorbic avatar token-generator-app[bot] avatar marcl avatar sarahetter avatar ericapisani avatar nickytonline avatar pi0 avatar dustincrogers avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.