Git Product home page Git Product logo

netlify-ipx's Introduction

Netlify Optimized Images

On Demand image optimization 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 nickytonline avatar marcl avatar sarahetter avatar dependabot[bot] avatar ericapisani avatar pi0 avatar dustincrogers avatar mannil avatar lexicondevil avatar eduardoboucas avatar pieh avatar pcn avatar

Stargazers

Rudy Portillo avatar Thomas Deinhamer avatar Tu Nguyen avatar Monish Atom avatar Marcel Pfeifer avatar Salem Aziel avatar  avatar Naveen MC avatar Leonardo Cavalcante avatar Crystal_Alchemist avatar Eddy Recio avatar Charles Kornoelje avatar Hossein Mirazimi avatar Qin Guan avatar Mehmet Ali Külahçı avatar Asım Tahir avatar (TS/JS).Gandalf(he/him) avatar Baki avatar Simon Babay avatar ハン / Han avatar Robert Soriano avatar  avatar Hisco avatar Joan León avatar

Watchers

James Cloos avatar  avatar

netlify-ipx's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/ci.yml
  • actions/setup-node v3
  • actions/cache v3
.github/workflows/codeql-analysis.yml
  • actions/checkout v4
  • github/codeql-action v2
  • github/codeql-action v2
  • github/codeql-action v2
.github/workflows/labeler.yml
  • netlify/pr-labeler-action v1.1.0
.github/workflows/release-please.yml
  • navikt/github-app-token-generator a3831f44404199df32d8f39f7c0ad9bb8fa18b1c
  • GoogleCloudPlatform/release-please-action v3
  • actions/checkout v4
  • actions/setup-node v3
npm
package.json
  • @netlify/functions ^2.8.1
  • etag ^1.8.1
  • fs-extra ^11.0.0
  • ipx ^1.3.1
  • micromatch ^4.0.5
  • mkdirp ^3.0.0
  • murmurhash ^2.0.0
  • node-fetch ^2.0.0
  • ufo ^1.0.0
  • unstorage 1.12.0
  • @nuxtjs/eslint-config-typescript ^12.0.0
  • @types/etag ^1.8.1
  • @types/fs-extra ^11.0.0
  • @types/node-fetch ^2.6.1
  • ava ^5.0.0
  • esbuild-node-loader ^0.8.0
  • eslint 8.57.0
  • jiti ^1.13.0
  • ts-node ^10.9.1
  • typescript ^5.0.0

  • Check this box to trigger a request for Renovate to run again on this repository

Add release-please

This repo needs to have releases managed by release-please, like our other node modules.

[security] Enforce Branch Protections

This is an issue generated by (for-internal-use-only)github-tools

Description

This repository has exceeded the development grace period, and the repo owner must decide if branch protections should be enforced.
If this repository houses code used in production in any capacity, branch protections must be enforced. Read (for-internal-use-only)here for more details

What do I need to do?

  • You can control whether or not branch protections are enforced by exclusively creating either of the labels:
    'enforce-branch-protections: true' or 'enforce-branch-protections: false'
  • If there is no branch protection control label, this issue will be recreated.
  • Once you have created either label, you can close this issue.
  • If you have issues or questions, please reach out to #internal-security-n-compliance on slack.

Problem with images loaded from external sources without content-type header

Hi team,

As discussed in our weekly sync regarding the ticket (internal only): https://netlify.zendesk.com/agent/tickets/75571, WEBP images requested from an external API that do not return a valid content-type header fail to load. The Next.js plugin makes a request to the IPX function and that returns a 502 with the error:

{"errorType":"TypeError","errorMessage":"Cannot read property 'startsWith' of null","trace":["TypeError: Cannot read property 'startsWith' of null","    at loadSourceImage (/var/task/node_modules/@netlify/ipx/dist/http.js:111:46)","    at async handler (/var/task/node_modules/@netlify/ipx/dist/index.js:56:54)"]}

This issue was last reproducible with Plugin v4 RC1.

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Location: renovate.json
Error type: The renovate configuration file contains some invalid settings
Message: Invalid configuration option: includeForks

Move to pnpm or Yarn >=3

This repo appears to be using Yarn v1, which hasn't received any significant attention since 2020 -- although funnily enough, v1.22.21 was released roughly 4 hours ago!

As the Yarn 1 repo says:

