Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Aliyun OSS image loader #18965

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/basic-features/image-optimization.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion errors/invalid-images-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
}
Expand Down
18 changes: 17 additions & 1 deletion packages/next/client/image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ const loaders = new Map<LoaderKey, (props: LoaderProps) => 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',
Expand Down Expand Up @@ -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 = []
Expand Down