diff --git a/docs/basic-features/image-optimization.md b/docs/basic-features/image-optimization.md index 551622ff0f359..e6b76877c54f9 100644 --- a/docs/basic-features/image-optimization.md +++ b/docs/basic-features/image-optimization.md @@ -88,6 +88,7 @@ The following Image Optimization cloud providers are supported: - [Imgix](https://www.imgix.com): `loader: 'imgix'` - [Cloudinary](https://cloudinary.com): `loader: 'cloudinary'` - [Akamai](https://www.akamai.com): `loader: 'akamai'` +- [Aliyun](https://www.aliyun.com): `loader: 'aliyun'` - Default: Works automatically with `next dev`, `next start`, or a custom server ## Caching diff --git a/errors/invalid-images-config.md b/errors/invalid-images-config.md index c060c62728da3..a4f6b886d8b04 100644 --- a/errors/invalid-images-config.md +++ b/errors/invalid-images-config.md @@ -18,7 +18,7 @@ module.exports = { // limit of 50 domains values domains: [], path: '/_next/image', - // loader can be 'default', 'imgix', 'cloudinary', or 'akamai' + // loader can be 'default', 'imgix', 'cloudinary', 'akamai', or 'aliyun loader: 'default', }, } diff --git a/packages/next/client/image.tsx b/packages/next/client/image.tsx index fb5e5dc95bf38..c6c16bffdb3e3 100644 --- a/packages/next/client/image.tsx +++ b/packages/next/client/image.tsx @@ -10,10 +10,11 @@ const loaders = new Map string>([ ['imgix', imgixLoader], ['cloudinary', cloudinaryLoader], ['akamai', akamaiLoader], + ['aliyun', aliyunLoader], ['default', defaultLoader], ]) -type LoaderKey = 'imgix' | 'cloudinary' | 'akamai' | 'default' +type LoaderKey = 'imgix' | 'cloudinary' | 'akamai' | 'aliyun' | 'default' const VALID_LAYOUT_VALUES = [ 'fill', @@ -493,6 +494,21 @@ function cloudinaryLoader({ root, src, width, quality }: LoaderProps): string { return `${root}${paramsString}${normalizeSrc(src)}` } +function aliyunLoader({ root, src, width, quality }: LoaderProps): string { + // Demo: http://image-demo.oss-cn-hangzhou.aliyuncs.com/example.jpg?x-oss-process=image/resize,w_300/quality,q_90 + const params = ['image'] + if (width) { + params.push(`resize,w_${width}`) + } + if (quality) { + params.push(`quality,q_${quality}`) + } + + const paramString = + params.length > 1 ? `?x-oss-process=${params.join('/')},` : '' + return `${root}${normalizeSrc(src)}${paramString}` +} + function defaultLoader({ root, src, width, quality }: LoaderProps): string { if (process.env.NODE_ENV !== 'production') { const missingValues = []