ℹ️ Important note
This repository holds the sources for Yarn 1.x (latest version at the time of this writing being 1.22). New releases (at this time the 3.2.3, although we're currently working on our next major) are tracked on the yarnpkg/berry repository, this one here being mostly kept for historical purposes and the occasional hotfix we publish to make the migration from 1.x to later releases easier.

If you hit bugs or issues with Yarn 1.x, we strongly suggest you migrate to the latest release - at this point they have been maintained longer than 1.x, and many classes of problems have already been addressed there. By using the nodeLinker setting you'll also have the choice of how you want to install your packages: node_modules like npm, symlinks like pnpm, or manifest files via Yarn PnP.

Yarn timeline (partial):

January 2019: Work on Yarn 2 began
January 2020: Yarn 2 released
February 2020: Yarn v1.22.0 released
July 2021: Yarn 3 released
October 2023: Yarn 4 released

pnpm

pnpm seems to be very popular nowadays, a lot of high-profile open source projects are using it.

Here are some yarn --> pnpm migration PRs:

Vue: vuejs/core#4766
Next.js: vercel/next.js#37259
Vite: vitejs/vite#5060
Pinia: vuejs/pinia#1179
Cycle.js: cyclejs/cyclejs#821
Verdaccio: verdaccio/verdaccio#1884
Element Plus: element-plus/element-plus#3571
Agora Flat: netless-io/flat#1442
Payload: payloadcms/payload#3128

yarn --> pnpm migration commits (couldn't locate PRs):

Browserlist: browserslist/browserslist@6d0c552
PostCSS: postcss/postcss@ec80417

Also using pnpm:

SvelteKit: https://github.com/sveltejs/kit
Prisma: https://github.com/prisma/prisma
Nhost: https://github.com/nhost/nhost
IPX: https://github.com/unjs/ipx

Yarn 4

As briefly mentioned above, Yarn 4 was released about 3 weeks ago, huzzah! I haven't had a chance to play with it, or find out how others are getting on with it. This was an interesting skim: https://news.ycombinator.com/item?id=37985779


Note: everything above is largely copied from microsoft/vscode#162803. I did not attempt to find new migration PRs (edit: added Payload PR), or new "high-profile open source projects" using pnpm, however I did:

  1. Quickly verify that all of the repos listed above as using pnpm are still doing so (superficial check: they all still have ./pnpm-lock.yaml files)
  2. Updated the Yarn 4 section
  3. Added IPX to the "Also using pnpm" list

[Bug]: Webp image format is returned for Safari 13.1.1

Summary

I have a site using Next v12.2.3. I haven't customized any of the configuration. I deployed it to Netlify and I'm using some next/image tags on some pages. Images were loading fine in Chrome and Firefox. When I opened the site on Safari 13.1.1 I noticed that the images were broken. Safari 12.2.3 doesn't support webp but it seems that the site was returning images in webp format anyway.

The image request Accept header for Safari 13.1.1 was image/png,image/svg+xml,image/*;q=0.8,video/*;q=0.8,*/*;q=0.5. The response content-type header was image/webp.

I couldn't figure out how to fix it and ended up setting NEXT_DISABLE_EDGE_IMAGES to true so that it's forced to return the original format for all browsers, png.

I know that the ipx function is involved here, but I'm not sure why it's choosing to return webp in this case or where the content-type response header is being set.

Any idea why this might be happening?

Steps to reproduce

  1. Create a Next site with v12.2.3
  2. Use a png image on a page using the next/image component
  3. Deploy the site to Netlify
  4. Load the page in Safari 12.2.3

A link to a reproduction repository

No response

Plugin version

4.14.2

More information about your build

  • I am building using the CLI
  • I am building using file-based configuration (netlify.toml)

What OS are you using?

Mac OS

Your netlify.toml file

`netlify.toml`
# Paste content of your `netlify.toml` file here

Your public/_redirects file

`_redirects`
# Paste content of your `_redirects` file here

Your next.config.js file

`next.config.js`
const nextConfig = {
  reactStrictMode: true,
  experimental: {
    newNextLinkBehavior: true,
    images: {
      allowFutureImage: true,
    },
  },
}

module.exports = nextConfig

Builds logs (or link to your logs)

Build logs
# Paste logs here

Function logs

Function logs
# Paste logs here

.next JSON files

generated .next JSON files
# Paste file contents here. Please check there isn't any private info in them
# You can either build locally, or download the deploy from Netlify by clicking the arrow next to the deploy time.

Add AVA for testing

Description

I need to write some tests for a fix and we currently do not have testing in place for the project.

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.