@blazity/next-image-proxy

中文文档
1.0.2发布于 2022/03/04MITTypeScriptESM + CJS近 30 天下载 6095

Next.js image proxy. Makes it possible to use dynamic domains in next/image component.

原始使用文档
文档目录5 节
  1. ❔ Motivation
  2. 🧰 Installation
  3. 💻 Use
  4. 🤲🏻 Contributing
  5. Creating A Pull Request

Next.js Image Proxy

Image proxy for Next.js. Makes it possible to use dynamic domains in next/image component.


npm version badge license badge

❔ Motivation

This library makes it possible to use next/image with dynamic domains. If you work with external providers, like Facebook, Instagram, Etsy, Medium, and others, the images often have dynamic subdomains. For example, you might get the first image from scontent-akl1-1.cdninstagram.com and the second one from scontent-akl3-1.cdninstagram.com. Although adding them one by one to the config could work temporarily, it would not be reliable since they can change at any time. The whole issue could be resolved by adding a regex pattern to next.config.js, but unfortunately, Next.js doesn't support that.

If you want to follow the discussion about Next.js supporting it outside of the box, please refer to this Discussion and this Pull Request

You have to remember that there're some cons:

  • You can create a security loophole if your regex isn't strict enough
  • Since it is a proxy, it will increase bandwidth costs. But the increase will be marginal unless you're working on big scale project (i.e. mils of requests per month)

🧰 Installation

$ npm i --save @blazity/next-image-proxy

# or

$ yarn add @blazity/next-image-proxy

💻 Use

It is really simple to setup, you just need to add a new API route that exports one function. The name of the endpoint is up to you.

// pages/api/imageProxy.ts

import { withImageProxy } from '@blazity/next-image-proxy'

export default withImageProxy({ whitelistedPatterns: [/^https?:\/\/(.*).medium.com/] })

and now you prefix the image you want to use:

import NextImage from 'next/image'

export function SomeComponent() {
  const actualImageUrl = 'https://cdn-images-1.medium.com/max/1024/1*xYoAR2XRmoCmC9SONuTb-Q.png'

  return <NextImage src={`/api/imageProxy?imageUrl=${actualImageUrl}`} width={700} height={300} />
}

🤲🏻 Contributing

Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated.

  • If you have suggestions for adding or removing projects, feel free to open an issue to discuss it, or directly create a pull request after you edit the README.md file with necessary changes.
  • Create individual PR for each suggestion.

Creating A Pull Request

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

版本与发布记录

@blazity/next-image-proxy 共发布 3 个版本,首次发布于 2022/03/04;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.2latest2022/03/044 年前
1.0.12022/03/044 年前
1.0.02022/03/044 年前

关于 @blazity/next-image-proxy 的常见问题

@blazity/next-image-proxy 是什么?

Next.js image proxy. Makes it possible to use dynamic domains in next/image component.

如何安装 @blazity/next-image-proxy?

在项目目录运行 npm install @blazity/next-image-proxy,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:next(>=9.0.0)、react(>=16.0.0)、react-dom(>=16.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@blazity/next-image-proxy 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@blazity/next-image-proxy 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@blazity/next-image-proxy 可以商用吗?

@blazity/next-image-proxy 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@blazity/next-image-proxy 的下载量有多少?

根据 npm 官方下载统计,@blazity/next-image-proxy 在 2026/10/09 – 2026/11/07 这 30 天内被下载 6,095 次,日均约 203 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@blazity/next-image-proxy 还在维护吗?

最新版本 1.0.2 发布于 2022/03/04(4 年前);至今共发布 3 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/03/04。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。